Function not working...returning ambiguous error.

http://www.cplusplus.com/ico/forum_question.png

Hello everyone,

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
  #include <iostream>
#include <ctime>
using namespace 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;
	}

Your missing a curly bracket on line 55.
You are attempting to return the functions name on line 62, this should be return randInteger;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

#include <iostream>
#include <ctime>
using namespace 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.

Your welcome :)
Topic archived. No new replies allowed.