Hey all,
I just started learning C++ about a week ago. The program is supposed to print a word equivalent whenever the user enters a number using a switch. Regardless of what the user enters (character, symbol, string) the result prints a blank space.
I'm pretty sure that the problem is somewhere with my numberToWord function because I tried messing with the main section and am able to get strings to print in place of that function. What am I missing here?
#include <iostream>
#include <string>
usingnamespace std;
//Variable Declarations
int numberInput; //Number that user chooses
//Switch function
string numberToWord(int number)
{
string theWord; //Declares theWord as a variable
int switchloop = 0; //creates switchloop variable used to exit while loop
//Initiate switch loop
while ( switchloop = 0 ) //Causes switch to repeat until valid input is received.
{
switch ( number ) //uses number variable to change theWord to the associated word.
{
case'0' :
theWord = "Zero.";
++switchloop; //Breaks from while loop
break;
case'1' :
theWord = "One.";
++switchloop; //Breaks from while loop
break;
case'2' :
theWord = "Two.";
++switchloop; //Breaks from while loop
break;
case'3' :
theWord = "Three.";
++switchloop; //Breaks from while loop
break;
case'4' :
theWord = "Four.";
++switchloop; //Breaks from while loop
break;
case'5' :
theWord = "Five.";
++switchloop; //Breaks from while loop
break;
case'6' :
theWord = "Six.";
++switchloop; //Breaks from while loop
break;
case'7' :
theWord = "Seven.";
++switchloop; //Breaks from while loop
break;
case'8' :
theWord = "Eight.";
++switchloop; //Breaks from while loop
break;
case'9' :
theWord = "Nine.";
++switchloop; //Breaks from while loop
break;
default : //Used to provide an output for invalid inputs.
cout << "Invalid input. Choose an integer between 0 and 9: ";//Since switchloop=0 switch will repeat
cin >> number; //allowing for a correct input to be read in
break;
}
}
return theWord; //Returns theWord to the numberToWord function.
}
//Initialize main function.
int main()
{
cout << "Choose an integer between 0 and 9: "; //Prompts user number choice
cin >> numberInput; //reads the choice into numberInput
cout << "\nYou entered the number "; //Prints result of numberToWord
cout << numberToWord(numberInput) << endl;
return 0;
}
Thats awesome. I really do have a lot more to learn. Thank you so much for the help. I will be able to make this work, but for the sake of my own understanding, what went wrong with my code?
I really can't just take an answer I need to know what I didn't do right or I'll make the same mistake again somewhere else down the line.
In string numberToWord(int number), number is an integer. 1, 2, 3 etc. are integers; '1', '2', '3' etc. are characters.
In particular, 1 is not equal to '1'.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
int main()
{
constint an_int = 7 ;
constchar a_char = '7' ;
std::cout << an_int << ' ' << a_char << '\n' // 7 7
<< an_int << ' ' << int(a_char) << '\n' // 7 <some implementation defined value;
// the integeral value of the character '7' on this implementation>
<< an_int << ' ' << ( a_char - '0' ) << '\n' ; // 7 7 ( the value of each character after '0' in the list of decimal digits
// is one greater than the value of the previous character)
// '5' - '0' == 5, '8' - '0' == 8 etc.
}
char numberInput; //numberInput should be a char type instead of an int type.
string numberToWord(char number) //number should be a char type instead of an int type.
{
...
while ( switchloop == 0 ) //comparison sign "==" instead of assignment sign "="
{
...