samedi 12 juin 2021

Trying to use the rand() function in C++

so I'm supposed to make a math quiz that would convert inches to feet and inches, and ask the user how many questions they would like. Then generate random heights no greater than 72 inches to be converted into feet and inches. I'm not sure how to make the options random as now the correct answers are all "a" option while the other 'b', 'c' options create incorrect heights. I want to them to not just be 'a' as the correct answer but i'm not sure how do i randomize that? and I'm not sure how to decide the correct answer position as all the correct answers are "a" in my code. This is what I have so far.

int inches = 0;
    int feet = 0;
    int leftinches = 0;
    int questions;
    char options;
    int total;
    //Convert inches into feet and inches
    feet = inches/12;
    leftinches = inches % 12;
    //Random generator seed
    srand(time(0));
    
    cout<<"Welcome to the distance conversion test."<<endl;
    while (true){
        cout <<"How many questions would you like?(Enter 0 to quit)"<<endl;
        cin >> questions;
        if(questions==0){
            cout<<"Bye!";
            break;
        }
        for (int i{}; i<questions; ++i){
            inches = rand() % 72 + 1;
            cout << "Which distance is the same as:"<<inches<<'\"';
            feet = inches/12;
            leftinches = inches % 12;
            cout<<" ""a)"<< feet<<"\'"<<leftinches<<'\"';
            inches = rand() % 72 + 1;
            feet = inches/12;
            leftinches = inches % 12;
            cout<<" ""b)"<<feet<<"\'"<<leftinches<<'\"';
            inches = rand() % 72 + 1;
            feet = inches/12;
            leftinches = inches % 12;
            cout<<" ""c)"<<feet<<"\'"<<leftinches<<'\"';
            cout <<" ""Select(a, b, c, q=quit)";
            options = rand();
            cin>>options;
            cout<<endl;
        }
    }
    return 0;
} 



Aucun commentaire:

Enregistrer un commentaire