CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    May 2009
    Posts
    9

    Unhappy Convert int to string with criteria

    Hello all,

    Hoping for some help with this. Yes, it is homework, but as you can see below, I have my sorry attempt at coding it for your review and laughter. The code compiles but when the program runs and I enter a sample integer it doesn't do the conversion and print it on screen.

    Any advice would be helpful. P.S. This is in C++ I just tend to use the C comment braces since I took that language previously. Thanks!!!!!

    Here's the question:

    Write a program using a function and a series of if…else statements. The user should be prompted to enter a number in main. The
    function is called and the number entered is passed to the function. This function parameter will then be used in a series of if…else
    statements to discover and print out the number word within the function (for example, 1=one, 2=two, 3=three, etc.).

    Enter a number: 1
    You entered the number one.

    You may choose to limit the number entered by the user to a particular range. Be sure to prompt the user with this range, so you can
    deal with correct and incorrect numbers entered.

    Here's my attempted answer:
    Code:
    /* Pseudocode:
    
    prompt user to enter a number between 1 and 10
    input the number
    if number <1 or >10 print "Your number is outside the range."
    call function
    if number == 1 then print "one"
    else if number == 2 then print "two"
    etc.
    
    */
    
    #include <iostream>
    #include <string>
    using std::string;
    using std::cout;
    using std::cin;
    using std::endl;
    using namespace std;
    
    
    string result;
    string one, two, three, four, five, six, seven, eight, nine, ten;
    int number = 0;
    
    string wordconvert (int);
    
    
    int main()
    {
    
    cout << "Enter a number between 1 and 10: "; /* prompt for input */
    cin >> number; /* input number */
    
    if (number < 1 or number > 10)
    	{
     	cout<< "Your number is outside the range."; /* check for incorrect entries */
    	}
    else 
    	{
    	result = wordconvert (number); /* call function */
    	}
    
    cout << "\nYou entered the number: " << result << endl; /* print word version of number entered by user */
    
    return 0;
    
    }
    
    
    string wordconvert (int number) /* start function */
    
    {
    
    	if (number == 1)
    		{
    		result = one;
    		}
    	else if (number == 2)
    		{
    		result = two;
    		}
    	else if (number == 3)
    		{
    		result = three;
    		}
    	else if (number == 4)
    		{
    		result = four;
    		}
    	else if (number == 5)
    		{
    		result = five;
    		}
    	else if (number == 6)
    		{
    		result = six;
    		}
    	else if (number == 7)
    		{
    		result = seven;
    		}
    	else if (number == 8)
    		{
    		result = eight;
    		}
    	else if (number == 9)
    		{
    		result = nine;
    		}
    	else if (number == 10)
    		{
    		result = ten;
    		}
    
    return (result);
    }
    Last edited by Viking Power; May 15th, 2009 at 11:48 AM. Reason: Show code properly.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Convert int to string with criteria

    The problem seems to be that you are creating several variables named one, two, ..., ten, but the aim is to print the relevant text "one", "two", ... "ten".

    This is what I suggest: get rid of those variables named one, two, ... ten. Turn your global variables result and number into local variables of the global main function. Then, in wordconvert(), return the appropriate strings depending on the input.

    By the way, please post code in [code][/code] bbcode tags.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    May 2009
    Posts
    9

    Re: Convert int to string with criteria

    Posted my code properly now. Sorry about that! Should have known better!

    I have tried the code the way that you suggested and it then wouldn't compile and spit back at me that it was unable to convert int to string as a return value of the function.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Convert int to string with criteria

    Quote Originally Posted by Viking Power
    I have tried the code the way that you suggested and it then wouldn't compile and spit back at me that it was unable to convert int to string as a return value of the function.
    What did you try?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    May 2009
    Posts
    9

    Re: Convert int to string with criteria

    I tweaked it some more, see below. What the compiler is now telling me is "error: ten was not declared in this scope." etc. for all my functions variables.

    Code:
    #include <iostream>
    #include <string>
    using std::string;
    using std::cout;
    using std::cin;
    using std::endl;
    using namespace std;
    
    string wordconvert (int);
    
    int main()
    {
    	string result;
    	int number = 0;
    	cout << "Enter a number between 1 and 10: "; /* prompt for input */
    	cin >> number; /* input number */
    	if (number < 1 or number > 10)
    	{
    		cout << "Your number is outside the range."; /* check for incorrect entries */
    	}
    	else
    	{
    		result = wordconvert (number); /* call function */
    	}
    	cout << "\nYou entered the number: " << result << endl; /* print word version of number entered by user */
    	return 0;
    }
    
    string wordconvert (int number) /* start function */
    {
    	if (number == 1)
    	{
    		result = one;
    	}
    	else if (number == 2)
    	{
    		result = two;
    	}
    	else if (number == 3)
    	{
    		result = three;
    	}
    	else if (number == 4)
    	{
    		result = four;
    	}
    	else if (number == 5)
    	{
    		result = five;
    	}
    	else if (number == 6)
    	{
    		result = six;
    	}
    	else if (number == 7)
    	{
    		result = seven;
    	}
    	else if (number == 8)
    	{
    		result = eight;
    	}
    	else if (number == 9)
    	{
    		result = nine;
    	}
    	else if (number == 10)
    	{
    		result = ten;
    	}
    	return (result);
    }

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Convert int to string with criteria

    The compiler is right. There is no variable with the name of one, etc. I don't really see why there should be either. If you're just trying to assign a value to a string variable, just say result = "one"; etc.

  7. #7
    Join Date
    May 2009
    Posts
    9

    Re: Convert int to string with criteria

    Quote Originally Posted by GCDEF View Post
    The compiler is right. There is no variable with the name of one, etc. I don't really see why there should be either. If you're just trying to assign a value to a string variable, just say result = "one"; etc.
    Are you telling me I just need to add quotes?! AAAAAARRRRGGGGHHHHH!!!!! If that's the case it definitely explains my love-hate relationship with programming!

  8. #8
    Join Date
    May 2009
    Posts
    9

    Re: Convert int to string with criteria

    Okay, tried the quotes and also added string result; in the function. Now the program compiles, however, when it prompts user for a number and a number is entered, NOTHING happens other than the screen telling me to hit enter to exit the program........Here's where I'm at now:

    Code:
    /* 
    
    Pseudocode:
    prompt user to enter a number between 1 and 10
    input the number
    call function
    if number <1 or >10 print "Your number is outside the range."
    if number == 1 then print "one"
    else if number == 2 then print "two"
    etc.
    
    */
    
    
    #include <iostream>
    #include <string>
    using std::string;
    using std::cout;
    using std::cin;
    using std::endl;
    using namespace std;
    
    string wordconvert (int);
    
    int main()
    {
    	string result;
    	int number = 0;
    	cout << "Enter a number between 1 and 10: "; /* prompt for input */
    	cin >> number; /* input number */
    	if (number < 1 or number > 10)
    	{
    		cout << "Your number is outside the range."; /* check for incorrect entries */
    	}
    	else
    	{
    		result = wordconvert (number); /* call function */
    	}
    	cout << "\nYou entered the number: " << result << endl; /* print word version of number entered by user */
    	return 0;
    }
    
    string wordconvert (int number) /* start function */
    {
    	string result;
    	
    	if (number == 1)
    	{
    		result = "one";
    	}
    	else if (number == 2)
    	{
    		result = "two";
    	}
    	else if (number == 3)
    	{
    		result = "three";
    	}
    	else if (number == 4)
    	{
    		result = "four";
    	}
    	else if (number == 5)
    	{
    		result = "five";
    	}
    	else if (number == 6)
    	{
    		result = "six";
    	}
    	else if (number == 7)
    	{
    		result = "seven";
    	}
    	else if (number == 8)
    	{
    		result = "eight";
    	}
    	else if (number == 9)
    	{
    		result = "nine";
    	}
    	else if (number == 10)
    	{
    		result = "ten";
    	}
    	return (result);
    }

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Convert int to string with criteria

    Quote Originally Posted by Viking Power
    Now the program compiles, however, when it prompts user for a number and a number is entered, NOTHING happens other than the screen telling me to hit enter to exit the program.
    That is weird. I tested your program and it works (at least when the input is within the given range).
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  10. #10
    Join Date
    May 2009
    Posts
    9

    Re: Convert int to string with criteria

    Would it be a compiler specific issue? Does that ever happen? I'm using Quincy 2005.

  11. #11
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Convert int to string with criteria

    Quote Originally Posted by Viking Power
    Would it be a compiler specific issue? Does that ever happen? I'm using Quincy 2005.
    Quincy 2005 is just an IDE, not a compiler, if I remember correctly. But if this is compiler specific, the compiler must be so buggy as to be unusable. At a glance, it is obvious that at the very least "You entered the number:" will be printed.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  12. #12
    Join Date
    May 2009
    Posts
    9

    Re: Convert int to string with criteria

    It is printing "You entered the number:" Just not the text version of entered number. I mis-typed when I stated earlier that it was just kicking the user out of the program.


    On whether or not Quincy is a compiler, I guess I meant that it includes a compiler. Isn't that what minGW is?

    Sorry if I come off as not knowing much. I am definitely a beginning programmer!
    Last edited by Viking Power; May 15th, 2009 at 02:13 PM.

  13. #13
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Convert int to string with criteria

    Quote Originally Posted by Viking Power
    It is printing "You entered the number:" Just not the text version of entered number. I mis-typed when I stated earlier that it was just kicking the user out of the program.
    Did you enter a number in the range [1, 10]? If not, then that is what I expect, since the wordconvert function would not be called (i.e., your handling of the out of range input is not complete).

    Quote Originally Posted by Viking Power
    On whether or not Quincy is a compiler, I guess I meant that it includes a compiler. Isn't that what minGW is?
    Yes, the MinGW port of g++ is a compiler.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  14. #14
    Join Date
    May 2009
    Posts
    9

    Re: Convert int to string with criteria

    Okay, still can't get this dang program to work. It is now working perfectly other than the fact that it is simply NOT outputting the users entry like it should.

    Any advice would be greatly appreciated!

    Here's the code:

    Code:
    #include <iostream>
    #include <string>
    using std::string;
    using std::cout;
    using std::cin;
    using std::endl;
    using namespace std;
    
    
    string result;
    string one, two, three, four, five, six, seven, eight, nine, ten;
    int number = 0;
    
    string wordconvert (int);
    
    
    int main()
    {
    
    cout << "Enter a number between 1 and 10: "; /* prompt for input */
    cin >> number; /* input number */
    
    if (number < 1 )
    	{
     	cout<< "Your number is outside the range."; /* check for incorrect entries */
    	}
    else if (number > 10)
    	{
    	cout<< "Your number is outside the range."; /* check for incorrect entries */
    	}
    else
    	{
    	result = wordconvert (number); /* call function */
    
    	cout << "\nYou entered the number: " << wordconvert << endl; /* print word version of number entered by user */
    	}
    return 0;
    
    }
    
    
    string wordconvert (int number) /* start function */
    
    {
    
    	if (number == 1)
    		{
    		result =  "one";
    		}
    	else if (number == 2)
    		{
    		result = "two";
    		}
    	else if (number == 3)
    		{
    		result = "three";
    		}
    	else if (number == 4)
    		{
    		result = "four";
    		}
    	else if (number == 5)
    		{
    		result = "five";
    		}
    	else if (number == 6)
    		{
    		result = "six";
    		}
    	else if (number == 7)
    		{
    		result = "seven";
    		}
    	else if (number == 8)
    		{
    		result = "eight";
    		}
    	else if (number == 9)
    		{
    		result = "nine";
    		}
    	else (number == 10);
    		{
    		result = "ten";
    		}
    
    return result;
    }
    Last edited by Viking Power; May 23rd, 2009 at 06:08 PM.

  15. #15
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Convert int to string with criteria

    Two problems:

    1) You are outputting the address of "wordconvert" , not the result.

    2) In your wordconvert function .. compare the section for 10 with
    the other sections.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured