User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,507 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,682 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1628 | Replies: 7 | Solved
Reply
Join Date: Sep 2007
Posts: 6
Reputation: HoldenCfld is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
HoldenCfld HoldenCfld is offline Offline
Newbie Poster

Uninitialized Local Variable help

  #1  
Sep 27th, 2007
I have a project for class and I have an error coming up that I don't understand. I've written code the same way I always do, and haven't found that I've done something oddly, but I still get an uninitialized local variable warning.

Here is my code:

include <iostream>
include <iomanip>
include <cmath>
include <cstdlib>
include <ctime>
using namespace std;

int main()
{
//Define variables
int userGuess, EuserGuess, wager;
int Black, black, Red, red, odd, even;
int Exact, exact;




// Welcome user and display odds
cout << "Here are your odds:\n\n";
cout << " 1 to 1 | Black (Odd Numbers)\n";
cout << " 1 to 1 | Red (Even Numbers)\n";
cout << "35 to 1 | Exact Guess..n" << endl;

//Ask for user input
cout << "How would you like to place your bet?\n";
cout << "Black, Red, or Exact: ";
cin >> userGuess;
if (userGuess == Black || black)
userGuess = odd;
if (userGuess == Red || red)
userGuess = even;
if (userGuess == Exact || exact)
{
cout << "What number would you like you place your bet(1-36)?";
cin >> EuserGuess;
}

cout << "How much would you like to wager?\n";
cout << "$ ";
cin >> wager;

cout << "\n\n\nYou have chosen to wager " << wager << " on " << userGuess << endl << endl;

return 0;
}

It's unfinished, but I don't think that matters much.

Any ideas?

Thanks for any help in advance.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2006
Location: Egypt
Posts: 763
Reputation: RamyMahrous is on a distinguished road 
Rep Power: 4
Solved Threads: 59
Featured Poster
RamyMahrous's Avatar
RamyMahrous RamyMahrous is offline Offline
Master Poster

Re: Uninitialized Local Variable help

  #2  
Sep 27th, 2007
initialize userGuess = 0
B.Sc Computer Science, Helwan University
Microsoft Student Partner
Personal blog http://ramymahrous.blogspot.com/
Arabic technical blog http://fci-h-ar.blogspot.com/
English technical blog http://fci-h.blogspot.com/
Reply With Quote  
Join Date: Sep 2007
Posts: 6
Reputation: HoldenCfld is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
HoldenCfld HoldenCfld is offline Offline
Newbie Poster

Re: Uninitialized Local Variable help

  #3  
Sep 27th, 2007
Originally Posted by RamyMahrous View Post
initialize userGuess = 0

Is there any special way or place I am supposed to put that in? Sorry for the ignorance, but I'm only beginning. I tried and it still gave me the warnings.

The warnings were on variables: Black, black, Red, red, odd, even, Exact, exact, in case it wasn't obvious.
Reply With Quote  
Join Date: May 2007
Location: Ireland
Posts: 1,761
Reputation: twomers will become famous soon enough twomers will become famous soon enough 
Rep Power: 6
Solved Threads: 34
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: Uninitialized Local Variable help

  #4  
Sep 27th, 2007
Well you'll have to initialise all of the variables.

>> if (userGuess == Black || black)
That's not (or rather mightn't), work as you would expect: it'll do the || ing first and then the == (or the other way around, I can't remember), so what you want to do is probably:

if (userGuess==Black || userGuess==black)

Same goes for the other two if's.


You know. I think what you're trying to do is use strings... something like this might work better:

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. #include <cstdlib>
  5. #include <ctime>
  6.  
  7. using namespace std;
  8.  
  9. int main( void ) {
  10. //Define variables
  11. std::string userGuess;
  12. int EuserGuess, wager;
  13.  
  14.  
  15. // Welcome user and display odds
  16. cout << "Here are your odds:\n\n";
  17. cout << " 1 to 1 | Black (Odd Numbers)\n";
  18. cout << " 1 to 1 | Red (Even Numbers)\n";
  19. cout << "35 to 1 | Exact Guess..n" << endl;
  20.  
  21. //Ask for user input
  22. cout << "How would you like to place your bet?\n";
  23. cout << "Black, Red, or Exact: ";
  24. cin >> userGuess;
  25.  
  26. if (userGuess == "black")
  27. userGuess = "odd";
  28. else if (userGuess == "red")
  29. userGuess = "even";
  30. else if (userGuess == "exact" ) {
  31. cout << "What number would you like you place your bet(1-36)?";
  32. cin >> EuserGuess;
  33. } else {
  34. cout<< "Error! Exiting";
  35. return 1;
  36. }
  37.  
  38. cout << "How much would you like to wager?\n";
  39. cout << "$ ";
  40. cin >> wager;
  41.  
  42. cout << "\n\n\nYou have chosen to wager " << wager << " on " << userGuess << endl << endl;
  43.  
  44. return 0;
  45. }

WIthout the capitalisation checking.
Last edited by twomers : Sep 27th, 2007 at 7:41 pm.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote  
Join Date: Aug 2006
Location: Egypt
Posts: 763
Reputation: RamyMahrous is on a distinguished road 
Rep Power: 4
Solved Threads: 59
Featured Poster
RamyMahrous's Avatar
RamyMahrous RamyMahrous is offline Offline
Master Poster

Re: Uninitialized Local Variable help

  #5  
Sep 27th, 2007
I though that you condition on the value of userGuess, that's not known in the compile time. so the warnings came from.
B.Sc Computer Science, Helwan University
Microsoft Student Partner
Personal blog http://ramymahrous.blogspot.com/
Arabic technical blog http://fci-h-ar.blogspot.com/
English technical blog http://fci-h.blogspot.com/
Reply With Quote  
Join Date: Aug 2006
Location: Egypt
Posts: 763
Reputation: RamyMahrous is on a distinguished road 
Rep Power: 4
Solved Threads: 59
Featured Poster
RamyMahrous's Avatar
RamyMahrous RamyMahrous is offline Offline
Master Poster

Re: Uninitialized Local Variable help

  #6  
Sep 27th, 2007
and also you want to say for the compile time you've garbage value may = garbage value that's may do this warnings.
Hint: you should initialize your variables before using.
B.Sc Computer Science, Helwan University
Microsoft Student Partner
Personal blog http://ramymahrous.blogspot.com/
Arabic technical blog http://fci-h-ar.blogspot.com/
English technical blog http://fci-h.blogspot.com/
Reply With Quote  
Join Date: Sep 2007
Posts: 6
Reputation: HoldenCfld is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
HoldenCfld HoldenCfld is offline Offline
Newbie Poster

Re: Uninitialized Local Variable help

  #7  
Sep 27th, 2007
Your first suggestion didn't stop the warning, and when I tried the second large change in the code, it says that both userGuess and EuserGuess are undeclared identifiers.

Also after you changed it to a string set-up, you changed all the variables (black, red, odd, even, etc) to inside quotations, why?
Reply With Quote  
Join Date: Sep 2007
Posts: 6
Reputation: HoldenCfld is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
HoldenCfld HoldenCfld is offline Offline
Newbie Poster

Re: Uninitialized Local Variable help

  #8  
Sep 27th, 2007
Ok, after adding include string, and adjusting the cin statement to getline(cin, userGuess), and a few other things here and there, it seems that the code is now working.

Thank you all for you help, I'll be sure to mark it solved.

I really appreciate all your help.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 3:40 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC