Sign in to follow this  
Billyjoeljr

Variable being used without initialized?

Recommended Posts

Billyjoeljr    105
Hi there, i'm a total newb with programming and I'm following along in this book. At the end of chapter one it has me write this program and I keep getting an error saying "Adventurers is being used without being initialized. Can someone please help or explain? Thanks

//Lost Fortune
//A Personalizwd adventure

#include<iostream>
#include<string>
using std::cout;
using std::cin;
using std::endl;
using std::string;

int main()
{
const int GOLD_PIECES = 900;
int adventurers, killed, survivors;
string leader;

//get the information
cout << "Welcome to Lost Fortune\n\n";
cout << "Please enter the follow for your personalized adventure\n";

cout<<"Enter a number: ";
cin >> killed;

survivors = adventurers - killed;

cout<< "Enter you last name: ";
cin>>leader;

//tell the story
cout<<"\n A Brave group of"<<adventurers<<"set out on a quest";
cout<<"- in search of the lost treasure of the Ancient Dwarves.";
cout<<"The group was led by that legendary rogue,"<<leader<<".\n";

cout<<"\nAlong the way, a band of marauding ogres ambushed the party.";
cout<<"All fought bracely under the command of"<<leader;
cout<<", and the ogres were defeated but at a cost.";
cout<<"Of the adventurers, "<<killed<<"were vanquished,";
cout<<"leaving just"<<survivors<<"in the group.\n";
cout<<"\nThe party was about to give up all hope.";
cout<<"But while laying the deceased to rest.";
cout<<"they stumbled upon the buried fortune";
cout<<"So the adventurers split"<<GOLD_PIECES<<"gold pieces.";
cout<<leader<<"held on to the extra"<<(GOLD_PIECES % survivors);
cout<<"pieces to keep things fair of course.\n";

return 0;

}

Share this post


Link to post
Share on other sites
frob    44903
The variable 'adventurers' has never been given a value. The compiler is telling you that the memory space contains garbage and you will get bad results.

I'm guessing that in your lesson it had some extra lines by the "get the information" section, where the adventurers variable was initialized.

Probably something like:

cout << "How many adventurers?";
cin >> adventurers; Edited by frob
writing code on a cell's touch screen is hard.

Share this post


Link to post
Share on other sites
musafir2007    201
when you come to this line in code:
survivors = adventurers - killed;

what do you think happens? it tries to subtract the value of killed from the value of adventurers. Since when you declared adventurers, you did not assign it a number, the program doesn't know what it is.
So in the code somewhere you need to assign the variable with some value. you can do this either at the beginening when you declare it:
int adventurers, killed, survivors;

The only varaible you don't "have" to declare is survivros, since you assign that with the subtraction before you use it.
It is always a good idea to define a int variable with 0, when you declare it, otherwise compiler might not give you error but the varible will hold random number (memory address)
Hope this helps.

Share this post


Link to post
Share on other sites
frazchaudhry    1220
here is how I did this exercise and this code works. Basically I assigned the int variables to some value before using them in any arithmetic operation.

[CODE]
// Lost Fortune
// A personalized adventure
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::string;
int main()
{
const int GOLD_PIECES = 900;
int adventurers, killed, survivors;
string leader;

// Get the information
cout << "Welcome to Lost Fortune\n\n";
cout << "Please enter the following for your personalized adventure\n";

cout << "Enter a number: ";
cin >> adventurers;

cout << "Enter a number, smaller than the first: ";
cin >> killed;

survivors = adventurers - killed;

cout << "Enter your last name: ";
cin >> leader;

// Tell the story
cout << "\nA brave group of " << adventurers << " adventurers set out on a quest ";
cout << "--in search of the lost treasure of the Ancient Dwarves. ";
cout << "The group was led by that legendary rogue, " << leader << ".\n";

cout << "\nAlong the way, a band of marauding ogres ambushed the party. ";
cout << "All fought bravely under the command of " << leader;
cout << ", and the ogres were defeated, but at a cost. ";
cout << "Of the adventurers, " << killed << " were vanquished, ";
cout << "leaving just " << survivors << " in the group.\n";

cout << "\nThe party was about to give up all hope. ";
cout << "But while laying the deceased to rest, ";
cout << "they stumbled upon the buried fortune. ";
cout << "So the adventurers split " << GOLD_PIECES << " gold pieces.";
cout << leader << " held on to the extra " << (GOLD_PIECES % survivors);
cout << " pieces to keep things fair of course.\n";


system("pause");
return 0;
}
[/CODE]

Share this post


Link to post
Share on other sites
larspensjo    1561
C and C++ are a little inconsistent here. If you declare a local variable in a function, it will not be initialized (unless it is an object from a class with a constructor). If it is declared globally, or with static scope inside a function, it will usually be initialized to 0. See [url="http://stackoverflow.com/questions/1169858/global-memory-management-in-c-in-stack-or-heap"]Memory management[/url] for some more details.

In general, it is a good idea to always initialize variables to 0 or a null value when you declare them.

Share this post


Link to post
Share on other sites
szecs    2990
I don't initialize local variables by default unless I explicitly need them to be initialized (with a constant or zero for example). I rely on the compiler with this, it gives a bit of extra bug-safety. If I forget to calculate something, the compiler will warn me.
Maybe it's not the best way to do it though, but it saved me a few times from debugging. Edited by szecs

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this