8 Replies - 1108 Views - Last Post: 05 September 2012 - 09:16 AM Rate Topic: -----

#1 time4f5  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 57
  • Joined: 02-December 11

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.
/* 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;

}



Is This A Good Question/Topic? 0
  • +

Replies To: String to int convert without casting

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: String to int convert without casting

Posted 05 September 2012 - 08:17 AM

In the following snippet:
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
Was This Post Helpful? 1
  • +
  • -

#3 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1629
  • View blog
  • Posts: 3,092
  • Joined: 30-May 10

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?
Was This Post Helpful? 1
  • +
  • -

#4 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 651
  • View blog
  • Posts: 2,234
  • Joined: 31-December 10

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

Was This Post Helpful? 1
  • +
  • -

#5 time4f5  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 57
  • Joined: 02-December 11

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;

}


Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

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
Was This Post Helpful? 0
  • +
  • -

#7 time4f5  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 57
  • Joined: 02-December 11

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.

This post has been edited by time4f5: 05 September 2012 - 09:03 AM

Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

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.
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
Was This Post Helpful? 1
  • +
  • -

#9 time4f5  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 57
  • Joined: 02-December 11

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1