0

I do not know how to declare "random" in the parentheses for "int main()," and need help. (I am a beginner in C++)

Please take a look at my code, try it out, and please notify me with an answer when you think you know how to solve this problem. It'd mean a lot to me. Thanks! Meanwhile, I will keep trying to solve the problem myself as well.

Note: I am using Code::Blocks if you want to be specific.

The error is on Line 7/9 of my code.

Here is my updated code below:

#include <iostream>
#include <stdlib.h>
#include <conio.h>

using namespace std;

int main()
{
int rn = random() % 21; // generates a random int from 0 to 20

// First output asking the user to guess the number
cout << "Please guess my number :" << endl;
int u;
cin >> u;

while (u != rn) // Calculates the answer that you give
{

// If the user's number is greater than the random number
// the program will let you know it's too large
if (u > rn)
{
    cout << "You guessed too big!" << endl;
}
// On the other hand, if the user guesses to small
// the program will tell them that it's too small
else if (u < rn)
{
    cout << "You guessed too small!" << endl;
}

// If the user does not get the right number, the program
// will tell the user to guess again
cout << "Please guess again :" << endl;
cin >> u;

}

// If the user guesses the number correctly, the program
// will say that they got it right, and end the program
cout << "You guessed it right!" << endl;
getch();
}

Here's the updated compiler error:

||=== Build: Debug in Guess The Number (compiler: GNU GCC Compiler) ===|

C:\Users\Minecraftship\Documents\CPP Programs From Book\Guess The Number\main.cpp||In function 'int main()':|

C:\Users\Minecraftship\Documents\CPP Programs From Book\Guess The Number\main.cpp|12|

error: 'randomize' was not declared in this scope|

||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

3 Answers3

10

Remove the semicolon near main, the compiler is telling you exactly what the issue is:

int main ();

Should be

int main ()

Your code will also not compile even after fixing this because you have not declared the std namespace. You can put this line at the top for now using namespace std; but it is bad practice. You should declare it manually using the scope resolution operator.

And a number of other issues as already mentioned in the comments above, make sure to read the compiler output thoroughly because it tells you what line is causing the issue.

Your code should look like:

#include <iostream>
#include <stdlib.h>
#include <conio.h>

using namespace std;

int main()
{
int rn = random() % 21; // generates a random int from 0 to 20

// First output asking the user to guess the number
cout << "Please guess my number :" << endl;
int u;
cin >> u;

while (u != rn) // Calculates the answer that you give
{

    // If the user's number is greater than the random number
    // the program will let you know it's too large
    if (u > rn)
    {
        cout << "You guessed too big!" << endl;
    }
    // On the other hand, if the user guesses to small
    // the program will tell them that it's too small
    else if (u < rn)
    {
        cout << "You guessed too small!" << endl;
    }

    // If the user does not get the right number, the program
    // will tell the user to guess again
    cout << "Please guess again :" << endl;
    cin >> u;

}

// If the user guesses the number correctly, the program
// will say that they got it right, and end the program
cout << "You guessed it right!" << endl;
getch();
}
Community
  • 1
  • 1
ifma
  • 3,673
  • 4
  • 26
  • 38
  • I have added the using namespace std;, and have tried lots of other fixes, but the error is still there for some reason... – Graphic Jet Jul 06 '16 at 20:49
  • Ugh. I'm still new to this place, and it says that I can't post another question for another day...any suggestions? – Graphic Jet Jul 06 '16 at 20:59
  • Sorry for the long wait, I've been busy. Yeah, I just pasted in the error from compiling the code into the question. It still doesn't want to work. – Graphic Jet Jul 07 '16 at 02:14
  • The code is now updated. I just removed a bunch of unneeded semi-colons, and put an '!' mark before the '=' in: while ( u != rn). – Graphic Jet Jul 07 '16 at 17:34
  • I realized that I also added: using namespace std;. – Graphic Jet Jul 07 '16 at 17:35
  • So it "seems" that I have fixed all of the problems except one. and this one seems easier. It says that i did not declare "random" in the "int main()" scope. Like I said, I am still a beginner in C++, and do not know hot to declare this "random" properly. If you have any solutions please let me know. Meanwhile, I am doing research of my own, on how to fix this error. – Graphic Jet Jul 14 '16 at 18:41
  • Ok, code updated. You can take a look at it now. I feel that the problems are close to being solved. :P – Graphic Jet Jul 14 '16 at 18:51
  • Well, when I run your code, it says that "randomize was not declared in the 'int main()' function. Any suggestions on how to properly declare "randomize in the parentheses for 'int main()'? – Graphic Jet Jul 24 '16 at 18:33
2

Someone else got to it. There are no semicolons after signatures to methods like main().

One other thing not mentioned, I'm guessing you want

while (u != rn)

Also, be careful of the difference in "=" and "==".

BTW -- Welcome to C++!!!

Brian Cross
  • 142
  • 1
  • 7
0

a little more portable version (doesn't use conio.h) which lets the computer play against himself:

#include <iostream>
#include <cstdlib>
#include <ctime>

int get_random_in_range(int min, int max)
{
    return std::rand() % (max - min) + min;
}

// returns 0 if user guessed right, negative value if user
// guessed too small, positive if user guessed too big
int check_user_guess(int guess, int my_secret)
{
    return guess - my_secret;
}

int main ()
{
    int my_guess = get_random_in_range(1, 10);

    std::cout << "I think of " << my_guess << std::endl;

    std::cout << "Please guess my number: ";
    int user_guess = get_random_in_range(1, 10);
    std::cout << user_guess << std::endl;

    while (check_user_guess(user_guess, my_guess) != 0)
    {
        std::cout << "You guessed " << user_guess << std::endl;

        if (check_user_guess(user_guess, my_guess) > 0)
        {
            std::cout << "You guessed too big!" << std::endl;
        }
        else if (check_user_guess(user_guess, my_guess) < 0)
        {
            std::cout << "You guessed too small!" << std::endl;
        }

        std::cout << "Please guess again: ";
        user_guess = get_random_in_range(1, 10);
        std::cout << user_guess << std::endl;

    }

    std::cout << std::endl << "You guessed it right!";
}

try it here: http://coliru.stacked-crooked.com/a/5bf0b9201ef57529

yussuf
  • 635
  • 1
  • 4
  • 18