0

I want to know whether I'm using the right form to get my command in a line and then by some ifs get the info each command needs. This is a part of my code; actually, the first part of my main function:

string line;
stringstream ss;

while (!cin.eof())
{
    getline(cin, line);
    //i dont know if next line should be used   
    ss << line;
    if (line.size() == 0)
        continue;

    ss >> command;

    if (command == "put")
    {
         string your_file_ad, destin_ad;
         ss >> your_file_ad >> destin_ad;
         //baraye history ezafe shod
         give_file(your_file_ad, p_online)->index_plus(command);
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
mrlo
  • 11
  • 5

1 Answers1

-1

I tried to run your code with two additional couts in your if, to see what happens when for example user enters put a b.

So, this is my code:

string line;
stringstream ss;
while (true)
{
    getline(cin, line);
    //i dont know if next line should be used   

    ss << line;
    if (line.size() == 0)
        continue;

    string command;
    ss >> command;

    if (command == "put")
    {
        string your_file_ad, destin_ad;
        ss >> your_file_ad >> destin_ad;
        cout << "input #1 is " << your_file_ad << endl;
        cout << "input #2 is " << destin_ad << endl;
    }
}

When I run this code, then if I write put a b in the console, I'll see this result, which is correct:

input #1 is a
input #2 is b

But it seems that this only works for the first command. after that commands couldn't process correctly.

So, I read the code again, and found out that the problem is, you are initializing your stringstream outside of the while.

I'm not sure why exactly it doesn't work (maybe already reached EOF and can't continue reading anymore?), but if you move stringstream ss; inside the while, it'll work correctly:

string line;
while (true)
{
    stringstream ss;

    getline(cin, line);
    //i dont know if next line should be used   

    ss << line;
    if (line.size() == 0)
        continue;

    string command;
    ss >> command;

    if (command == "put")
    {
        string your_file_ad, destin_ad;
        ss >> your_file_ad >> destin_ad;
        cout << "input #1 is " << your_file_ad << endl;
        cout << "input #2 is " << destin_ad << endl;
    }
}

enter image description here

Update: read @LightnessRacesinOrbit comment below about the issue with the first code.

Mahdi Ghiasi
  • 14,873
  • 19
  • 71
  • 119
  • No, you are also using `while (!cin.eof())`. Stop doing that immediately!!! Let me know when you have fixed this bug so I can remove my downvote. – Lightness Races in Orbit Jun 02 '15 at 12:37
  • 1
    And, yes, the problem is the EOF bit being set on the stringstream. You can re-use it if you reset its content and flags, but instead making it scope-bound works too. – Lightness Races in Orbit Jun 02 '15 at 12:37
  • @LightnessRacesinOrbit That is not related to what the question wants, but I'll replace it with a simple `while (true)` anyway :) asker can change it to whatever is necessary based on what he wants. Also, thanks for your comment about EOF. I'll update the answer with more details. – Mahdi Ghiasi Jun 02 '15 at 12:42
  • The OP wants code that works, and advice that is correct. Your answer does not satisfy those criteria as long as you are spreading this misconceptive `while (!cin.eof())` nonsense. Changing it to `while (true)` _does not fix the problem_. It actually shows that right there in your screengrab: one additional loop iteration that you don't want. Read [this](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong), carefully. Then read it again. Then phone up the person who taught you to write `while (!cin.eof())`, and tell _them_ to read it. – Lightness Races in Orbit Jun 02 '15 at 13:33