I'm writing a program that will show a unit and the user must confirm if its right or wrong.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
usingnamespace std;
int measurement [] = {mass, time, momentum};
int n, scalar, vector, result=0;
int main ()
{
cout << // ex. "What is Mass?" // I want this line to choose randomly from those arrays above
cin n; // ex. scalar
// after the answer is entered, there must be a confirmation if its right or wrong.
return 0;
}
My two problems are:
1.I don't have any idea on how to choose randomly from array of strings.
2.I dont know how to make the confirmation
I'm planning to add additional units aside from above after you help me to solve my problem. Thanks.
srand(time(NULL)); //Makes sure random is actually random between runs
int rand_index = rand() % measurement.size();
cout << "What is " << measurement[rand_index] << "?";
Use rand to get a random index into the array of strings and use the string at that index
You could use char* instead of string bet strings make stuff more simple. You'll have to include <string>
Generate random numbers like this: int r = rand()%3+2;//generates a random number between 2 to 4
You may have to include <cstdlib>.
You may also want to use sand: srand(time(0));
Do this once, at the beginning of your main(). You'll need to include <ctime>.
If you want to compare strings using c++ string library, usual opeartors work (==, !=, > ...). If you want to use char* then you'll need to use strcmp.
If you need more info about the functions I named, see reference on this site.
Ok It goes like this:
if the program ask about mass or time, the user must input "scalar" and the program will respond its right. If not, the reply is "Sorry its wrong." Its also the same with momentum except the correct answer is vector.