Problem with this Str-to-Int?

This program is to convert a string into an integer. However, it says that on line 20 (in the for loop), the iChangedNumber is uninitialized. What does that mean?

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
#include <iostream>
#include <cmath>
using namespace std;
#include <string>

int main()
{
	string Number;
	int pause;
	float iChangedNumber;
	int length = Number.length();
	cout << "Please enter a number to be changed into an integer, please." << endl;
	cin >> Number;

	
	for (int i = 0; i <= length-1;i++)
	{
		char b = Number[i];
		int a = static_cast<int>('b')-48;
		iChangedNumber = a * pow(static_cast<float>(10),(length-1-i))+iChangedNumber;
	}
		

	cout << "The number in the string that was changed into an integer was " << iChangedNumber << endl;

	cin >> pause;
	return 0;
}
Last edited on
because on line 10 IChangedNumber is uninitialized aka you never set an initial value and then you are trying to add that to the current uninitialzed value. That is like having a mystery number of apples and trying to add another mystery number of apples to it.
It means that you are using iChangedNumber without having initialized it. It has a random value, although in some cases that value happens to be 0 -- depending on the phases of the moon and the optimization options.

iChangedNumber = a * pow(static_cast<float>(10),(length-1-i))+iChangedNumber;

The second time iChangedNumber appears in the above code, what value does it have when the for() loop begins?

float iChangedNumber = 0.0f; // initialization

Also, your task would be much easier if you used an std::stringstream to "extract" the number from the std::string instead of doing it yourself.
Thanks guys. The program worked. by the way, I need a bit more help, on line 20, my teacher says that the "pow(static_cast<float>(10),(length-1-i))" can be simplified. But I'm not sure how.
change your double to a float since pow returns a double and then you don't need to static cast. Also line 16 the common way to do for loops is < value not <= value -1 try
 
for( int i = 0; i < 10; ++i )

versus
 
for( int i = 0; i <= 10 - 1; ++i )
Last edited on
You could simplify the program by removing #include <cmath> , and not using pow() at all.
closed account (3qX21hU5)
You could simplify even more by using stringstreams.

Or better yet std::stoi(Your string here); but that would defeat the whole point of the assignment :(
Last edited on
@giblit I'm not sure how to change the double into a float since we didn't even learn pow (had to research it on my own)
@Chervil same as above
@Zereo I never learned about stringstreams nor didn't even understand them after reading about them :/
@Zereo I never learned about stringstreams nor didn't even understand them after reading about them :/

Do you know what a string is? It's a collection of characters.

Do you know what a stream is? It's cout, cin, and other files. What are streams used for? Extracting and inserting information.

So what is a stringstream? It's a way to extract information from a string. Or to insert information.

Example:
http://ideone.com/QTkk7S

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <sstream>
#include <string>
 
int main()
{
    std::string s;
 
    std::cout << "Input a number: ";
    std::getline(std::cin, s);

    // construct string stream from existing string data
    std::stringstream ss(s);
    int n;
 
    ss >> n;
    std::cout << "Number was: " << n << '\n';
}
Input a number: -6223 abracadabra
Number was: -6223


Edit: copied example from Ideone.com just in case...
Last edited on
And a second example showing how data can be inserted to construct a new string.

std::stringstream is like a combination of std::cout and std::cin, but which helps you extract data from strings, or to create new strings.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <sstream>
#include <string>

int main()
{
    std::stringstream ss; // empty string stream

    ss << "Hello";
    ss << ',';
    ss << " World of " << 2013 << '!';

    const std::string s = ss.str();

    std::cout << s << '\n';
}
Hello, World of 2013!
Last edited on
Hmph, I don't believe using a stringstream is what is needed in this assignment. Now I need to figure out how to convert a double into a float.
Nevertheless, doing this without using pow() is simpler, requires less code and probably executes faster (not that it's a major concern). I would post the code for you but I think you would learn more by trying to do it for yourself.

converting from double to float - well it isn't particularly necessary, but if you want to do so, just put for example
1
2
double a = 1234.56; 
float b = a;

but I would just use double throughout and forget about float.

(or on the original idea, don't use either float or double, just use int throughout).
Last edited on
closed account (3qX21hU5)
Floats and double serve the same purpose except double has 2x the precision then a float. I am not sure why you would want to do that or why it matters in your program.
Last edited on
I meant change the float to double was a mistype.
closed account (3qX21hU5)
Changing the float to double has nothing to do with it either (Heck it shouldn't even be a decimal point type in the first place). The only thing you gain by changing a float to double is 15-16 decimal digits vs 7 decimal digits in a float.

This problem should be using integers instead if he wants to go his route and not use stringstreams.

Personally I would go with stringstreams or even use the new C++11 functions provided to solve the problem easier but sometimes professors can be a pain in the ass in how they want things done (Or so I hear).

Last edited on
Rather than using a stringstream, just change this:
string Number;
to this:
int Number;

Seriously though, the purpose of doing things the long way is to gain some understanding of the internal workings of the built-in library functions. Taking too many shortcuts undermines that goal, making the exercise pointless.
closed account (3qX21hU5)
That is true, and I totally agree that it is needed to learn the inner working of the language, though most of the time I feel like that is all they are teaching is the inner working of the language. I don't know maybe I have a different approach to it (Teach the easy way first to get the student familiar with the subject then most onto dissecting how it works) but that is off topic and I will leave it there.
Thanks everyone, you really helped me out. I discovered what I needed to change in the code. This is the final product:

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
#include <iostream>
#include <string>
using namespace std;

int main()
{
	int pause;
	string Number;
	int k;
	int multiplier=1;
	int iChangedNumber = 0;
	cout << "Please enter a number to be changed into an integer, please." << endl;
	cin >> Number;
	int length = Number.length();

	
	for (k = length-1; k >=0;k--)
	{
		char d = Number[1];
		int f = (static_cast<int>(Number[k])-48)*multiplier;
		iChangedNumber += f;
		multiplier = multiplier * 10;
	}
		

	cout << "The number in the string that was changed into an integer was " << iChangedNumber << endl;

	cin >> pause;
	return 0;
}

Now I only need some help on the Luhn Algorithm one. That's a pin in the butt (especially how my professor wants it).
What about this:
1
2
3
4
5
6
7
8
    int iChangedNumber = 0;
    int length = Number.length();

    for (int k=0; k < length; k++)
    {
        iChangedNumber *= 10;
        iChangedNumber += Number[k] - '0';
    }
Topic archived. No new replies allowed.