If statements?

Hi there,
Im building a very simple mock phone diagnostics tool and this is what I have so far.. For some reason my if statements aren't working like I want them to, when I enter 1 it works but when I enter 2 or 3 it doesn't display the cout it just moves to the first if statement...

What am I missing? (aside from a brain haha!)

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
#include <iostream>
#include <limits>
using namespace std;

int main()
{
	int a, b, c;
	a = 1;
	b = 2;
	c = 3;
	system("Color 7E");
	cout << "Easy Phone Diagnostic Tool" << endl;
	cout << "enter 1 to start diagnostics..." << endl;
	cout << "enter 2 to display phone info..." << endl;
	cout << "enter 3 to retry phone connection..." << endl;

	if (cin >> a)
	{
		cout << "Starting Diagnostics" << endl;
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}
	if (cin >> b)
	{
		cout << "Displaying Phone Info" << endl;
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}
	if (cin >> c)
	{
		cout << "Retrying Phone Connection" << endl;
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}




	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	return 0;
}
Try something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int input_from_keyboard;
cin >> input_from_keyboard;

if (input_from_keyboard == 1)
{
  // do something
}
else if (input_from_keyboard == 2)
{
  // do something
}
else if (input_from_keyboard == 3)
{
  // do something
}


There is no purpose in this code to your variables a, b, c
Thanks a lot!

creating variables and assigning a number was the only thing I could think of to accomplish what I wanted with my limited C++ knowledge. Thanks a lot! Really appreciate your time.

Last edited on
Topic archived. No new replies allowed.