Write a program that will ask the user a question with four possible answers. The question should be asked 20 times. After all the input is gathered, the program should output the number of times each answer was selected.
Solution:
#include <iostream.h>
#include <conio.h>
int main ()
{
clrscr();
int Answer1 = 0 , Answer2 = 0, Answer3 = 0, Answer4 = 0l ;
int Counter;
int Value;
for (Counter = 0; Counter < 20 ; Counter++)
{
cout<< "Enter either 1,2,3 or 4 " <<endl;
cin>> Value;
switch (Value)
{
case 1:
Answer1++;
break;
case 2:
Answer2++;
break;
case 3:
Answer3++;
break;
case 4:
Answer4++;
break;
default:
cout<< "Incorrect choice" ;
Counter--;
}
}
cout<< "Number of Answers 1's = " << Answer1<<endl;
cout<< "Number of Answers 2's = "<< Answer2<<endl;
cout<< "Number of Answers 3's = "<< Answer3<<endl;
cout<< "Number of Answers 4's = " << Answer4<<endl;
getch();
}
#include <conio.h>
int main ()
{
clrscr();
int Answer1 = 0 , Answer2 = 0, Answer3 = 0, Answer4 = 0l ;
int Counter;
int Value;
for (Counter = 0; Counter < 20 ; Counter++)
{
cout<< "Enter either 1,2,3 or 4 " <<endl;
cin>> Value;
switch (Value)
{
case 1:
Answer1++;
break;
case 2:
Answer2++;
break;
case 3:
Answer3++;
break;
case 4:
Answer4++;
break;
default:
cout<< "Incorrect choice" ;
Counter--;
}
}
cout<< "Number of Answers 1's = " << Answer1<<endl;
cout<< "Number of Answers 2's = "<< Answer2<<endl;
cout<< "Number of Answers 3's = "<< Answer3<<endl;
cout<< "Number of Answers 4's = " << Answer4<<endl;
getch();
}
No comments:
Post a Comment
If you have any doubt please let me know