/* Write a program to convert a character representation of a non-negative number to an integer value without using the string-to-integer conversions available in some languages (you must do a character-by-character conversion). Use a separate method to do the actual conversion. */ #include <iostream> #include <string> using namespace std; int convert(string c); //convert function int main() { string c; int number; cout << "Enter a string to convert:" << endl; cin >> c; int convert(string c); cout << number << endl; system ("PAUSE"); return 0; }// main /****************** convertion ************************************/ int convert(string c, int number) { int index, integer_digit, current_power_of_ten; char current_digit; current_power_of_ten = 1; // initialize for (index = 0; index <c.length(); ++index) //step through the string { current_digit=c[c.length()-index]; integer_digit=current_digit -'0'; number = number + integer_digit * current_power_of_ten; current_power_of_ten *=10; }//for return number; }
String to int convert without casting
Page 1 of 18 Replies - 2220 Views - Last Post: 05 September 2012 - 09:16 AM
#1
String to int convert without casting
Posted 05 September 2012 - 07:54 AM
this is a school project. I have already turned it in, but it doesn't work and the next project builds on this. It compiles, but I get 'number' not initialized error and the value of number is -804.... regardless of the string I input.
Replies To: String to int convert without casting
#2
Re: String to int convert without casting
Posted 05 September 2012 - 08:17 AM
In the following snippet:
You never assign a value to number, also int convert(string c); is a function prototype not, a function call. You may want to study the tutorials in my signature, they should help explain how to properly use functions.
Jim
int main() { string c; int number; cout << "Enter a string to convert:" << endl; cin >> c; int convert(string c); cout << number << endl;
You never assign a value to number, also int convert(string c); is a function prototype not, a function call. You may want to study the tutorials in my signature, they should help explain how to properly use functions.
Jim
#3
Re: String to int convert without casting
Posted 05 September 2012 - 08:20 AM
> 10 int convert(string c); //convert function
This does not match
> 28 int convert(string c, int number)
> 40 number = number + integer_digit * current_power_of_ten;
What is the initial value of number?
This does not match
> 28 int convert(string c, int number)
> 40 number = number + integer_digit * current_power_of_ten;
What is the initial value of number?
#4
Re: String to int convert without casting
Posted 05 September 2012 - 08:24 AM
Take a look at the "String conversion" functions at the top of this page: http://cplusplus.com...ibrary/cstdlib/. There is also the stringstream library which can be used to convert strings to numbers, but it's a little more advanced than just using those functions above. If you want to look into stringstreams check here.
This post has been edited by vividexstance: 05 September 2012 - 08:25 AM
#5
Re: String to int convert without casting
Posted 05 September 2012 - 08:48 AM
I would love to use the sstream or the atoi functions, but part of the assignment is to create our own function. I have updated the code with proper function syntax (Thanks Jim) and now I'm getting a runtime error of script out of range. any thoughts? Also, I changed the function and took number out because I'm not really passing that in the function. I also put the output statement in the function.
/* Write a program to convert a character representation of a non-negative number to an integer value without using the string-to-integer conversions available in some languages (you must do a character-by-character conversion). Use a separate method to do the actual conversion. */ #include <iostream> #include <string> using namespace std; int convert(string c); //convert function prototype int main() { string c; cout << "Enter a string to convert:" << endl; cin >> c; convert(c); // call system ("PAUSE"); return 0; }// main /****************** convertion ************************************/ int convert(string c)// function definition { int index, number, integer_digit, current_power_of_ten; char current_digit; current_power_of_ten = 1; // initialize for (index = 0; index <c.length(); ++index) //step through the string { current_digit=c[c.length()-index]; integer_digit=current_digit -'0'; number = number + integer_digit * current_power_of_ten; current_power_of_ten *=10; }//for cout << number << endl; return number; }
#6
Re: String to int convert without casting
Posted 05 September 2012 - 08:58 AM
Post the complete error message exactly as it appears in your development environment. These messages have important information embedded within them to aid in locating and fixing the error.
Jim
Jim
#7
Re: String to int convert without casting
Posted 05 September 2012 - 09:02 AM
Run-Time Check Failure #3 - The variable 'number' is being used without being initialized.
There is another Debug error, but I'm able to 'ignore' it and move on.
There is another Debug error, but I'm able to 'ignore' it and move on.
This post has been edited by time4f5: 05 September 2012 - 09:03 AM
#8
Re: String to int convert without casting
Posted 05 September 2012 - 09:08 AM
In your function you try to use your variable number before you assign it any value.
The first time through your loop number's value is not defined. You need to assign a known value to this variable before you can use it in your calculation.
Jim
number = number + integer_digit * current_power_of_ten;
The first time through your loop number's value is not defined. You need to assign a known value to this variable before you can use it in your calculation.
Jim
#9
Re: String to int convert without casting
Posted 05 September 2012 - 09:16 AM
wow, I can't believe I didn't catch that. good find. that fixed it.
Page 1 of 1