Question #39293

write a program in C++ that will ask 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.?

Expert's answer

Answer on Question#39293 – Programming - C++


#include <iostream>
#include <stdlib.h>
#include <vector>
using namespace std;
#define TIMES 20
int main()
{
    char var;
    vector<int> variants(4);
    for(int i = 0; i < TIMES; ++i)
    {
        cout << "What type of variable does take four bytes in memory?" << endl;
        cout << "\tA: char" << endl;
        cout << "\tB: double" << endl;
        cout << "\tC: int" << endl;
        cout << "\tD: long long int" << endl;
        cin >> var;
        switch(tolower(var))
        {
            case 'a':
                ++variants[0];
                break;
            case 'b':
                ++variants[1];
                break;
            case 'c':
                ++variants[2];
                break;
            case 'd':
                ++variants[3];
                break;
            default:
                cout << "You've inputed wrong variant of answer. Try again." << endl;
                --i;
        }
    }
    cout << "\nVariant A was chosen " << variants[0] << " times." << endl;
    cout << "\nVariant B was chosen " << variants[1] << " times." << endl;
    cout << "\nVariant C was chosen " << variants[2] << " times." << endl;
    cout << "\nVariant D was chosen " << variants[3] << " times.\n" << endl;
    system("PAUSE");
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS