Issue with stringstream

I'm attempting to compile this code to test out certain blocks before I proceed to others, but there seems to be an issue at line 51 with stringstream. I don't know if I'm misusing it, but I get an error on that line when I try to compile:

error: no match for 'operator>>' in 'std::basic_stringstream<char, ...
And so on.

I've tried removing the first instance of stringstream, but then I get the same error message for the next instance.

Here is my code:

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
 #include <iostream>
 #include <cmath>
 #include <string>
 #include <sstream>
 #include <cstdlib>

using namespace std;

 #define PI 3.14159

 struct placestats {
    string place;
    float latitude;
    float elevation;
    };


void newmodel (float& nradius, float& nperiod)
{
    cout << endl << "PLANET MODEL REDEFINITION" << endl << "Enter planet's radius: ";
    cin >> nradius;
    cout << endl << "Enter period of rotation: ";
    cin >> nperiod;
}

int main()
{
    char yn; //used for y/n questions
    float radius = 6378137; //default (earth) radius in meters. Can be changed in newmodel().
    float period = 86400; //default rotational period (length of "day") in seconds. Can be changed in newmodel().
    bool defbreak = false;
    int defcount = 1;
    string hold;
    placestats loco[99];

    cout << "***WHEN THE EARTH STOPPED SPINNING***";
    cout << "\nPlease use meters for length, seconds for time, and degrees for latitude.\nAssume a spherical planet.";
    cout << "\n\nUse earth model? (y/n) " << endl;
    cin >> yn;

    if ((yn == 'N') || (yn == 'n')) //Used for redefining earth model.
        newmodel(radius, period);
    cout << "\nEnter the information about the locations you would like to test.\nType \"end\" in name entry to continue to results.\n";
    while (defbreak == false)
    {
        cout << endl <<"LOCATION #" << defcount << endl;
        cout << "Enter name: ";
        getline (cin,hold);
        if (hold != "end" || hold != "End" || hold != "END")
        {
            stringstream(hold) >> loco[defcount].place;
            cout << "\nEnter latitude (degrees): ";
            getline (cin,hold);
            stringstream >> loco[defcount].latitude;
            cout << "\nEnter elevation (meters): ";
            getline (cin,hold);
            stringstream >> loco[defcount].elevation;
            defcount++;
        }

        else
            defbreak = true;
    }
    return 0;
}


Thank you in advance.
From the looks of it (correct me if I am erroneous), you are using the '>>' operator on a temporary object. The temporary object will not retain the value, so attempting to "extract" the information within the object stream and assign it to "loco[defcount].latitude" will do nothing. I've altered your code and posted it below. Does my answer satisfy you?

Quick question: Why are the numbers entered by the user being stored in a std::string object?

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
 #include <cmath>
 #include <string>
 #include <sstream>
 #include <cstdlib>
 
using namespace std;
 
 #define PI 3.14159
 
 struct placestats {
    string place;
    float latitude;
    float elevation;
    };
 
 
void newmodel (float& nradius, float& nperiod)
{
    cout << endl << "PLANET MODEL REDEFINITION" << endl << "Enter planet's radius: ";
    cin >> nradius;
    cout << endl << "Enter period of rotation: ";
    cin >> nperiod;
}
 
int main()
{
    char yn; //used for y/n questions
    float radius = 6378137; //default (earth) radius in meters. Can be changed in newmodel().
    float period = 86400; //default rotational period (length of "day") in seconds. Can be changed in newmodel().
    bool defbreak = false;
    int defcount = 1;
    string hold;
    placestats loco[99];
 
    cout << "***WHEN THE EARTH STOPPED SPINNING***";
    cout << "\nPlease use meters for length, seconds for time, and degrees for latitude.\nAssume a spherical planet.";
    cout << "\n\nUse earth model? (y/n) " << endl;
    cin >> yn;
 
    if ((yn == 'N') || (yn == 'n')) //Used for redefining earth model.
        newmodel(radius, period);
    cout << "\nEnter the information about the locations you would like to test.\nType \"end\" in name entry to continue to results.\n";
    while (defbreak == false)
    {
        cout << endl <<"LOCATION #" << defcount << endl;
        cout << "Enter name: ";
        getline (cin,hold);
        if (hold != "end" || hold != "End" || hold != "END")
        {
            stringstream ss(hold);
            ss >> loco[defcount].place;
            cout << "\nEnter latitude (degrees): ";
            getline (cin,hold);
            ss >> loco[defcount].latitude;
            cout << "\nEnter elevation (meters): ";
            getline (cin,hold);
            ss >> loco[defcount].elevation;
            defcount++;
        }
 
        else
            defbreak = true;
    }
    return 0;
}
Last edited on
Yes, this solves the problem, thank you!

In response to your question, there's no particular reason I'm storing the numbers in such a way. In fact, now that you point it out, I think I'll change it.
Topic archived. No new replies allowed.