I am trying to learn C++, like so many people on this forum, and have been told that some of the best help on the internet is here, and I am definitely in need of some guidance. Below, I have pasted the code I am having trouble running. It is a program I copied from my text book for the sake of altering it as per one of the assignments. The program is supposed to display random addition problems and then compare the user answer to the correct answer, displaying the appropriate message. Only problem is, the value-returning function keeps giving a somewhat ambiguous error. The opening bracket is underlined, and when you hover over the symbol, the error reads "expected a ';' ". It also says something about it has a { that has not been matched (close to word for word)in the output console. In case it helps, I am running Visual Studio 2010, as that is the best full version I have access to right now. Can someone please give me some advice as to what I'm not seeing here? I greatly appreciate the help.
#include <iostream>
#include <ctime>
usingnamespace std;
//function prototype
int getRandomNumber(int lower, int upper);
int main()
{
//declare variables
int smallest = 0;
int largest = 0;
int num1 = 0;
int num2 = 0;
int correctAnswer = 0;
int userAnswer = 0;
//initialize rand function
srand(static_cast<int>(time(0)));
cout << "Smallest integer: ";
cin >> smallest;
cout << "Largest integer: ";
cin >> largest;
cout << endl;
for (int x = 1; x< 6; x += 1)
{
//generate two random integers
//from smallest to largest, then
//calculate the sum
num1 = getRandomNumber(smallest, largest);
num2 = getRandomNumber(smallest, largest);
correctAnswer = num1 + num2;
//display addition problem and get user;s answer
cout << "What is the sum of " << num1
<< "+" << num2 << "?";
cin >> userAnswer;
//determine whether user's answer is correct
if (userAnswer == correctAnswer)
cout << "Correct!";
else
cout << "Sorry, the correct answer is "
<< correctAnswer << ".";
//end if
cout << endl << endl;
} //end for
system("pause");
return 0;
//end of main function
//********function definitions********
int getRandomNumber(int lower, int upper)
{
int randInteger = 0;
//generate random integer from lower through upper
randInteger = lower + rand() % (upper - lower + 1);
return getRandomNumber;
}
#include <iostream>
#include <ctime>
usingnamespace std;
//function prototype
int getRandomNumber(int lower, int upper);
int main()
{
//declare variables
int smallest = 0;
int largest = 0;
int num1 = 0;
int num2 = 0;
int correctAnswer = 0;
int userAnswer = 0;
//initialize rand function
srand(static_cast<int>(time(0)));
cout << "Smallest integer: ";
cin >> smallest;
cout << "Largest integer: ";
cin >> largest;
cout << endl;
for (int x = 1; x < 6; x += 1)
{
//generate two random integers
//from smallest to largest, then
//calculate the sum
num1 = getRandomNumber(smallest, largest);
num2 = getRandomNumber(smallest, largest);
correctAnswer = num1 + num2;
//display addition problem and get user;s answer
cout << "What is the sum of " << num1
<< "+" << num2 << "?";
cin >> userAnswer;
//determine whether user's answer is correct
if (userAnswer == correctAnswer)
cout << "Correct!";
else
cout << "Sorry, the correct answer is "
<< correctAnswer << ".";
//end if
cout << endl << endl;
} //end for
system("pause");
return 0;
//end of main function
}
//********function definitions********
int getRandomNumber(int lower, int upper)
{
int randInteger = 0;
//generate random integer from lower through upper
randInteger = lower + rand() % (upper - lower + 1);
return randInteger;
}
Wow....thank you so much for that Softrix. Like I said from the beginning, I am obviously an amateur at this, and appreciate the guidance. It suddenly seems so clear. Value-returning functions is a new topic for me, but I feel like I have a new level of understanding how they work. Again, thank you so much for the help. I hope to see you around.