Generate random numbers and create function with more than 1 return

So i want to create a function that generates 2 random numbers and adds them together. And also return more than one value(as in return the number generated and the answer).

Soo my returned numbers generated isn't between my defined limits and the returned sum is always equal to zero. Help pls

# include <iostream> // using the iostream header file
# include <ctime> // using the ctime header file to help start the random number generator
# include <cstdlib> // using the cstdlib header file
# include <string>
using namespace std; // use the namespace library in order to use cout, cin etc

static const string correct[] = {
"Very good",
"Exellent",
"Nice work",
"Keep up the good work!"};

static const string wrong[] = {
"No. Please try again",
"Wrong. Try once more",
"Don't give up!",
"No. Keep trying"
};

int strlenght = 3;

struct question {
int randomm[2];
int answer;
};

question numbergenerator(){
int ans = 0, i, rand_1[2]; // variable declaration and assignment
srand( time(0)); // rand will be triggered by time to generate numbers
for (i = 0; i < 2; i++){ // loop with conditions
rand_1[i] = rand()%9+0; // generates random numbers from 0 to 9
ans += rand_1[i]; // adds together the generated number and assign to ans
}
return question{rand_1[i], ans};

}

string correct_1(){
return correct[rand()%strlenght];
}

string wrong_1(){
return wrong[rand()%strlenght];
}

int main(){ // the main function
int num;
question q = numbergenerator();



cout<< "What is "<<q.randomm[0]<< "+"<<q.randomm[1]<<"?"<<endl; // display message
cin>> num; // takes users input

cout << "ans is"<< q.answer <<endl;

string v = correct_1();
string b = wrong_1();
/*
if (num == q.answer){
cout<< v << endl;
} else {
cout<< b <<endl;
}
*/
return 0;
}



Last edited on
warning: missing initializer for member ‘question::answer’ [-Wmissing-field-initializers]
||   return question{rand_1[i], ans};
||                                 ^

Also, there `i' is 2, out of bounds.


> rand_1[i] = rand()%9+0; // generates random numbers from 0 to 9
0 to 8
vector<int> yourfunc() //function that returns more than 1 integer, in a vector
{
vector<int> ret;
ret.pushback(rand()%9); //insert random numbers
ret.pushback(rand()%9);
ret.pushback(ret[0]+ret[1]); //insert sum
return ret;
}

Topic archived. No new replies allowed.