I'm trying to store different sets of characters of the string scientificNotation. So far, I've captured the values prior to the 'E' and stored them as function.mant. Now, I'm stuck trying to store the values AFTER the E. The only references I can find are all form std::, and I don't understand that sort of code :/. If anybody could point me in the right direction it'd be greatly appreciated :)
In your particular case, can't you just do something like
1 2 3 4 5 6 7 8 9 10 11
void readCharacters(component& function)
{
// I'd actually have these output statements in main(), but whatever
cout << "\t\t\tScientific Notation Calculator\n\n";
cout << "Please enter a number in scientific notation.\n";
cout << "For Example: -0.1234E20\n";
cin >> function.mant; // Stops at the 'E' (hopefully)
cin.ignore(); // This will (hopefully) gobble up the 'E'
cin >> function.exp; // Grabs everything after the 'E'
}
?
(That's just the basic idea -- you'll probably want to do some input validation as well in case the user enters something in the wrong format.)
Also, should the types of mant and exp be switched?
It seems to me that you want double mant; and int exp;, and not int mant; and double exp;.
I don't think so, I wish. I tried it, and it didn't work. Maybe I'm wrong? This function is running but returning garbage....plus it read matissma as the value AFTER the 'E' not before. Any ideas?
Say you enter "-0.1234E20".
Let's walk through your code:
22 23 24 25 26 27 28 29 30 31 32 33
// Input buffer contains: "-0.1234E20\n"
getline(cin, scientificNotation, 'E');
// Now scientificNotation = "-0.1234"
// Input buffer now contains: "20\n"
function.mant = cin.get(); // cin.get() returns the next *character* in the input buffer
// Now function.mantissa = 50, probably (ASCII value of '2')
// Input buffer now contains: "0\n"
cout << "The mantissa is " << function.mant << '\n';
cin.ignore('E' && 'e'); // Same as 'cin.ignore(true);', which might become 'cin.ignore(1)'
// Input buffer contains: "\n" (??)
cin >> function.exp; // Wait, this doesn't stop and prompt you for more input?
cout << function.exp;