1
#include <iostream> #include <string> using namespace std; int main() { int userInput; string numberType; cout << "Enter a number and I will Check to see if it is odd or even" << endl; cin >> userInput; if (userInput % 2 == 0) { string numberType = "Even"; } else { string numberType = "Odd"; } cout << "The number you input is " << numberType << endl; return 0; } 

I'm trying to output the manipulated string value but the string that's output in the last line comes out blank?

1
  • The numberType that you assign in the if/else is local to that block and doesn't assign it to the numberType outside of that block. Remove the string declaration type when assigning it. You'll get what you're looking for. Commented Sep 12, 2013 at 3:17

5 Answers 5

8

In your if and else blocks, you are actually creating new variables with the name of numberType, and that those new variables hide the original numberType you had previously declared. Any changes you make on those variables will NOT reflect to the original numberType, as they do not refer to it.

To solve that problem, remove the string parts (the types, as having these denotes that you are defining new variables) as you don't want to define those new variables, and want use the previously defined numberType.

if (userInput % 2 == 0) { numberType = "Even"; // Look ma, no string! } else { numberType = "Odd"; // Also here ma! } 

With the modified and fixed code, you are now referring to the numberType where you intend to put the values "Even" or "Odd".

... int userInput; string numberType; // This is now where the values be put in cout << "Enter a number and I will Check to see if it is odd or even" << endl; ... 

This should now give you the correct and desired output.

Sign up to request clarification or add additional context in comments.

Comments

2

The problem is that you're redeclaring numberType. This actually declares a new numberType, since it is in a different scope.

What you want to do is this

if (userInput % 2 == 0) { numberType = "Even"; } else { numberType = "Odd"; } 

Comments

1

Beacuse you re declared numberType this will become local to if-else block,In this case the variable local to if-else will get modified.So simply use,

int main() { int userInput; string numberType; cout << "Enter a number and I will Check to see if it is odd or even" << endl; cin >> userInput; string numberType; //variable local to function .You can modify this variable anywhere inside this function if (userInput % 2 == 0) { numberType = "Even"; //here you will refer already declared variable local function. So no need to declare it here again as that will be local to this block. } else { numberType = "Odd"; } cout << "The number you input is " << numberType << endl; // Print modified value of string. return 0; } 

Comments

0

numberType the one whose value is updated is local in if and else block.

remove string

if (userInput % 2 == 0) { numberType = "Even"; } else { numberType = "Odd"; } 

Comments

0

Firstly, you're redeclaring numberType, meaning it only exists in the inner scope it is declared in; effectively, numberType exists only within either the if or else block. You then print out the original outer scoped numberType which was simply default constructed. To fix this, don't redeclare it, but assign it a different value:

string numberType; ... if(userInput % 2 == 0) { numberType = "Even"; } else { numberType = "Odd"; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.