How to take char and int simultaneously from user

Dear all,
hope you are doing good. I am new user of this fancy site c++.
actually I am a new learner of c++, I am trying to right codes for an assignment but I have stuck how to take simultaneously a char from user and then an integer. may anyone please help me out. below is my code for my assignment.

this assignment is to interact with user ask to select a char from (list) then user inputs some integers then selects (S) to show the sum of the numbers and (A) to average and so on so forth.
your assistance will be highly appreciated.

Best regards,
Fahim


#include <iostream>
#include <iomanip>

using namespace std;


int main()
{
int size=100; //Array size
int array[size]; //Declaring array
double Aray[100];
int N, B, F, H, M, Q, S, A, x, choice;
char command;
double sum=0;
double average=0;
int smallest=0;
int largest=0;
int temp = 0;


cout << "Welcome to Stats Array!"<<endl<<endl<<"These are your choices:"<<endl<<endl<<"N- Numbers"<<endl<<"S- Sum of all"<< endl<<"A- Average of all"<<endl<<"B- Biggest of all"<<endl<<"F- Most Frequent of all"<<endl<<"H- How many numbers"<<endl
<<"M- Median of all"<<endl<<"Q- Quit" <<endl;

cout<<endl<<"Please enter your choice:"<<endl;
cin>>choice;

choice=command;
switch(choice)
{
case 1:
cin>>command;
break;
case 2:
cin>>choice;
break;
}


if(choice==N)
for(int i=0;i<size;i++)
{
cout<<"Enter element number "<<i+1<<endl;
cin>>array[i];
cout<<"OK"<<endl;
}

if(choice==S)
for(int i=0;i<size;i++)
{
sum=sum+array[i];
cout<<sum<<endl;
}
if(choice==A)
for(int i=0;i<size;i++)
{
cout<<"Average of elements is "<<sum/100;
}


return 0;
}
Last edited on
You should have post this in the beginner Forum. See comments in code.
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
#include <iostream>
#include <iomanip> // you haven't used any fuction from this lib in your code ...delete it

using namespace std;


int main()
{
	int size = 100; //Array size    // you should delete this 
	int array[size]; //Declaring array  // you declare array size when making arrays ... "int array[100];"
	double Aray[100]; //don't know what this variable purpose is
	int N, B, F, H, M, Q, S, A, x, choice;  // You do not have to put initialze the choice all you need is "char choice;"
	char command;  // don't know what this variable purpose is
	double sum = 0;  
	double average = 0;
	int smallest = 0;  //use double all throught out this program 
	int largest = 0;
	int temp = 0;


	cout << "Welcome to Stats Array!" << endl << endl << "These are your choices:" << endl << endl << "N- Numbers" << endl << "S- Sum of all" << endl 
		<< "A- Average of all" << endl << "B- Biggest of all" << endl << "F- Most Frequent of all" << endl << "H- How many numbers" << endl
		<< "M- Median of all" << endl << "Q- Quit" << endl;

	cout << endl << "Please enter your choice:" << endl;
	cin >> choice;

	choice = command; //delete this line . lets say that choice = H and command = N.. you are saying that choice = N. you override the user input
	switch (choice)
	{
	case 1: //you choices are char ... case 'N': etc...
		cin >> command;
		break;
	case 2:
		cin >> choice;
		break;
	}

	//Your if statements should go in to the case statments. 
	/*case 'N':
		cout << "Enter element number " << i + 1 << endl;
		cin >> array[i];
		cout << "OK" << endl;
		break;
	*/
	//you can create a while loop to do the increments

	if (choice == N)
		for (int i = 0; i<size; i++)
		{
			cout << "Enter element number " << i + 1 << endl;
			cin >> array[i];
			cout << "OK" << endl;
		}

	if (choice == S)
		for (int i = 0; i<size; i++)
		{
			sum = sum + array[i];
			cout << sum << endl;
		}
	if (choice == A)
		for (int i = 0; i<size; i++)
		{
			cout << "Average of array elements is " << sum / 100;
		}


	return 0;
}

dear tibrado, thank you very much for your time to reply but I implemented all your suggestion, I didn't get the result, actually gives error. what I am looking is looks like following, its an interaction between the computer and user.

Welcome to Stats Array!

These are your choices:

N- Numbers

S- Sum of all

A- Average of all

B- Biggest of all

F- Most Frequent of all

H- How many numbers

M- Median of all

Q- Quit

Following is interaction between user and computer:

N 2
OK
N 2
OK
N 6
OK
N 5
OK
H
4
S
15
A
3.75
F
2
N 3
OK
M
3
Q
END
Last edited on
Here is an example of the direction I was trying to direct you.
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

#include <iostream>

using namespace std; 

int main() {

	int aArray[5];
	int sum;

	char choice;
	bool done = false; 

	do {
		cout << "Choices are A = input, B = sum, C = quit: ";
		cin >> choice;

		switch (choice)
		{
		case 'A':
		case 'a':
			for (int i = 0; i < 5; i++) {
				cout << "\nEnter Number: ";
				cin >> aArray[i];
			}
			break;
		case 'B':
		case 'b':
			sum = 0;
			for (int i = 0; i < 5; i++) {
				sum = sum + aArray[i];
			}
			cout << sum << endl; 
			break;
		case 'C':
		case 'c':
			done = true;
			break;
		default:
			cout << "Invalid choice End of Programm";
			break;
		}

	} while (!done);
}
tibrado once again thank you for your time, but unfortunately above recent code when the you user enters some integers then press B to sum those integers, the program loops like never ends !
Last edited on
What do you mean? You only quit the loop by inputting 'C'. Unless you did something to the above code it should work fine.
tibrado bro, I didn't do anything, I just copied from here and pasted in my compiler. it takes integers as expected but when you press B to give you sum of those integers it goes to infinite loop.
I think I know what might be the problem.
- Are you entering a characters after you give an input of "A"?
- The first 5 input should only be numbers, until you are prompted to input something else.
- Follow the messages and place Numbers when asked for a number.
Dear tibrado, thanks alot for your help, it's really helpful and understandable. but is it possible to do it the way it's asked in assignment, because they have given below hint, that how to cope with input variable int and char:

HINTS:

Read the command characters with an input statement like this (where command is a char variable):

cin >> command;

Use a switch statement to control the action of the program based on the command character that has been read. If the command character was an N, then read the number with an input statement like this (where number is an integer variable):

cin >> number;

Don't forget the break statement at the end of each case.
You are pretty much asking me to give you the answer. I gave you a really good shell to work with. I expected you to use it as an example / different way to approach your problem. You should be able to tweak it into what you want.
Last edited on
I know bro you helped alot, thank you for everything you did. I got at least the idea how to rectify the errors. thank you once again bro.
Try running this code.
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
#include <iostream>

using namespace std; 

int main() {

	int aArray[5];
	int sum;
	int i = 0;

	char choice;
	bool done = false; 

	do {
		cout << "\nChoices are A = input, B = sum, C = quit: ";
		cin >> choice;

		switch (choice)
		{
		case 'A':
		case 'a':
				cout << "\nEnter Number: ";
				cin >> aArray[i];
				i++;
			
			break;
		case 'B':
		case 'b':
			sum = 0;
			for (int j = 0; j < i; j++) {
				sum = sum + aArray[j];
			}
			cout << sum << endl; 
			break;
		case 'C':
		case 'c':
			done = true;
			break;
		default:
			cout << "Invalid choice End of Programm";
			break;
		}

	} while (!done);
}
tibrado Bro, I don't wanna give you headache anymore,
to be honest above code does not take integer at all !
it's because you have to take both char and int simultaneously from user
Topic archived. No new replies allowed.