Issues with 'if-else loop..

I am working on a project(again) and am running into an issue with this code. I am trying to choose a "plan" and my cin works to type in a plan..but then repeats the "Please enter the number of prescriptions" part several times and the other cin won't allow me to enter anything.
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
#include <iostream>
#include <cmath>
using namespace std;

main() {
int planA;
int planB;
int planC;
int userChoice;
int userMeds;



cout << "Plan A: ";
cout << endl;
cout << "  Monthly premium: $10.00";
cout << endl;
cout << "  Deductible $200.00";
cout << endl;
cout << "  Copay 40%";
cout << endl;

cout << "Plan B:";
cout << endl;
cout << "  Monthly premium $25.00";
cout << endl;
cout << "  Deductible $100.00";
cout << endl;
cout << "  Copay 20%";
cout << endl;

cout << "Plan C:";
cout << endl;
cout << "  Monthly premium $50.00";
cout << endl;
cout << "  Deductible $0.00";
cout << endl;
cout << "  Copay 0%";
cout << endl;
cout << endl;

cout<< "Please select a plan (enter A, B, or C): ";
cin >> userChoice;

if ((userChoice == 'A')||(userChoice == 'a'));{
   cout << "Please enter the estimated number of prescriptions (0 to 96): ";
   cin >> userMeds;
  
   if ((userChoice == 'B')||(userChoice == 'b')); {
   cout << "Please enter the estimated number of prescriptions (0 to 96): ";
   cin >> userMeds;
   
   if ((userChoice == 'C')||(userChoice == 'c'));{
   cout<< "Please enter the estimated number of presciptions (0 to 96): ";
   cin >> userMeds;
   cout << userMeds;
   }
   }
   
   }   
  

return 0;
}
Lines 45,49,53: You have a semicolon after the condition. This terminates the if statements.
1
2
int userChoice;
(userChoice == 'A')

int is short for integer
userChoice is of type int. Hence cin >> userChoice will only accept an integer value. A B C are not integer values hence the >> fails, puts the input stream into fail mode so that subsequent cin >> statements also fail. Hence multiple output displays before the program terminates. Make userChoice type char.

L9
 
char userChoice {};


Also

L5 main has a return type of int

L6 - 10 - variables should be initialised when defined.

Why L2? The code as provided doesn't need this.
Lines 45,49,53: You have a semicolon after the condition. This terminates the if statements.

Lets replace that ';' with '{}' and refine indentation to be more expressive:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
if ((userChoice == 'A')||(userChoice == 'a')) {
}

{
   cout << "Please enter the estimated number of prescriptions (0 to 96): ";
   cin >> userMeds;
  
   if ((userChoice == 'B')||(userChoice == 'b')) {
   }

   {
      cout << "Please enter the estimated number of prescriptions (0 to 96): ";
      cin >> userMeds;
   
      if ((userChoice == 'C')||(userChoice == 'c')) {
      }

      {
         cout<< "Please enter the estimated number of presciptions (0 to 96): ";
         cin >> userMeds;
         cout << userMeds;
      }
   }
}

That does look suspicious. Now, without the ; / {}:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if ((userChoice == 'A')||(userChoice == 'a')) {
   cout << "Please enter the estimated number of prescriptions (0 to 96): ";
   cin >> userMeds;
  
   if ((userChoice == 'B')||(userChoice == 'b')) {
      cout << "Please enter the estimated number of prescriptions (0 to 96): ";
      cin >> userMeds;
   
      if ((userChoice == 'C')||(userChoice == 'c')) {
         cout<< "Please enter the estimated number of presciptions (0 to 96): ";
         cin >> userMeds;
         cout << userMeds;
      }
   }
}

If userChoice is neither 'A' nor 'a', then the inner IFs are never evaluated.
If userChoice is 'A' or 'a', then the conditions in inner IFs are always false.
As the same prompt/input is required for each plan, why not as a starter:

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
#include <iostream>

int main() {
	char userChoice {};
	int userMeds {};

	std::cout << "Plan A: \n";
	std::cout << "  Monthly premium: $10.00\n";
	std::cout << "  Deductible $200.00\n";
	std::cout << "  Copay 40%\n";

	std::cout << "Plan B:\n";
	std::cout << "  Monthly premium $25.00\n";
	std::cout << "  Deductible $100.00\n";
	std::cout << "  Copay 20%\n";

	std::cout << "Plan C:\n";
	std::cout << "  Monthly premium $50.00\n";
	std::cout << "  Deductible $0.00\n";
	std::cout << "  Copay 0%\n";

	std::cout << "\nPlease select a plan (enter A, B, or C): ";
	std::cin >> userChoice;

	std::cout << "Please enter the estimated number of prescriptions (0 to 96): ";
	std::cin >> userMeds;

	if ((userChoice == 'A') || (userChoice == 'a')) {
		// Process A here
	} else if ((userChoice == 'B') || (userChoice == 'b')) {
		// Process B here
	} else if ((userChoice == 'C') || (userChoice == 'c')) {
		// Process C here
	} else
		std::cout << "Invalid plan\n";
}

Topic archived. No new replies allowed.