Random Array

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>
using namespace 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.
1
2
3
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
Array of strings looks like this:
1
2
3
4
5
string str[] = {
    "string one", 
    "string two",
    "string three"
};
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.
Thanks, but sorry I cant still correct my code. Can you show an example by editing my original code?
Not sure how you want to compare but if you take what hamsterman and i said into account it should work.

You need to replace the following:
int measurement [] = {mass, time, momentum}; with string measurement [] = {"mass", "time", "momentum"};.

cout << // ex. "What is Mass?" // I want this line to choose randomly from those arrays above
with
1
2
int rand_index = rand() % measurement.size();
cout << "What is " << measurement[rand_index] << "?";


You'll have to clearly explain how you want to compare the user answer for me to be able to say anything on the comparison method
Last edited on
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.

Thanks hope you can complete this code.
Sorry i can't complete the code. I'll let you do it because you'll learn better that way :p. I will tell you, however, that you want to put what the user enters into a string variable not an int and compare that with what the answer should be.
These should help:
http://www.cplusplus.com/reference/iostream/istream/operator%3E%3E/
http://www.cplusplus.com/reference/string/string/

Also what hamsterman said would be very helpful.

It's really important that you understand what you need to do.
Last edited on
Sure. I think I should check the links you gave to me. I have to start again and study for now. Cheers.
Topic archived. No new replies allowed.