-
Using an int or char
Ok,
This is my problem, here is the question to better understand "Enter the length of a rectangle or enter 'x' to exit" (when you exit you return to an earlier function, but that is not important). When the user enters a value for the square I need to use it for a mathmatical operations, obviously the int will not accept a char, but if I put a number in a char it will not work because a char has its own numerical value.
Matt
P.S. Thanks for the help with my "Stuck in a loop" problem.
-
A char is simply a smaller int, you can use an int like a char provided the value is consistent with the character set that you use. With appropriate casting, an int can be used as both an int and a char.
-Prelude
-
One option is to read the input as a string, then convert it to an int using atoi().
Code:
char str[15];
int length;
cout << "Enter the length of a rectangle (or 'x' to exit):";
cin >> str;
if (str[0] == 'x') return 0;
length = atoi(str);
cout << "length:" << length << endl;