If-Else statements and Executive Brackets

I'm a newb, trying to use if-else statements and brackets. I keep getting this error: sstream.cpp:45 error: expected primary-expression before "else"
" " expected ';' before "else"

Pretty sure it's got something to do with the brackets, but I don't know what's going wrong. Any help is greatly appreciated. Line with the error code (45) is designated by astericks.

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
// stringstream
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main ()
{
	string mystr, mystr2, mystr3, mystr4, mystr5;
	float price=0;
	int quantity=0;
	
	cout << "Welcome to Bob's Generic Store! \n";
	cout << "Enter price: ";
	getline (cin, mystr);
	stringstream(mystr) >> price;
	cout << "Enter quantity: ";
	getline (cin, mystr);
	stringstream(mystr) >> quantity;
	cout << "Total price: " << price*quantity << endl;
	cout << "\n";
	
	cout << "How would you like to pay for this purchase?\n";
	getline (cin, mystr2);
	if (mystr2 == "credit" ) 
	 cout << "You chose credit!\n";
	else if (mystr2 == "cash" ) 
	 cout << "You chose cash!\n";
	else
	 cout << "Please specify your payment type!\n";
	
	if (mystr2 == "credit" )
	 cout << "Please insert your pin: ";
	 getline (cin, mystr3);

	 if (mystr3 == "0000") {
	  cout << "Pin entered successfully! Processing... \n";
	  cout << price*quantity;
	  cout << "Is this correct? \n";
	  getline (cin, mystr4);
	  if (mystr4 == "yes" ) {
	   cout << "Please enter your name to verify purchase \n";
       getline (cin, mystr5);
       cout << "Thank you for shopping at Bob's. Have a nice day!";
**    else (mystr4 == "no")
       cout << "Please reenter your pin!";
       }	   
	 else 
      cout << "Please enter your pin again! \n";
      }
	
}

Last edited on
you forgot the closing } at the end of line 44.
Didn't work.

Messed with the code a bit more, ended up getting the following error.

$ g++ sstream.cpp -o sstream.exe
sstream.cpp: In function `int main()':
sstream.cpp:43: error: expected primary-expression before "else"
sstream.cpp:43: error: expected `;' before "else"

Adding a closing } to line 45 with the else statement gives me 7 errors, all saying the same expected ';' before "else" at a bunch of different lines where I already have closing and opening brackets. Anyone got any ideas? Starting to slam my head against the wall here...

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
// stringstream
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main ()
{
	string mystr, mystr2, mystr3, mystr4, mystr5;
	float price=0;
	int quantity=0;
	
	cout << "Welcome to Bob's Generic Store! \n";
	cout << "Enter price: ";
	getline (cin, mystr);
	stringstream(mystr) >> price;
	cout << "Enter quantity: ";
	getline (cin, mystr);
	stringstream(mystr) >> quantity;
	cout << "Total price: " << price*quantity << endl;
	cout << "\n";
	
	cout << "How would you like to pay for this purchase?\n";
	getline (cin, mystr2);
	if (mystr2 == "credit" ) 
	 cout << "You chose credit!\n";
	else if (mystr2 == "cash" ) 
	 cout << "You chose cash!\n";
	else
	 cout << "Please specify your payment type!\n";
	
	 if (mystr2 == "credit" ) 
	 {
	 cout << "Please insert your pin: ";
	 getline (cin, mystr3);
     }
	  if (mystr3 == "0000") 
	 {
	  cout << "Pin entered successfully! Processing... \n";
	  cout << price*quantity;
	  cout << "Is this correct? \n";
	  getline (cin, mystr4);
	  else (mystr3 == "1111") {
	  cout << "Pin incorrectly entered.";

	   if (mystr4 == "yes" ) 
	  {
	   cout << "Please enter your name to verify purchase \n";
       getline (cin, mystr5);
       cout << "Thank you " << mystr5 << "for shopping at Bob's. Have a nice day!";
	   }
       else (mystr4 == "no"){ 
       cout << "Please reenter your pin!";
       }
	 else (mystr2 == "cash" ) {
	  cout << "Please insert cash and have a good day!";
	  }
	return 0;
    }


}

}



Line 42: insert } before else
Line 43: either use else if or delete (mystr3 == "1111")
Line 52, 55: same as above.
Line 64: you don't need that bracket
Last edited on
else if didn't work, but deleting the whole statement and rewriting it as a straight up if statement did. Weird stuff, but thanks for the help! Why did you say else if though?

Also, how could I make it so that any pin used other than 1111 would result in incorrect pin?

Thanks!!!
Last edited on
1
2
3
4
if (pin == "1111")
    std::cout << "correct" << std::endl;
else
    std::cout << "incorrect" << std::endl;
Topic archived. No new replies allowed.