Sign in with
Sign up | Sign in
Your question
Solved

Code::Blocks C++ question

Last response: in Applications
Share
January 18, 2011 4:02:54 PM

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-AO...

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

More about : code blocks question

a b L Programming
January 19, 2011 1:31:30 AM

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).
m
0
l
January 19, 2011 6:08:08 PM

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.

m
0
l
Related resources
January 19, 2011 11:12:49 PM

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


m
0
l

Best solution

a b L Programming
January 20, 2011 1:00:54 AM

try adding to the top of the document (at least in a linux enviorment, not sure about windows):
#include <cstdlib>
Share
January 20, 2011 1:31:40 AM

Thanks kyeana, that worked. Do you know why that was needed? The tutorial video in the OP didn't have that included.
m
0
l
January 20, 2011 1:32:33 AM

Best answer selected by Gamerboy.
m
0
l
a b L Programming
January 20, 2011 2:24:06 PM

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/
m
0
l
January 20, 2011 6:42:59 PM

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.
m
0
l
!