Seperating/Storing Parts of String

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 :)


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
31
32
33
34
35
36
37
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <cstdlib>
#include <string> 
using namespace std; 

//Global struct   
struct component
 {
   int mant;                
   double exp;                          
 };

//Function Prototypes
void readCharacters(component& function)                    //OUT:Data destination
 {
     string scientificNotation;
     
     cout << "\t\t\tScientific Notation Calculator\n\n";
     cout << "Please enter a number in scientific notation.\n";
     cout << "For Example: -0.1234E20\n";
     getline(cin, scientificNotation, 'E' && 'e');
     cin >> function.mant;
     cout << function.mant;
     cin >> function.exp;
     cout << function.exp;
}
 
 int main ()
 {
 component function;

     readCharacters(function);
     return 0;
 }
Look up the string function c_str, then look up the cstring function strtok. And its probably time to start understanding namespaces.




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;.
Last edited on
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?

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
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <cstdlib>
#include <string> 
using namespace std; 

//Global struct   
struct component
 {
   int mant;                
   double exp;                          
 };

//Function Prototypes
void readCharacters(component& function)                    //OUT:Data destination
 {
     string scientificNotation;

     cout << "\t\t\tScientific Notation Calculator\n\n";
     cout << "Please enter a number in scientific notation.\n";
     cout << "For Example: -0.1234E20\n";
     getline(cin, scientificNotation, 'E');
     function.mant = cin.get();
     cout << "The mantissa is " << function.mant << '\n';
     cin.ignore('E' && 'e');
     cin >> function.exp;
     cout << function.exp;
}
 
 int main ()
 {
 component function;

     readCharacters(function);
     system("pause");
     return 0;
 }
 
Aww, but you changed my original code.

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;
Does that help?
Topic archived. No new replies allowed.