C++ error that's bugging me...

Discuss all aspects of programming here.

Moderator: The Mod Squad

C++ error that's bugging me...

Postby Snowfire » Fri Dec 10, 2004 11:29 am

Hello all, I know that this is kind of a basic question, but my school is really bad at teaching how to fix errors, here is the error I keep getting:

error C2065: 'number' : undeclared identifier.

How do I fixie?
Tono Sien


Love is the greatest conspiracy, by the most loving genius.
Snowfire
Black Belt 1st Degree
Black Belt 1st Degree
 
Posts: 1486
Joined: Sun May 11, 2003 8:47 pm
Location: Omaha, NE

Postby Roach412 » Fri Dec 10, 2004 11:32 am

dim number as int.

need to declare that sucker. :D

-Roach
Roach412
Black Belt 2nd Degree
Black Belt 2nd Degree
 
Posts: 2718
Joined: Mon Aug 09, 2004 7:33 pm
Location: Milwaukee - Wisconsin

Postby Snowfire » Fri Dec 10, 2004 11:34 am

Ok, but now I get this:

warning C4700: local variable 'number' used without having been initialized

:?
Tono Sien


Love is the greatest conspiracy, by the most loving genius.
Snowfire
Black Belt 1st Degree
Black Belt 1st Degree
 
Posts: 1486
Joined: Sun May 11, 2003 8:47 pm
Location: Omaha, NE

Postby Roach412 » Fri Dec 10, 2004 11:35 am

dim number as int = 0

just set it to some number so that it has something to start with.

-Roach
Roach412
Black Belt 2nd Degree
Black Belt 2nd Degree
 
Posts: 2718
Joined: Mon Aug 09, 2004 7:33 pm
Location: Milwaukee - Wisconsin

Postby Snowfire » Thu Dec 16, 2004 8:35 am

ok, but if I'm trying to use it with something like this:

for (int j=0; j<arraysize; j++){
cout << salrange[k] << endl;
}

Then how do I get it to work? If I int it as 0 then I get a major negative number...
Tono Sien


Love is the greatest conspiracy, by the most loving genius.
Snowfire
Black Belt 1st Degree
Black Belt 1st Degree
 
Posts: 1486
Joined: Sun May 11, 2003 8:47 pm
Location: Omaha, NE

Postby Falklian » Thu Dec 16, 2004 6:36 pm

If you declare number as int, then you must assign a value to it or assign it later before using it.

If you do something like this:

int number;

cout << number;

You're going to get garbage. So if you do something like this:

int number;

for (int x = 0; x < number; x++)
{
cout << x << endl;
}

You're going to get mixed results as number is not initialized as anything yet. You will get basically garbage and an error. You need to assign the maximum you want to output.

So for example, you have the code:

for(int j = 0; j < arraysize; j++)
{
cout << salrange[k] << endl;
}

You would need to assign a number to your arraysize variable if you haven't elsewhere. Also, I don't see k as working in this scenario. Is it defined elsewhere? And if so, you're going to get the same result x amount of times (whatever you have assigned to arraysize) Basically, if k equals 5 and salrange[5] stores a wage, for example 100 dollars, you are going to print out the number 100 for each iteration of your for loop.

So let's say you have an array of 100 salaries. You would do something like this:

int arraysize = 100;

for(int j = 0; j < arraysize; j++)
{
cout << salrange[j] << endl;
}

Is that what you were asking about? I didn't fully understand your question with the example you gave.
Falklian
Black Belt
Black Belt
 
Posts: 921
Joined: Tue Oct 01, 2002 2:06 pm
Location: Illinois

Postby Roach412 » Thu Dec 16, 2004 6:51 pm

i think you're really close, but i "think" he's asking about the for loop variable j.

dim int j = 0

for(int j=0; j<blah; j++){
do stuff;
}

should work...i think.
or you might be able to just initialize it in the declaration.
for(dim in j=0;.......) i can't remember if c++ likes that or not....i'm starting to think it doesn't....

it's been about 2 years since i did any c++.... :?

-Roach
Lian Li Lancool First Knight Series PC-K59W
Intel Core i7-960 Bloomfield 3.2GHz
EVGA X58 FTW3 132-GT-E768-TR
EVGA GeForce GTX 1070 FTW GAMING ACX 3.0
G.SKILL Ripjaws Series 12GB (3 x 4GB)
Crucial M4 128gb SATAIII SSD x2
Crucial M4 256gb SATAIII SSD
OCZ Saber 1000 960GB Enterprise SSD
Corsair Professional Series HX850 PSU
Dell UltraSharp U2713HM 27" w/drop ceiling mount
Logitech G930 7.1 Headset
DBPOWER RGB LED Keyboard
Logitech G402 Hyperion Fury
Roach412
Black Belt 2nd Degree
Black Belt 2nd Degree
 
Posts: 2718
Joined: Mon Aug 09, 2004 7:33 pm
Location: Milwaukee - Wisconsin

Postby Falklian » Thu Dec 16, 2004 7:34 pm

I haven't worked with C++ in about 2 years as well, but I don't think he's asking about the variable j. Looking at his example, with each iteration of the for loop, j is increasing by one and in no way would result in a negative number unless j ends up exceeding the capacity of what can be stored in an int. If that is the case then he has a lot of ouput 8O

By the way, what is dim? I don't recall ever hearing that before... or I just don't remember :oops:

Or... for some reason it is not declaring correctly. I just fired up VC++ 6 to see what I got without giving an int variable a value and I also recieved a very large negative number (actually, that would be small :wink: ) Basically, when your application allocates memory for that int that you want to use, whatever is stored in that hunk of memory at the time will be displayed if you do not give a value to your variable. That shouldn't happen if you assign a value to your int variable. If it is assigning garbage, you could try a couple things. If your variable is declared in one function and you are trying to use it in another, you need to pass that variable to the function you are using it in. You can also declare that variable globally instead of locally in a function.

Hope that makes sense, kinda started rambling. :lol:
Falklian
Black Belt
Black Belt
 
Posts: 921
Joined: Tue Oct 01, 2002 2:06 pm
Location: Illinois

Postby Roach412 » Thu Dec 16, 2004 7:49 pm

lol. :lol: :lol: :lol:

wow...it's been way too long since i did some c++...that, and i've been doing like 4 other languages since then....i'm mixing and matching.

nevermind on the dim statement. that's a diff lang.

my bad. :oops: somebody should have said something when i started going on it!

-Roach
Lian Li Lancool First Knight Series PC-K59W
Intel Core i7-960 Bloomfield 3.2GHz
EVGA X58 FTW3 132-GT-E768-TR
EVGA GeForce GTX 1070 FTW GAMING ACX 3.0
G.SKILL Ripjaws Series 12GB (3 x 4GB)
Crucial M4 128gb SATAIII SSD x2
Crucial M4 256gb SATAIII SSD
OCZ Saber 1000 960GB Enterprise SSD
Corsair Professional Series HX850 PSU
Dell UltraSharp U2713HM 27" w/drop ceiling mount
Logitech G930 7.1 Headset
DBPOWER RGB LED Keyboard
Logitech G402 Hyperion Fury
Roach412
Black Belt 2nd Degree
Black Belt 2nd Degree
 
Posts: 2718
Joined: Mon Aug 09, 2004 7:33 pm
Location: Milwaukee - Wisconsin


Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest