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?
#include <iostream>
#include <cmath>
usingnamespace 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;
}
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
@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 :/
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).
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.
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).
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.
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.
#include <iostream>
#include <string>
usingnamespace 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).