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.

Recommended Answers

All 7 Replies

initialize userGuess = 0

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.

I though that you condition on the value of userGuess, that's not known in the compile time. so the warnings came from.

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:

#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

int main( void ) {
  //Define variables
  std::string userGuess;
  int EuserGuess, wager;


  // 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")
    userGuess = "odd";
  else if (userGuess == "red")
    userGuess = "even";
  else if (userGuess == "exact" ) {
    cout << "What number would you like you place your bet(1-36)?";
    cin >> EuserGuess;
  } else {
    cout<< "Error! Exiting";
    return 1;
  }

  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;
}

WIthout the capitalisation checking.

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.

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?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.