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;
}
#include <iostream>
#include <iomanip> // you haven't used any fuction from this lib in your code ...delete it
usingnamespace 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
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 !
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.