Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

Simple code doesn't work?

Think you can spy out that little nasty bug? Try debugging some code in here

Simple code doesn't work?

Postby VLucarius on Sun May 24, 2009 7:22 am

Hey all. I got redirected to this community from antiRTFM's YouTube channel.
Since I am new to these forums, forgive me if I have posted this in the wrong place.

Enough of the chit-chat.

Well, the problem is that I was practicing on special operators such +=, -=, *=, /= and %=.
I tried using this code, as well as some additional comments to aid me:

Code: Select all
include "stdafx.h" // include only this for Microsoft Visual C++ compilers!!


#include <iostream> // blue words are known as 'keywords'.

using namespace std; // 'namespace' is a package which is found in the file 'iostream'.

int main() // 'int' is a function . 'main' is a function name.
{
   signed long int j;
   j += 1; // '+=' will take whatever is on the left plus whatever is on the right, and then set that number to whatever is on the left. For instance, i += 2 means i = i+2.

   cout << j << endl;

   j -= 1; // '-=' will take whatever is on the left minus whatever is on the right, and then set that number to whatever is on the left. For instance, i -= 2 means i = i-2.

   cout << j << endl;

   j *= 1; // '*=' will take whatever is on the left multiply whatever is on the right, and then set that number to whatever is on the left. For instance, i *= 2 means i = i*2.

   cout << j << endl;

   j /= 1; // '/=' will take whatever is on the left divide by whatever is on the right, and then set that number to whatever is on the left. For instance, i /= 2 means i = i/2.

   cout << j << endl;

   j %= 1;

   cout << j << endl;
   cin.get(); // waits until the user presses enter.
   return 0; // indicates 'over'. Goes back to the computer and exits the application compiled.
}


This code was successfully compiled. However, when the program was opened, there was a Visual C++ Run-Time error indicating that the variable 'j' was used without being initialized, or something along those lines, and it asked me if I would like to break or continue the program. If I chose continue, the program would hang and I had to end its process. If I chose break, the program would simply hang for a moment before quitting by itself.

What is the error?
VLucarius
 
Posts: 3
Joined: Sun May 24, 2009 7:14 am

Re: Simple code doesn't work?

Postby C++ on Sun May 24, 2009 10:38 am

Code: Select all
int j = 0; // You need to assign a starting value to j, because not doing so in this case results in undefined behavior
New to C++ programming? Click here
C++
teh awesome
 
Posts: 217
Joined: Sun May 25, 2008 7:45 am

Re: Simple code doesn't work?

Postby antiRTFM on Sun May 24, 2009 2:27 pm

"Run-Time error indicating that the variable 'j' was used without being initialized"

couldn't have explained it better :)
User avatar
antiRTFM
Administrator
 
Posts: 470
Joined: Sun Apr 13, 2008 9:10 am

Re: Simple code doesn't work?

Postby VLucarius on Mon May 25, 2009 3:16 am

Oh I see, thanks guys, by the way, does integers need to be given a number at creation? I thought only constants need that?
VLucarius
 
Posts: 3
Joined: Sun May 24, 2009 7:14 am

Re: Simple code doesn't work?

Postby antiRTFM on Mon May 25, 2009 7:08 am

nope integers dont need to be given something at creation

but using an integer variable without having given it something first - that's the problem
User avatar
antiRTFM
Administrator
 
Posts: 470
Joined: Sun Apr 13, 2008 9:10 am

Re: Simple code doesn't work?

Postby VLucarius on Mon May 25, 2009 9:52 am

Oh I see thanks :D
VLucarius
 
Posts: 3
Joined: Sun May 24, 2009 7:14 am


Return to Debug

Who is online

Users browsing this forum: No registered users and 0 guests

cron