Thread: limits for reading inputlines

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    16

    limits for reading inputlines

    hello

    I am having problems with reading input lines.
    i wanna tell my progrem that whan i input a line it have to read diferent strings.

    if i input a line:
    n10000 x-123 465 y

    i have to get this
    string strn ( line , 'n' , 'x');
    string strx (line , 'x' , 'y');
    cout << " n = " << strn <<endl;
    cout << " x = " << strx << endl;

    so when i run a program i shuld get this output:
    n = 10000
    x = -123465
    it wont work couse the start of astring is wrong why
    here is my
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    
    int main()
    {
        string str;
        cout << "Enter a number: ";
        getline(cin, str);
    
        stringstream ss;
        string s4 ( str , 'n' , 'x'); //if i write a number instead of 'n' it will work and start reading
        string s5 (str , 'x' , 'y');
        ss << s4 ;
    cout << " s4 = " << s4 << endl;
    cout << " s5  =  " << s5 <<endl ;
    //int i;
    //ss >> i;
        //int i;
       // if ( ss >> i )
       //     cout << "Valid conversion to int:\n " << i <<"\n";
       //    cout << " i + 100 =  " << ( i + 100)<<endl;
    /*else
            cout << "Invalid input";
    }
    */
    }
    Last edited by ziga; 08-24-2007 at 12:22 AM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The entire line is read in to str. You are trying to separate it by the characters 'n', 'x', and 'y'. However, I don't think the string constructor works like that. I think you have to call find to find the location of those characters and then substr to get a substring between them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Limits of C/C++
    By Abda92 in forum C Programming
    Replies: 4
    Last Post: 12-24-2007, 01:22 PM
  2. Process Resources and System Limits Problem
    By yair in forum C Programming
    Replies: 2
    Last Post: 09-07-2005, 02:24 AM
  3. Exceeding the limits
    By WrathFox in forum C++ Programming
    Replies: 5
    Last Post: 01-10-2005, 07:00 PM
  4. data type limits
    By JTtheCPPgod in forum C++ Programming
    Replies: 7
    Last Post: 01-26-2003, 10:08 PM
  5. memory limits in console (win NT/9X)?
    By dp_goose in forum C++ Programming
    Replies: 1
    Last Post: 10-29-2002, 06:00 AM