•
•
•
•
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
![]() |
•
•
Join Date: Sep 2007
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
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:
It's unfinished, but I don't think that matters much.
Any ideas?
Thanks for any help in advance.
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.
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/
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/
•
•
Join Date: Sep 2007
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 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.
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:
WIthout the capitalisation checking.
>> 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:
c Syntax (Toggle Plain Text)
#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.
Last edited by twomers : Sep 27th, 2007 at 7:41 pm.
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/
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/
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.
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/
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/
•
•
Join Date: Sep 2007
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
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?
Also after you changed it to a string set-up, you changed all the variables (black, red, odd, even, etc) to inside quotations, why?
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- I dont know why I am getting these warning please help!! (C++)
- C++ Variable help (C++)
- [Warning] address of local variable returned??? (C++)
- Return variable (C++)
- cout is too slow (C++)
- Urgent help needed Win_socket questions (C)
- static variable (C)
- C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays (C++)
Other Threads in the C++ Forum
- Previous Thread: Linked List problem
- Next Thread: postfix expression evaluation



Linear Mode