I'm creating a basic car rental program, which also takes the details of the customer and shows it all as a nicely formatted invoice. Except i'm not that great at c++, what i've done so far is below but I keep getting an error saying [ERROR] else without a previous if. I thought I had done the if and else statements correctly but I must have done something wrong. Also i'm not entirely sure how to call the function from the main correctly. Can anyone help me with those and also any other improvements or advice please. Thanks.
#include <iostream>
#include <windows.h> //Allows me to enter headers
#define pound char(156)
usingnamespace std;
int car1 = 10;
int car2 = 20;
int car3 = 30;
int car4 = 40;
int car;
int days;
char FirstName(20);
char SurName (20);
char Street (40);
char City (20);
char Postcode (10);
char Phone (20);
char confirm;
int Confirm ()
{
if (confirm == 'Y' || confirm == 'y')
system("cls");
cout << "Please enter your Customer Information"<< endl;
cout << "First Name -";
cin >> FirstName;
cout << endl << "Surname -";
cin >> SurName;
cout << endl << "Street Address -";
cin >> Street;
cout << endl << "City -";
cin >> City;
cout << endl <<"Postcode -";
cin >> Postcode;
cout << endl << "Contact Number -";
cin >> Phone;
}
int main()
{
//Sets background colour to white and foreground colour to black
system("color f0");
SetConsoleTitle("Car Rental System");
cout << "Nolan Car Rentals" << endl << endl;
cout << "Please choose your vehicle:" << endl << endl;
cout << "1 - " << pound << car1 <<" - Volkswagon Polo or comparable car." <<endl<<endl;
cout << "2 - " << pound << car2 <<" - Volkswagon Jetta or comparable car." <<endl<<endl;
cout << "3 - " << pound << car3 <<" - Audi A4 or comparable car." <<endl<<endl;
cout << "4 - " << pound << car4 <<" - Mercedes E Class or comparable car." <<endl<<endl;
cin >> car;
cout << endl << "How many days do you wish to rent for?" <<endl<<endl;
cin >> days;
if (car == 1)
cout << "You chose a Volkswagon Polo or comparable car. For a length of " << days << " days will cost " << pound << car1*days << endl;
cout << "Do you wish to continue with this order? Y/N" << endl;
cin >> confirm;
elseif (car == 2)
cout << "You chose a Volkswagon Jetta or comparable car. For a length of " << days << " days will cost " << pound << car2*days << endl;
cout << "Do you wish to continue with this order? Y/N" << endl;
cin >> confirm;
elseif (car == 3)
cout << "You chose a Audi A4 or comparable car. For a length of " << days << " days will cost " << pound << car3*days << endl;
cout << "Do you wish to continue with this order? Y/N" << endl;
cin >> confirm;
elseif (car == 3)
cout << "You chose a Mercedes E Class or comparable car. For a length of " << days << " days will cost " << pound << car4*days << endl;
cout << "Do you wish to continue with this order? Y/N" << endl;
cin >> confirm;
Return 0;
}
By default only next line after if statement relates to it. That means that line 68 will be run regardless of car value.
So in line 72 there isn't any related ifs.
You need to encase block of execution in curly brackets:
64 65 66 67 68 69 70 71 72
if (car == 1)
{
cout << "You chose a Volkswagon Polo or comparable car. For a length of " << days << " days will cost " << pound << car1*days << endl;
cout << "Do you wish to continue with this order? Y/N" << endl;
cin >> confirm;
}elseif (car == 2)
I fixed that part and i called the Confirm function but when i'm entering details, im prompted to enter first name which i type then press enter, but then the surname, street address and city all are asked together rather than one at a time. Why would this happen?
if (car == 1)
{
cout << "You chose a Volkswagon Polo or comparable car. For a length of " << days << " days will cost " << pound << car1*days << endl;
cout << "Do you wish to continue with this order? Y/N" << endl;
cin >> confirm;
}
elseif(car == 2)
{
cout << "You chose a Volkswagon Jetta or comparable car. For a length of " << days << " days will cost " << pound << car2*days << endl;
cout << "Do you wish to continue with this order? Y/N" << endl;
cin >> confirm;
}
elseif (car == 3)
{
cout << "You chose a Audi A4 or comparable car. For a length of " << days << " days will cost " << pound << car3*days << endl;
cout << "Do you wish to continue with this order? Y/N" << endl;
cin >> confirm;
}
elseif(car == 4)
{
cout << "You chose a Mercedes E Class or comparable car. For a length of " << days << " days will cost " << pound << car4*days << endl;
cout << "Do you wish to continue with this order? Y/N" << endl;
cin >> confirm;
}
else{
}
Confirm();
}
First name is a single char initializated with value of 20 (space).
You need square brackets to denote array. But I would advise you to use std::string instead of char arrays.
Thanks, I've made the changes.
If I do street address as a cin >> Street; then it skips the next cin's according to how many spaces I used. I tried to use getline(cin,Street); but it isn't working how i want it to. Its skipping over the street cin and going to city part.
If you use std::cin>>some_string; it will only read the first word. If you want to read the whole line, you will need to use something like getline: std::getline(std::cin, some_string);
Now enter a single whitespace at the end of previous input before pressing enter.
If you read article you will notice that it is an unreliable solution. Actual working solution are at the bottom under SOLUTION header.
I posted a preferable solution. Second possibility is std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n')
BTW I noticed you guys keeps using
It tells compiler that those names should be in namespace std. You do not have problem with it because of usingnamespace std; line. But this line is notorious for creating hard to find bugs in code which is more complex than a simple student assigment and generally thought as bad habit.
I read the solution but it seems a bit more than my brain can take right now! At least it works with the way I have it now. Though you say I need to leave a space after my input before I press enter, I tried it with and without leaving a space and there seems to be no difference.
I inserted a loop for the main part of my program which seems to work fine. Now to display all the information onto an invoice or a something that at least looks like one.