Thread: stringstream Help...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    7

    Question stringstream Help...

    Hello all-

    I need to take the first few lines on standard input and print it out. However, I need to check them against some conditions. I have stored the first line in a string vector and then converted to a string stream in preparation for the second line. The following fails:
    Code:
    n.clear();
    while(getline(cin,n)) {
    v.push_back(n);
    if(v[0] != "P2"){
    exit(1); //Works up to this point
    }
    
    stringstream ss(n);
    ss >> a;
    iv.push_back(n);
    if(a < 0)
    exit(1);
    }
    This fails. Some help would be appreciated.
    Last edited by neogst; 02-07-2011 at 03:38 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by man exit
    void exit(int status);

    The exit() function causes normal process termination and the value of status & 0377 is returned to the parent
    You sure that's what you want? Your problem would be solved if you found some other way besides exit. Like, returning 0;

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Fails how?

    Doesn't compile?
    Exits when it shouldn't?
    Doesn't exit when it should?

    What type is a?

    What did you actually type in to make it pass/fail?

    Post a short and complete example, and the input you type in, if you want a fast and accurate answer.
    A few random lines of code without any context just draws a load of questions.

    And remember the code tags!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    7

    Here is my code. Second if statement fails

    Code:
    #include<cstdio>
    #include<cstdlib>
    #include<vector>
    #include<string>
    #include<iostream>
    #include<sstream>
    #include<cmath>
    using namespace std;
    
    main() {
    
    
    int a;
    int check;
    string n, m, s;
    vector <string> v;
    vector <int> iv;
    
    
    n.clear();
    while(getline(cin,n)){
        v.push_back(n);
                                                                  
    
    
    vector <string> v;
    vector <int> iv;
    
    
    n.clear();
    while(getline(cin,n)){
        v.push_back(n);
        n.clear();
        if(v[0]!= "P2"){
            return 0;
                        }
        //stringstream ss(n);
        //ss >> a;
    //  v.push_back(n);
         a = atoi(n.c_str());
        stringstream ss(n);
        ss >> a;
        if (a <0){
            return 0;
        }
    
    }
    }
    Last edited by neogst; 02-07-2011 at 03:39 PM.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    What do you mean by "Second if statement fails" do you mean that it always executes or never executes. Also please notice the third line after your while
    Code:
     n.clear();
    this clears (erases) anything that was in your variable n so when you get to your stringstream there is nothing in your variable 'n'.

    Jim
    Last edited by jimblumberg; 02-08-2011 at 10:40 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using string and stringstream effectively
    By leeor_net in forum C++ Programming
    Replies: 5
    Last Post: 07-13-2009, 12:16 PM
  2. stringstream problem
    By black_spot1984 in forum C Programming
    Replies: 3
    Last Post: 10-28-2008, 04:09 PM
  3. stringstream clear
    By misterowakka in forum C++ Programming
    Replies: 3
    Last Post: 01-01-2008, 01:03 PM
  4. Socket: send stringstream
    By crisis in forum C++ Programming
    Replies: 5
    Last Post: 11-22-2007, 10:50 AM
  5. Length of a stringstream
    By nickname_changed in forum C++ Programming
    Replies: 1
    Last Post: 10-24-2003, 07:30 PM