problem receiving output from user defined function

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
 
#include <iostream>
#include <string>

using namespace 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;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>
#include <cctype>

char get_number()
{
    std::cout << "enter an integer between 0 and 9: "; //Prompts user number choice

    std::string number ;
    std::cin >> number ; // accept the number as a string

    // if exactly one character was entered && that character is a decimal digit,
    // return that character.  http://en.cppreference.com/w/cpp/string/byte/isdigit
    if( number.size() == 1 && std::isdigit( number[0] ) ) return number[0] ;

    std::cout << "invalid input; try again.\n";
    return get_number() ; // try again
}

std::string number_to_word( char number )
{
    switch(number)
    {
        case '0' : return "Zero";
        case '1' : return "One";
        case '2' : return "Two";
        case '3' : return "Three";
        case '4' : return "Four";
        case '5' : return "Five";
        case '6' : return "Six";
        case '7' : return "Seven";
        case '8' : return "Eight";
        case '9' : return "Nine";
        default  : return "invalid";
    }
}

int main()
{
	const char number = get_number() ;
	std::cout << "\nYou entered the number '" << number_to_word(number) << "'.\n";
}
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()
{
    const int an_int = 7 ;
    const char 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.
}

http://coliru.stacked-crooked.com/a/d6b65bd4f0311093
Also, look at line 16 of your original post. = should be ==. = is the assignment operator, == is comparison.
1
2
3
4
5
6
7
8
9
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 "="
	{
		...
Last edited on
@JLBorges
Thank you for explaining it to me. I'm still pretty blown back at how much shorter your program was for you to write.

@dhayden & elaleph
TY very much I would have completely missed that. I am definitely still getting the hang of the nuances of C++.



I appreciate it greatly all.
Case closed.
Once we have learnt about arrays, it can be made even more succinct.

1
2
3
4
5
6
7
8
9
std::string number_to_word( char number )
{
    // ten strings: an 'array of 10 std::string'
    static const std::string number_string[] = { "Zero", "One", "Two", "Three", "Four",
                                                 "Five", "Six", "Seven", "Eight", "Nine" };

    if( std::isdigit(number) ) return number_string[ number - '0' ] ; // '6' - '0' == 6 etc.
    else return "invalid" ;
}
Topic archived. No new replies allowed.