Code::Blocks C++ question

Gamerboy

Distinguished
Aug 13, 2010
36
0
18,580
0
Hello, I'm just starting to toy with programming in my spare time.

I was watching a video about a random number game and tried to write the program into codeblocks but it's giving me errors that I don't understand.

This is the video:
http://www.youtube.com/user/reconnetworks#p/u/3/cW3c-AO4g7s

It works for him, but it's not working for me.

This is my code:

1 #include<iostream>
2 #include<ctime>
3 using namespace std;
4
5 int main(void)
6 {
7 int iGumballs;
8 int iUserguess;
9 int iGuesses = 0;
10
11 while(true)
12 {
13
14 system("CLS");
15 cin.clear();
16 iGuesses = 0;
17
18 srand(static_cast<unsigned_int>(time(0)));
19 iGumballs = rand()%1000+1;
20 cout << "How many gumballs are in the jar?" << endl;
21
22 do
23 {
24 cout << "Enter your guess: ";
25 cin >> iUserguess;
26 if(iUserguess > iGumballs)
27 {
28 cout << "Too high" << endl << endl;
29 }
30 if(iUserguess < iGumballs)
31 {
32 cout << "Too low" << endl << endl;
33 }
34 iGuesses ++;
35
36 }while(iUserguess > iGumballs || iUserguess < iGumballs);
37 cout << "You're Right, yay!" << endl << endl;
38 cout << "You took " << iGuesses << " guesses" << endl << endl;
39 system("PAUSE");
40 }
41 return 0;
42}


These are my errors:

line 1 error: iostream: No such file or directory
line 2 error: ctime: No such file ore directory
line 3 error: expected '=', ',', ';', 'asm' or '_attribute_' before 'namespace'
line 11 error: 'true' undeclared (first use in this function)
line 11 error: (Each undeclared identifier is reported only once
line 11 error: for each function it appears in.)
line 14 warning: implicit declaration of function 'system'
line 15 error: 'cin' undeclared (first use in this function)
line 18 warning: implicit declaration of function 'srand'
line 18 error: 'static_cast' undeclared (first use in this function)
line 18 error: 'insigned_int' undeclared (first use in this function)
line 18 warning: implicit declaration of function 'time'
line 19 warning: implicit declaration of function 'rand'
line 20 error: 'cout' undeclared (first use in this function)
line 20 error: 'endl' undeclared (first use in this function)



I don't understand what it's doing. Why doesn't it recognize iostream and ctime? It does that with some programs but not others.

Can you explain what it's doing? Thanks
 

calmstateofmind

Distinguished
Jul 2, 2009
292
0
19,010
21
What IDE are you using? It sounds to me like you don't have any libraries installed, or they can't be accessed. I'd say your best (fastest) bet would be to uninstall whatever IDE you're using and reinstall, making sure this time around all of your libraries are being installed.

All of your errors refer to functions and operators, which are defined in libraries and accessed through your include statements at the top of the page. Other than those, it seems like everything else is in order (both grammatically and semantically).
 

Gamerboy

Distinguished
Aug 13, 2010
36
0
18,580
0
I'm using Code::Blocks. I'll try that, thanks.

The weird thing is, it doesn't do that with the hello world program that the IDE came with. I can modify the file that the hello world was in and they work correctly, but when I try to create a new, empty, project this is what happens.


edit: I reinstalled Code::Blocks, but the same errors occur.

 

Gamerboy

Distinguished
Aug 13, 2010
36
0
18,580
0
So I wrote the code into a "Console application" instead of an "Empty project" and it works better, but still not correctly.

Code:


#include <iostream>
#include <ctime>
using namespace std;

int main(void)
{
int iGumballs;
int iUserguess;
int iGuesses = 0;

while(true)
{
system("CLS");
cin.clear();
iGuesses = 0;

srand(static_cast<unsigned int>(time(0)));
iGumballs = rand()%1000+1;
cout << "How many gumballs are in the jar?" << endl;
do
{
cout << "Enter your guess: ";
cin >> iUserguess;
if(iUserguess > iGumballs)
{
cout << "Too high!" << endl << endl;
}
if(iUserguess < iGumballs)
{
cout << "Too low!" << endl << endl;
}
iGuesses ++;
}while(iUserguess > iGumballs || iUserguess < iGumballs);
cout << "You're correct, nice going!" << endl << endl;
cout << "You took " << iGuesses << "guesses" << endl << endl;
system("PAUSE");
}
return 0;
}




Errors:

-------------- Build: Debug in Gumballs ---------------

mingw32-g++.exe -Wall -fexceptions -g -c "C:\Program Files\CodeBlocks\Gumballs\main.cpp" -o obj\Debug\main.o
C:\Program Files\CodeBlocks\Gumballs\main.cpp: In function 'int main()':
C:\Program Files\CodeBlocks\Gumballs\main.cpp:13: error: 'system' was not declared in this scope
C:\Program Files\CodeBlocks\Gumballs\main.cpp:17: error: 'srand' was not declared in this scope
C:\Program Files\CodeBlocks\Gumballs\main.cpp:18: error: 'rand' was not declared in this scope
Process terminated with status 1 (0 minutes, 0 seconds)
3 errors, 0 warnings


 

kyeana

Distinguished
May 21, 2008
230
0
18,860
7
I don't know why the tutorial in the video worked without it. I didn't actually watch it, but it seems like it shouldn't have so I couldn't tell you there.

The reason you need it is like the computer was saying when you tried to compile the code, it couldn't figure out what srand, rand, and system were. As it turns out all of those are included in cstdlib, and thus when you included that the computer knew what to do.

Here is a list of everything that can be used from cstdlib as a reference:
http://www.cplusplus.com/reference/clibrary/cstdlib/
 

Gamerboy

Distinguished
Aug 13, 2010
36
0
18,580
0
Yeah, I googled C++ library and realized that. I don't know how he got the functions to work under ctime, as far as I could find they're not listed there.

Thanks again for your help.
 
Thread starter Similar threads Forum Replies Date
R Apps General Discussion 11
C Apps General Discussion 2
C Apps General Discussion 3
henrytcasey Apps General Discussion 1
S Apps General Discussion 3
J Apps General Discussion 1
C Apps General Discussion 1
Mighty Apps General Discussion 9
A Apps General Discussion 8
A Apps General Discussion 1
J Apps General Discussion 1
O Apps General Discussion 1
G Apps General Discussion 0
R Apps General Discussion 4
kol12 Apps General Discussion 7
J Apps General Discussion 6
Durwesh Naeem Apps General Discussion 2
P Apps General Discussion 2
R Apps General Discussion 1
g-unit1111 Apps General Discussion 7

Similar threads


ASK THE COMMUNITY