Thread: Strings

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Can you provide more code? What you posted works when put into a simple (and complete) program:
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    int main()
    {
        std::istringstream iss("show 0");
        std::string show;
        int poly;
        if (iss >> show >> poly)
        {
            std::cout << "Success!\n";
        }
        else
        {
            std::cout << "Failure!\n";
        }
    }

  2. #17
    Registered User
    Join Date
    Mar 2006
    Posts
    34
    Here is the loop:

    Code:
     int poly, argument, coefficient, exponent;
     char colon = ':';
     char assignment = '=';
     char multiply = '*';
    
     while(INPUTFILE && INPUTFILE.peek() != EOF)
     {
       getline(INPUTFILE, theTerms);
       iss.str(theTerms);
         
    if(iss >> poly >> colon)
    {
     locationOfPoly = poly;                   
       
       while(iss >> coefficient >> exponent)
       {
        tPoly.insert(exponent, coefficient);
       }
    
       //postcondition: all info has been extracted from iss so assign tmpPoly
       //to Poly[locationOfPoly] 
       Poly[locationOfPoly] = tPoly; 
    }  
      
        
      if(iss >> poly >> assignment >> poly >> multiply >> poly)
       {
        Poly[poly] = Poly[poly] * Poly[poly];
       }
       
       if(iss >> show >> poly)
       {         
        locationOfPoly = poly;
        cout <<"Poly[" << locationOfPoly << "] = " << Poly[locationOfPoly] << "\n";
       }
     
     }//end while INPUTFILE loop

  3. #18
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If the line is "show 0", then the first if statement will run and fail.
    Code:
    if(iss >> poly >> colon)
    poly is an int, but "show" is not, so this causes a failed read. I think you are assuming that it will move on to the next if and try again, right? Unfortunately, when one failure happens it sets a failbit in the stream. you have to clear that failbit before trying again. You can do this by calling clear.

    However, you stil might have a problem between the first and second if. Both start with an int, so if the line is meant for the second if, the first if might still read part of the data.

    What I would do is re-set the istringstream after each if:
    Code:
    iss.clear();
    iss.str(theTerms);

  4. #19
    Registered User
    Join Date
    Mar 2006
    Posts
    34
    Thanks, that did it.

  5. #20
    Registered User
    Join Date
    Mar 2006
    Posts
    34
    Quote Originally Posted by Daved View Post
    If the line is "show 0", then the first if statement will run and fail.
    Code:
    if(iss >> poly >> colon)
    poly is an int, but "show" is not, so this causes a failed read. I think you are assuming that it will move on to the next if and try again, right? Unfortunately, when one failure happens it sets a failbit in the stream. you have to clear that failbit before trying again. You can do this by calling clear.

    However, you stil might have a problem between the first and second if. Both start with an int, so if the line is meant for the second if, the first if might still read part of the data.

    What I would do is re-set the istringstream after each if:
    Code:
    iss.clear();
    iss.str(theTerms);
    I now realize that I was using the stringstream improperly and found that this was the cause of a major problem. Now I know and have learned that the variables in the if () conditions just accept a string, a char, or int in my case. So to do what I really wanted, I had to add an && condition to check if that variable actually contained what I was looking for.

    char colon = ':';
    char assignment = '=';
    char multiply = '*';

    The above was pointless since these would be replaced by what was read.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM