-
May 14th, 2009, 10:36 PM
#1
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.
-
May 14th, 2009, 10:44 PM
#2
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.
-
May 15th, 2009, 11:51 AM
#3
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.
-
May 15th, 2009, 11:54 AM
#4
Re: Convert int to string with criteria
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?
-
May 15th, 2009, 12:16 PM
#5
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);
}
-
May 15th, 2009, 12:22 PM
#6
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.
-
May 15th, 2009, 12:35 PM
#7
Re: Convert int to string with criteria
Originally Posted by GCDEF
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!
-
May 15th, 2009, 12:40 PM
#8
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);
}
-
May 15th, 2009, 12:44 PM
#9
Re: Convert int to string with criteria
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).
-
May 15th, 2009, 01:05 PM
#10
Re: Convert int to string with criteria
Would it be a compiler specific issue? Does that ever happen? I'm using Quincy 2005.
-
May 15th, 2009, 01:12 PM
#11
Re: Convert int to string with criteria
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.
-
May 15th, 2009, 01:55 PM
#12
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.
-
May 15th, 2009, 08:22 PM
#13
Re: Convert int to string with criteria
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).
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.
-
May 23rd, 2009, 06:01 PM
#14
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.
-
May 23rd, 2009, 06:30 PM
#15
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|