My code is supposed to guess a number the user thought of that is between 0 and 100. It generates a different number each time, and prompts the user if it is the right number. The code compiles with no errors, except two ones I know nothing about. It uses a while loop to repeat the process.
Here's my code:
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <unistd.h>
#include <string>
usingnamespace std;
int main() {
bool repeat = false;
bool guessedright = false;
cout << "Please think of a random number between 0 and 100" << endl;
sleep(1);
cout << "Did you finish thinking? Press the return key when you are ready";
cin.get();
while (!guessedright)
{
int rand_lim(int limit) {
int divisor = RAND_MAX/(limit+1);
int retval;
do {
retval = rand() / divisor;
} while (retval > limit);
return retval;
}
int random_num = rand_lim(100);
string current;
current = random_num;
string num_try;
num_try += random_num + " ";
if (num_try.find(current) != std::string::npos)
{
repeat = true;
}
if (repeat)
{
continue;
}
else
{
char a;
cout << endl << "My guess is " << random_num
<< ". If it is right type \'y\' or \'n\' "
<< "if it is wrong: " << endl;
cin >> a;
if (a=='y' || a=='Y')
{
guessedright = true;
}
elseif (a=='n' || a=='N')
{
continue;
}
else
{
cout << "WHAT was that?? I will take it as wrong anyway!" << endl;
guessedright = false;
continue;
}
}
cout << "Yeah! Yeah babe... I WIN!";
return 0;
}
}
and here is the error KDevelop and g++ give me:
main.cpp: In function ‘int main()’:
main.cpp:18:29: error: a function-definition is not allowed here before ‘{’ token
int rand_lim(int limit) {
^
main.cpp:63:1: error: expected ‘}’ at end of input
}
^
main.cpp:63:1: error: expected ‘}’ at end of input
The strange thing is, even when I add two '}''s to the end of the code it gives the same error. I am still learning C++, and I don't understand what's wrong with the first error. I appreciate your sacrificing of time for me.