You are using a version without Ads of this website. Please, consider donating:

### Variable isn't being initialized

I am trying to create a basic program that takes a binary number (0 to 15) and converts to hex. However, I am getting a run time error saying that b is being used without being initialized. Any help would be greatly appreciated.
Here's the code:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586`` ``````#include using namespace std; char convert_to_hex( int x ){ char b; if (x == 0000) b = '0'; else if (x == 0001) b = '1'; else if (x == 0010) b = '2'; else if (x == 0011) b = '3'; else if (x == 0100) b = '4'; else if (x == 0101) b = '5'; else if (x == 0110) b = '6'; else if (x == 0111) b = '7'; else if (x == 1000) b = '8'; else if (x == 1001) b = '9'; else if (x == 1010) b = 'A'; else if (x == 1011) b = 'B'; else if (x == 1100) b = 'C'; else if (x == 1101) b = 'D'; else if (x == 1101) b = 'E'; else if (x == 1111) b = 'F'; return b; } int main(){ int i; char a, yesorno; int x = 1; while (x == 1){ cout << "Enter binary number:" << endl; cin >> i; a = convert_to_hex( i ); cout << a << endl; cout << "Would you like to continue? (y/n)" << endl; cin >> yesorno; if (yesorno == 'y'){ x = 1; } else if (yesorno == 'n'){ x = 0; } } return 0; }; [code]``````
[/code]
Last edited on
What happens if x in your function is none of the values you specified?
It would not work, and I understand that the user must enter either 0000, 0001....1110,or 1111. I just don't understand why I'm getting the run time error, or how to remedy it.
Follow your function with you pass a value not in there and you should see why you are getting that problem.
 ``12345678`` ``````if x == 0 if x == 8 if x == 9 if x == 64 if x == 72 if x ==73 if x == 1000 //Note that there is no leading zero ...``````

2) you can't store leading zeroes in int. You can use strings to store input if you need them.

When you enter anything from 0001 to 0111 it doesn't matches anything and b is not assigned. Make another last else, which will throw an eror or something, so when user enters something inappropriate, it tells him.
Last edited on
Thank you for the help! I have one more issue. Because I'm now using string for inputting the binary numbers, I've gone from cin >> i to getline(cin, i). The conversion from binary to hex is working, however the getline is only working once in the loop. It is being skipped after the initial binary input. How can I resolve this? Thanks again.
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586`` ``````#include #include using namespace std; string convert_to_hex(string x ){ string b; if (x == "0000") b = '0'; else if (x == "0001") b = '1'; else if (x == "0010") b = '2'; else if (x == "0011") b = '3'; else if (x == "0100") b = '4'; else if (x == "0101") b = '5'; else if (x == "0110") b = '6'; else if (x == "0111") b = '7'; else if (x == "1000") b = '8'; else if (x == "1001") b = '9'; else if (x == "1010") b = 'A'; else if (x == "1011") b = 'B'; else if (x == "1100") b = 'C'; else if (x == "1101") b = 'D'; else if (x == "1110") b = 'E'; else if (x == "1111") b = 'F'; return b; } int main(){ string i,a; char yesorno; int x = 1; while (x == 1){ cout << "Enter binary number:" << endl; getline(cin, i); a = convert_to_hex( i ); cout << a << endl; cout << "Would you like to continue? (y/n)" << endl; cin >> yesorno; if (yesorno == 'y'){ x = 1; } else if (yesorno == 'n'){ x = 0; } } return 0; };``````
I'll take a look at this later ( it's NewYear' Eve).

However, I recommend that you get rid of all those ELSE IF's.
and convert them into a single line which compares the value of "X" to a true 0x0100 value.

btw, in main(), you set "X" to 1, then check it in the While, and reset it to 1 each time "Y" is typed.

You can drop out the else if (again) by only checking in the IF whether the typed response is "N", then set it to anything other than 1.

btw, if I were to ask you whether the vars i,a,b,x, were INT's or CHAR's you may have to check. I suggest that var names include their Type and Reason.

That way you won't assign "x" as an Int in main() and assign it as a String elsewhere - as you've done here.

Insert a cin.ignore() after line 77, to remove the newline from the input buffer:
 ``7778`` `````` cin >> yesorno; cin.ignore(1000,'\n'); ``````

And, for amusement purposes, an alternative version of the function convert_to_hex(). Should work for up to 31 bits.
 ``123456789`` ``````#include string convert_to_hex(string x ) { char * endptr = 0; int num = strtol(x.c_str(), &endptr, 2); // convert binary string to int ostringstream oss; oss << hex << uppercase << num; // convert int to hex return oss.str(); }``````

Last edited on
Thanks for the help. The only question I have now is what exactly you mean by "remove from input buffer."
The input buffer is a block of memory set aside to store the keystrokes until the program is ready for them.

Consider this code:
 ``1234`` `````` char ch; cout << "Enter a character: " << endl; cin >> ch; cout << "Char was:" << ch << endl;``````

What is it that you actually enter from the keyboard?
If you just press 'enter', nothing really happens, the program just waits for more input. If you just press any character, again nothing happens. You may in fact type several characters. All of them are stored in the buffer. Then you press 'enter'.

Now the program processes the buffer. The first non-whitespace character is assigned to the variable ch. Anything else you typed, including pressing the 'enter' key, remains in the buffer. Thus there may now be several characters waiting in the input buffer. At the minimum, the newline character '\n' will be in the buffer.

At the next operation using cin, the characters from the buffer will be received first, before anything else which is typed.

What actually happens depends on the type of operation. Some operations ignore leading whitespace (such as space, tab or newline) but the getline function will accept everything up to the first newline, and the newline itself is discarded.
 ``12345678910`` `````` char ch; string str; cout << "Enter a character: " << endl; cin >> ch; cout << "Enter a string: " << endl; getline(cin, str); cout << "Char was:" << ch << endl; cout << "\String was:" << str << endl;``````

input: "a\n"
output:
 ```Enter a character: a Enter a string: Char was:a String was:```

Try entering several characters. Input: "asdfg\n"
Output:
 ```Enter a character: asdfg Enter a string: Char was:a String was:sdfg```

In the first case, the string is empty, "". In the second it is "sdfg".
But in neither case does the user get the opportunity to actually type anything. That's because of the characters, including the newline which were already in the buffer as a result of the previous operation.

The solution is to use ignore() to remove anything from the buffer before attempting the getline() operation.
I tend to use something like `cin.ignore(1000,'\n');` which will remove up to 1000 characters, or until the newline '\n' is reached and removed.

To ignore an unlimited number of characters, use
#include <limits>
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
Wow! Great explanation, I understand this much better. Thank you.
Topic archived. No new replies allowed.

You are using a version without Ads of this website. Please, consider donating: