My console is giving me an error? - c++

Hello there I was wondering if you may help me with my code the g++ console says "28 37 C:\Users\paul\Documents\C++\main.cpp [Error] expected ';' before 'movie'"
#include <iostream>
#include <string>
using namespace std;
int main()
{
int tomscore;
string movie;
int metascore;
cout << "Hello there what movie are you wondering about?" << endl;
cin >> movie;
cout << "What is the Rotten Tomato score of the movie in decimal form?"<< endl;
cin >> tomscore;
cout << "What is the metascore?" << endl;
cin >> metascore;
int average = tomscore+metascore;
int averageGOD = average/2;
cout << "The average score for" " " movie " " "was" averageGOD << endl;
}

This is an error:
cout << "The average score for" " " movie " " "was" averageGOD << endl;
You need to use << in between each item you are sending to the stream. Also it is redundant to put the spaces in their own literal:
cout << "The average score for " << movie << " was " << averageGOD << endl;
If you were trying to output quote marks then use \" within the string.

Related

Cin not producing required results

i'm trying to set up a simple flight booking system program, but my second bit of cin code is not calling for input when i run the program. Unlike the initial cin that requires your name initially. the program just runs and returns 0. I'm a beginner at c++ and i know this is a simple fix so please be understanding . Thank you any guidance will be greatly appreciated.
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
int name;
int Seatnumber;
int optionnumber = 1-5 ;
std::string out_string;
std::stringstream ss;
ss << optionnumber;
out_string = ss.str();
cout << "Welcome to the COS1511 Flight Booking System" "\n" << endl;
cout << "Enter full name : " << endl;
cin >> name ; "\n";
cout << "\n" "The Available travel times for flights are:" << endl;
cout << " Depart Arrive" << endl;
cout << "1. 7.00 9.30" << endl;
cout << "2. 9.00 11.30" << endl;
cout << "3. 11.00 13.30" << endl;
cout << "4. 13.00 15.30" << endl;
cout << "5. 15.00 17.30" << endl;
cout << "Choose the time by entering the option number from the displayed list : " << endl;
cin >> optionnumber ;
if (optionnumber == 1-5){
cout << "\n" "The available seats for are as follows " << endl;
}
else
cout << "Incorrect option! Please Choose from 1-5 " << endl;
cout << "First Class(1920)" << endl;
cout << "|A1||A2||A3|----|A4||A5||A6|" << endl;
cout << "|B1||B2||B3|----|B4||B5||B6|" << endl;
cout << "|C1||C2||C3|----|C4||C5||C6|" << endl;
cout << "|D1||D2||D3|----|D4||D5||D6|" << endl;
cout << "| Economy Class(1600)" << endl;
cout << "|E1||E2||E3|----|E4||E5||E6|" << endl;
cout << "|F1||F2||F3|----|F4||F5||F6|" << endl;
cout << "|G1||G2||G3|----|G4||G5||G6|" << endl;
cout << "|H1||H2||H3|----|H4||H5||H6|" << endl;
cout << "|I1||I2|" << endl;
cout << "Please Key in a seat number to choose a seat(eg: A2)" << endl;
cin >> Seatnumber;
}
prompt the user to enter their name.
Then display a menu showing the available times for the flight.
the user can choose a preferred departure time(option 1-5)
the option selected should be validated for 1-5
if the user entered the correct time a seating arrangement for that particular flight time should be displayed to the next user for the user to choose a seat.
Warning
int optionnumber = 1-5 ;
does
int optionnumber = -4 ;
and
if (optionnumber == 1-5){
does
if (optionnumber == -4){
but you wanted if ((optionnumber >= 1) && (optionnumber <= 5))
if the user entered the correct time a seating arrangement for that particular flight time should be displayed to the next user for the user to choose a seat.
No, whatever the result of the test above you continue and write "First Class(1920)" etc so even when the choice is invalid
in
cin >> name ; "\n";
what did you expect about the "\n" ?
I encourage you to check the read success, currently if the user does not enter an integer you do not know that
But are you sure the name must be an integer ? Probably it must be s string
out_string is unused, it can be removed
Visibly Seatnumber is not an int but a string (A1 ...)
you probably want to loop until a valid time is enter, also fixing the other problems a solution can be :
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
string Seatnumber;
int optionnumber;
cout << "Welcome to the COS1511 Flight Booking System" "\n" << endl;
cout << "Enter full name : " << endl;
if (!(cin >> name))
// EOF (input from a file)
return -1;
cout << "\n" "The Available travel times for flights are:" << endl;
cout << " Depart Arrive" << endl;
cout << "1. 7.00 9.30" << endl;
cout << "2. 9.00 11.30" << endl;
cout << "3. 11.00 13.30" << endl;
cout << "4. 13.00 15.30" << endl;
cout << "5. 15.00 17.30" << endl;
cout << "Choose the time by entering the option number from the displayed list : " << endl;
for (;;) {
if (!(cin >> optionnumber)) {
// not an int
cin.clear(); // clear error
string s;
// flush invalid input
if (!(cin >> s))
// EOF (input from a file)
return -1;
}
else if ((optionnumber >= 1) && (optionnumber <= 5))
// valid choice
break;
cout << "Incorrect option! Please Choose from 1-5 " << endl;
}
cout << "\n" "The available seats for are as follows " << endl;
cout << "First Class(1920)" << endl;
cout << "|A1||A2||A3|----|A4||A5||A6|" << endl;
cout << "|B1||B2||B3|----|B4||B5||B6|" << endl;
cout << "|C1||C2||C3|----|C4||C5||C6|" << endl;
cout << "|D1||D2||D3|----|D4||D5||D6|" << endl;
cout << "| Economy Class(1600)" << endl;
cout << "|E1||E2||E3|----|E4||E5||E6|" << endl;
cout << "|F1||F2||F3|----|F4||F5||F6|" << endl;
cout << "|G1||G2||G3|----|G4||G5||G6|" << endl;
cout << "|H1||H2||H3|----|H4||H5||H6|" << endl;
cout << "|I1||I2|" << endl;
cout << "Please Key in a seat number to choose a seat(eg: A2)" << endl;
cin >> Seatnumber;
return 0;
}
Compilation and execution :
pi#raspberrypi:/tmp $ g++ -pedantic -Wextra -Wall cc.cc
pi#raspberrypi:/tmp $ ./a.out
Welcome to the COS1511 Flight Booking System
Enter full name :
bruno
The Available travel times for flights are:
Depart Arrive
1. 7.00 9.30
2. 9.00 11.30
3. 11.00 13.30
4. 13.00 15.30
5. 15.00 17.30
Choose the time by entering the option number from the displayed list :
aze
Incorrect option! Please Choose from 1-5
7
Incorrect option! Please Choose from 1-5
2
The available seats for are as follows
First Class(1920)
|A1||A2||A3|----|A4||A5||A6|
|B1||B2||B3|----|B4||B5||B6|
|C1||C2||C3|----|C4||C5||C6|
|D1||D2||D3|----|D4||D5||D6|
| Economy Class(1600)
|E1||E2||E3|----|E4||E5||E6|
|F1||F2||F3|----|F4||F5||F6|
|G1||G2||G3|----|G4||G5||G6|
|H1||H2||H3|----|H4||H5||H6|
|I1||I2|
Please Key in a seat number to choose a seat(eg: A2)
qsd
pi#raspberrypi:/tmp $
Note entering the name with cin >> name does not allow it to contain several names separated by a space, to allow composed name getline can be used

finding the present value using C++

assignment at school asks me to find the present value using double, and void. i was able to write my code up to a certain degree but the result is not what i was expecting.. i ended up separating the present value into different section so at the end i'd multiply the amount given with the rest.. any tips on how to make the code actually work the way its supposed to?
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double payment,year_term, interest;
double sum;
double Power;
double presentv;
double present;
cout << "Hello, how are you doing?" << endl;
cout << "Please insert a payment amount" << endl;
cin >> payment;
cout << " amount inserted: " << payment << endl;
cout << "Enter number of years" << endl;
cin >> year_term;
cout << " number of years: " << year_term << endl;
cout << "Enter interest rate" << endl;
cin >> interest;
cout << " the interest is: " << interest << "%" << endl;
Presentv = ((1 - (pow((1 + interest),year_term))))/interest;
cout << " the value: " << Presentv << endl;
presentva = payment * Presentv;
cout << " the present value is: " << presentva << endl;
}

Error: ld returned 1 exit status June 2015

I need help understanding the [Error] id return 1 exit status due to 2 undefined references: getGrades() and getAverage(). I was issued DEV C++ and knocked out the syntax errors with mild frustration but these "linking" errors are still giving me a hard time. This is the most recent update of the code, if anyone could help me understand these linking errors that would be great.
Compiler - Dev C++
Windows 7
Code:
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
using namespace std;
// Function declarations
string getStudentName();
string getWork();
int getGrades();
double getAverage();
int main()
{
string studentName, work[3];
int grades[3];
double average;
// Get the name of the student
studentName = getStudentName();
// Get the work
work[3] = getWork();
// Get the grades
grades[3] = getGrades();
// Get the average
average = getAverage();
// Dynamic spacing for grades
ostringstream ss;
int gradesLength = ss.str().length();
ss <<setprecision(0) << fixed << showpoint;
ss << grades;
cout << "\n the average for " << studentName << " is: " << average << endl;
cout << "The grades for " << studentName << " are: " << endl;
cout << setw(30) << work[0] << ": " << setw(gradesLength) << grades[0] << endl;
cout << setw(30) << work[1] << ": " << setw(gradesLength) << grades[1] << endl;
cout << setw(30) << work[2] << ": " << setw(gradesLength) << grades[2] << "\n\n\n";
cout << "You have completed the program: \n";
return 0;
}
// Student Name
string getStudentName()
{
string name;
cout << "Enter students full name: ";
getline(cin, name);
return name;
}
// Assignments
string getWork()
{
string work[3];
cout << "\nEnter the name of each assignment \n";
cout << "First assignment: ";
getline (cin, work[0]);
cout << "Second assignment: ";
getline (cin, work[1]);
cout << "Third assignment: ";
getline (cin, work[2]);
return work[3];
}
// Grades
int getGrades(string work[3])
{
int grades[3];
cout << "\nEnter the grade for " << work[0] << ": ";
cin >> grades[0];
cout << "Enter the grade for " << work[1] << ": ";
cin >> grades[1];
cout << "Enter the grade for " << work[2] << ": ";
cin >> grades[2];
return grades[3];
}
// Math
double getAverage(int grades[3])
{
double average;
average = (grades[0] + grades[1] + grades[2]) / 3.0f;
return average;
}

Change length of dashed line to match input string length?

#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
int income;
int tax;
cout << "What is your full name? ";
getline(cin,name);
cout << "What is your annual income? ";
cin >> income;
if (income < 50000)
{
tax = income*0.33;
}
else { tax = income*0.38; }
cout << "\t\t\t" << name << ": Tax Report" << endl;
cout << "\t\t\t" << "-------------------------" << endl;
cout << "Income =$" << income << endl;
cout << "Tax =$" << tax << endl;
system("pause");
return 0;
}
I would like the line of hyphens to match the length of the name string regardless of its length. I am in an introductory c++ class and I am sure there is a simple way to do this. Could anyone help?
cout << "\t\t\t" << string(name.length() + 12, '-') << endl;
just put in this line, instead of your current dash line
cout << "\t\t\t" << std::string(name.length(), '-') << endl;
for further information see http://en.cppreference.com/w/cpp/string/basic_string/basic_string
You can do it the following way
#include <iostream>
#include <iomanip>
int main()
{
std::string name( "01234567890" );
std::cout << "\t\t\t" << name << ": Tax Report" << std::endl;
std::cout << "\t\t\t" << std::setfill( '-' ) << std::setw( name.size() )
<< '-' << std::endl;
return 0;
}
The program output is
01234567890: Tax Report
-----------
If you want that all preceding line would be underlined then you need tp add the size of string ": Tax Report" to the argument of std:;setw For example
#include <iostream>
#include <iomanip>
int main()
{
std::string name( "01234567890" );
std::cout << "\t\t\t" << name << ": Tax Report" << std::endl;
std::cout << "\t\t\t" << std::setfill( '-' ) << std::setw( name.size() + 12 )
<< '-' << std::endl;
return 0;
}
In this case the porgram output is
01234567890: Tax Report
-----------------------
Thus all you need is to use functions std::setfill and std::setw declared in header <iomanip>. There is no need to a create a temporary object of type std::string because it is inefficient..
You can make a string of hyphens of a certain length. Get the length by first building a string for the name line and then calling its length method. Here is an example:
std::string hyphens;
hyphens.assign(length, '-');
check out thislink for more details on using strings:
http://www.cplusplus.com/reference/string/string/

Bank Account Program Issues with build and getting started

Onto another program. this is a program that simulates a bank account. I need help with the showing of the balance, withdraw, and deposit. how do i get this to show the final balance after the withdraw/deposit?
#include <iostream>
#include <string>
#include "BACCOUNT.H"
using namespace std;
int main ()
{
double amount = 0.0;
double withrdraw = 0.0;
double deposit = 0.0;
string name;
double startamount = 100.00;
double balance = 0.0;
cout << "name: ";
cin >> name;
cout << "initial balance: " << startamount <<endl;
cout << "deposit? ";
cin >> amount;
cout << "withdraw? ";
cin >> amount;
cout << "balance for " << name << " is " << balance << endl;
system ("pause");
return 0;
}
cout << "balance for " << name () << " is " << balance()
<<endl;
By putting parenthesis after them, you're trying to call name and balance like functions, but they're strings. Remove the parenthesis.
More importantly, what made you think you had to include parenthesis there? There may be a fundamental piece of C++ that you're confused about (functions) that you should seek to fully understand.
You are attempting to call name and balance as functions here:
cout << "balance for " << name () << " is " << balance() <<endl;
^^^^^^^ ^^^^^^^^^
but name is a string and balance is a double. This edit will fix the problem:
cout << "balance for " << name << " is " << balance <<endl;