Thread: Simple Question..

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    13

    Question Simple Question..

    lets say...
    Code:
    int something;
    cin>>something;
    user enters:"1,space,1"
    How do I stop (input buffer?) at the space?

    Not sure if you guys can understand that.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Are the commas part of the input? Or is it just 1 space 1

    What exactly do you mean by "stop"?
    cin will "stop" automatically in the sense that it will read the 1 in as an integer and the file pointer wll be pointing to the space (or comma if they're part of the input).
    If you mean how do you get rid of any extra stuff on the line, then look at cin.ignore()
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    13
    Sorry if I am being confusing.
    Let's say
    Code:
    void GetSex(){
        int input=0,count=0,input2=0;
        do{
        if(count==1)
        cout<<"Please enter a correct input...\n\n";
        cout<<"Enter your sex...\n";
        cout<<"-----------------\n";
        cout<<"1.Male\n";
        cout<<"2.Female\n";
        cin>>input;
        if(!cin.good()){
            cin.clear();
            cin.ignore();
        }
        count=1;
        system("cls");
        }while(input!=1 && input!=2);
        switch(input){
        case 1:
            count=0;
            cin.ignore();
            do{
            if(count==1)
            cout<<"Enter a correct input...\n";
            cout<<"You have entereed Male, are you sure?\n";
            cout<<"1.Yes  2. No\n";
            cin>>input2;
            if(!cin.good()){
            cin.clear();
            cin.ignore();
            }
            count=1;
            system("cls");
            }while(input2!=1 && input2!=2);
            switch(input2){
                case 1:
                    system("cls");
                    break;
                case 2:
                    system("cls");
                    cout<<"Select again...\n";
                    GetSex();
                    break;
            }
        break;
        case 2:
            count=0;
            do{
            if(count==1)
            cout<<"Enter a correct input...\n";
            cout<<"You have entereed Female, are you sure?\n";
            cout<<"1.Yes  2. No\n";
            cin>>input2;
            if(!cin.good()){
            cin.clear();
            cin.ignore();
            }
            count=1;
            system("cls");
            }while(input2!=1 && input2!=2);
            switch(input2){
                case 1:
                    system("cls");
                    break;
                case 2:
                    system("cls");
                    cout<<"Select again...\n";
                    GetSex();
                    break;
            }
         break;
    
    
        }
    So when user enters"1 1 1"
    It will skip through and input the other 'ones' into the cin statements.
    Sorry if I'm unclear.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Then, after each std::cin, add
    std::cin.ignore(65535);

    This will "get rid" of any input left in the input buffer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Simple Question (I think)
    By slawrence10 in forum C Programming
    Replies: 7
    Last Post: 11-21-2008, 06:28 PM
  2. Simple question
    By lleon in forum C Programming
    Replies: 9
    Last Post: 01-19-2006, 01:28 PM
  3. simple simple design question
    By Chaplin27 in forum C++ Programming
    Replies: 6
    Last Post: 05-31-2005, 11:33 PM
  4. simple question
    By pancho in forum C Programming
    Replies: 3
    Last Post: 04-09-2002, 03:26 PM
  5. a simple question
    By DramaKing in forum C Programming
    Replies: 5
    Last Post: 01-20-2002, 12:18 PM