Else if and functions.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  #include <iostream>
#include <windows.h> //Allows me to enter headers
#define pound char(156)
using namespace 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;


else if (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;


else if (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;


else if (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;

}
else if (car == 2)
etc.
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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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;

}
else if(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;

}
else if  (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;

}
else if(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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
string FirstName;
string SurName;
string Street;
string City;
string Postcode;
string Phone;
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 -";
getline(cin,Street);


cout << endl << "City -";
cin >> City;


cout << endl <<"Postcode -";
cin >> Postcode;


cout << endl << "Contact Number -";
cin >> Phone;



}
Last edited on
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);
std::getline(std::cin >> std::ws, Street);
Read this: http://stackoverflow.com/a/21567292/3410396
Ok i read that link and tried it like this.
1
2
3
4
5
6
7
8
9
int Confirm ()
{
if (confirm == 'Y' || confirm == 'y')
system("cls");

cout << "Please enter your Customer Information"<< endl<< endl;
cin.ignore();
cout << "First Name - ";
getline(cin, FirstName);


The line cin.ignore(); seemed to do the trick.

BTW I noticed you guys keeps using
std:
and
std::
I haven't ever used that and it hasn't been a problem. Whats it for?
The line cin.ignore(); seemed to do the trick.
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 using namespace 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.
Last edited on
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.

My code so far
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <iostream>
#include <windows.h> //Allows me to enter headers
#define pound char(156)
using namespace std;

int car1 = 10;
int car2 = 20;
int car3 = 30;
int car4 = 40;
int car;
int days;
string FirstName;
string SurName;
string Street;
string City;
string Postcode;
int Phone;
char confirm;

int Confirm ();
int Invoice ();
int main()
{
//Sets background colour to white and foreground colour to black
system("color f0");

SetConsoleTitle("Car Rental System");

do
{
system("cls");
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;

}
else if(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;

}
else if  (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;

}
else if(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;

}


}
while (confirm =='n' || confirm == 'N');

Confirm();
Invoice();
}



int Confirm ()
{
if (confirm == 'Y' || confirm == 'y')
system("cls");

cout << "Please enter your Customer Information"<< endl<< endl;
cin.ignore();
cout << "First Name - ";
getline(cin, FirstName);


cout << endl << "Surname - ";
getline(cin, SurName);


cout << endl << "Street Address - ";
getline(cin, Street);


cout << endl << "City - ";
getline(cin, City);


cout << endl <<"Postcode - ";
getline(cin, Postcode);


cout << endl << "Contact Number - ";
cin >> Phone;

cin.get();

}

int Invoice ()
system("cls");

cout << "First Name			: " << FirstName << endl;
cout << "Surname				: " << SurName << endl;
cout << "Street Address			: " << Street << endl;
cout << "City				: " << City << endl;
cout << "Postcode			: " << Postcode << endl;
cout << "Contact Number			: " << Phone << endl;
if (car == 1)
{
cout << "Car				: Volkswagon Polo or comparable car" << endl;

cout << "Cost				: "<< pound << car1*days;

}
if (car == 2)
{
cout << "Car				: Volkswagon Jetta or comparable car" << endl;

cout << "Cost				: "<< pound << car2*days;

}
if (car == 3)
{
cout << "Car				: Audi A4 or comparable car" << endl;

cout << "Cost				: "<< pound << car3*days;

}
if (car == 4)
{
cout << "Car				: Mercedes E Class or comparable car" << endl;

cout << "Cost				: "<< pound << car4*days;

}
}
Last edited on
Topic archived. No new replies allowed.