Help with homework, splitting strings and assigning variables.

so my homework wants me to take an input that is "x1888.88" and assign the "x" to a char, the "1" to a char, and the "888.88" to a float. anyone know how i can do this? and the "888.88" part could be any length could be a single digit or multiple with a decimal.
Have you talked about std::cin yet?

-Albatross
yes, i might not of been clear enough in my first post. the program will be reading from a text file and in that text file there will be "x1888.88" no spaces or other break characters. so how do i get it to store to separate variables of different types?
Here's the problem statement from the homework directly. http://imgur.com/dxM7txy
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
40
41
42
#include <iostream>
#include <sstream>
#include <string>

int main ()
{
    // 1. read each line from the file into a string with std::getline()
    // http://www.cplusplus.com/reference/string/string/getline/

    // as an wexample, say, a line read from the file is:
    std::string line = "x1888.88 14.56 123456789 xxx yyy zzz" ;

    // 2. create an input string stream which reads from the line
    std::istringstream stm(line) ;

    // 3. read the fields from the input string stream
    char junk ;
    char type ;
    float hours ;
    double salary ;
    long id ;
    std::string fname ;
    std::string mname ;
    std::string lname ;

    if( stm >> junk >> type >> hours >> salary >> id >> fname >> mname >> lname )
    {
        std::cout << "junk (to be thrown away): " << junk << '\n'
                   << "type: " << type << '\n'
                   << "hours: " << hours << '\n'
                   << "salary: " << salary << '\n'
                   << "id: " << id << '\n'
                   << "fname: " << fname << '\n'
                   << "mname: " << mname << '\n'
                   << "lname: " << lname << '\n' ;
        // ...
    }
    else
    {
        // badly formed line
    }
}

http://ideone.com/XEQsmG
Topic archived. No new replies allowed.