C++ Program 'Random Number...' Refuses to Compile

Apr 9, 2015 at 11:58pm
Hello!

My project for CS class is to create a program that generates a random number and prompts the user to guess it. Though, to me, it seems as if I have all the parts, it won't compile. These are the error I keep getting (I have no idea how to fix them):

Prog3.cpp: In function ‘int main()’:
Prog3.cpp:20:4: error: ‘cout’ was not declared in this scope
Prog3.cpp:20:4: note: suggested alternative:
In file included from Prog3.cpp:6:0:
/usr/include/c++/4.7/iostream:62:18: note: ‘std::cout’
Prog3.cpp:27:39: error: ‘endl’ was not declared in this scope
Prog3.cpp:27:39: note: suggested alternative:
In file included from /usr/include/c++/4.7/iostream:40:0,
from Prog3.cpp:6:
/usr/include/c++/4.7/ostream:562:5: note: ‘std::endl’
Prog3.cpp:28:4: error: ‘cin’ was not declared in this scope
Prog3.cpp:28:4: note: suggested alternative:
In file included from Prog3.cpp:6:0:
/usr/include/c++/4.7/iostream:61:18: note: ‘std::cin’

And here's the actual code:
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
  #include <iostream>
#include <ctime>
#include <cstdlib>

int main()
{
   int guess;
   int tries = 0;
   int randomN;
   randomN= rand()%100 + 1;
   srand(time(0));
   int numbercases;
   numbercases= 0;

   cout << "The computer has chosen a random number. You have to "
        << "guess what it is.";

//Here, the user will take their guess to see what the random
//number is.
do
{
   cout << "What is your guess?: " << endl;
   cin >> guess;
   numbercases++;

   if (guess < randomN)
   {
        cout << "Aw! Too low! Try again, okay?" << endl;
   }
   else if (guess > randomN)
   {
        cout << "Ugh! Too high! Try again." << endl;
   }
     if (guess == randomN)
   {
        cout << "That's it; you got it!" << endl;
   }
   else if (guess < 0)
        cout << "Game over. You've given up." << endl;
} while (guess != randomN);

return 0;

}


If I'm missing a library, too, could someone let me know? I feel like I am, but I don't know them all.
Apr 10, 2015 at 12:28am
closed account (2LzbRXSz)
It seems like most of your problems stem from not having a namespace.

you could always do at the top of your main function.
1
2
3
using std::cout;
using std::endl;
using std::cin;


Or, even though it's considered bad practice just write using namespace std; under your includes.

Other than that, it compiles just fine for me now:)
Apr 10, 2015 at 12:31am
Try putting this above main - using namespace std;
Apr 10, 2015 at 12:31am
closed account (2LzbRXSz)
(I'm not sure if my reply already posted because my browser crashed, but just in case...)

You're missing your namespace.

You can fix that by adding
1
2
3
using std::cout;
using std::endl;
using std::cin;

Or
using namespace std; after your includes, though just know to be careful with namespaces with future use. It's not the safest, or the best practice.

You can read more about namespaces here:)
http://www.cplusplus.com/doc/tutorial/namespaces/
Apr 10, 2015 at 12:32am
closed account (2LzbRXSz)
(I'm not sure if my reply already posted because my browser crashed, but just in case...)

You're missing your namespace.

You can fix that by adding
1
2
3
using std::cout;
using std::endl;
using std::cin;

Or
using namespace std; after your includes, though just know to be careful with namespaces with future use. It's not the safest, or the best practice.

You can read more about namespaces here:)
http://www.cplusplus.com/doc/tutorial/namespaces/
Apr 10, 2015 at 12:32am
closed account (2LzbRXSz)
(I'm not sure if my reply already posted because my browser crashed, but just in case...)

You're missing your namespace.

You can fix that by adding
1
2
3
using std::cout;
using std::endl;
using std::cin;

Or
using namespace std; after your includes, though just know to be careful with namespaces with future use. It's not the safest, or the best practice.

You can read more about namespaces here:)
http://www.cplusplus.com/doc/tutorial/namespaces/
Apr 10, 2015 at 12:33am
Put using namespace std; above main
Apr 10, 2015 at 12:33am
closed account (2LzbRXSz)
(I'm not sure if my reply already posted because my browser crashed, but just in case...)

You're missing your namespace.

You can fix that by adding
1
2
3
using std::cout;
using std::endl;
using std::cin;

Or
using namespace std; after your includes, though just know to be careful with namespaces with future use. It's not the safest, or the best practice.

You can read more about namespaces here:)
http://www.cplusplus.com/doc/tutorial/namespaces/
Apr 10, 2015 at 12:34am
closed account (2LzbRXSz)
(I'm not sure if my reply already posted because my browser crashed, but just in case...)

You're missing your namespace.

You can fix that by adding
1
2
3
using std::cout;
using std::endl;
using std::cin;

Or
using namespace std; after your includes, though just know to be careful with namespaces with future use. It's not the safest, or the best practice.

You can read more about namespaces here:)
http://www.cplusplus.com/doc/tutorial/namespaces/
Apr 10, 2015 at 12:35am
closed account (2LzbRXSz)
Oh my god my reply posted like 7 times. Sorry about that. I'll delete them.
Apr 10, 2015 at 7:26am
LOOOOOOOOL THIS THREAD... HAHA.

Yeh, it lagged like crazyyyyyyyy last night I also posted twice apparently :D
Apr 10, 2015 at 10:33am
closed account (2LzbRXSz)
The lag was so bad I couldn't get on the site, and then when I finally could, i couldn't get on the servers. I thought the website was under attack or something.

It won't let me delete my replies.
Apr 10, 2015 at 1:08pm
For future reference: you cannot delete your post unless it is the last post in the thread.
Apr 10, 2015 at 7:34pm
Thank you, everyone! I didn't even realize my problem. I truly appreciate the help! <3
Apr 10, 2015 at 9:06pm
closed account (2LzbRXSz)
Ahh, I didn't know that. Thanks LB!

No problem, A R :)
Topic archived. No new replies allowed.