problem with cin.getline()

This is a discussion on problem with cin.getline() within the C++ Programming forums, part of the General Programming Boards category; my program compiles fine, but when i use cin.getline(); to get some data, i believe it reads the return from ...

  1. #1
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001

    problem with cin.getline()

    my program compiles fine, but when i use cin.getline(); to get some data, i believe it reads the return from a previous cin so it blows through the code during runtime before i can even enter any information, i've put more cin's after the cin.getline, and they don't get skipped over, just the cin.getline();...here's the code, it's fairly short, can anyone help me with this bug?
    Code:
    #include <iostream>
    #include <fstream>
    #include <stdio>
    using namespace std;
    
    char id[500000];
    char fileHandle[]=".txt";
    char* fileName;
    char input[100000];
    
    int main() {
    
    cout<<"PORTFOLIO EDITOR-FILE CREATION TEST"<<endl;
    cout<<"Basically you type in the id number, it gets used in the creation of a file called that number plus .txt"<<endl;
    cout<<"ENTER ID NUMBER"<<endl;
    cin>>id;
    cout<<"Now give me something to write into the file"<<endl;
    
    cin.getline(input,99990,'\n');
    
    
    
    fileName=strcat(id,fileHandle);
    ofstream fout(fileName);
    fout<<input;
    fout.close();
    
    return 0;
    }
    thanks
    PHP and XML
    Let's talk about SAX

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    4
    >>#include <stdio>
    It's either stdio.h or cstdio, Edward knows of no other version.

    Replace...
    cin>>id;
    with...
    cin>>id, cin.get();
    and it will work as expected.

    =Ed=

  3. #3
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    thanks it worked, but uh...how do i get it to read after the first space? i know how to do it with the other method, but not with this...how would i go about it?
    PHP and XML
    Let's talk about SAX

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    4
    >>but uh...how do i get it to read after the first space?
    Edward is slightly confused. Isn't that what getline is for in your code? Ed's solution was to use get after cin to remove the floating newline from the input stream so that getline would work right...
    Code:
    cin>>id, cin.get();
    cout<<"Now give me something to write into the file"<<endl;
    
    cin.getline(input,99990L,'\n');
    =Ed=

  5. #5
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    ok, i got it, i thought that you meant to use that on the cin.getline(); , now i know what you mean....it's been a long couple of days so im not exactly on the top of my game. thanks for your trouble.
    PHP and XML
    Let's talk about SAX

  6. #6
    EDL
    EDL is offline
    Registered User
    Join Date
    May 2002
    Posts
    18
    Hey, what I usually do in that case, is put a cin.ignore(); in right before the line it tends to skip. I don't know if that is good coding practice, lol. But it works.

  7. #7
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    The guy above me is right, if you put a cin.ignore before cin.getline it should work, like this.

    Code:
    #include <iostream>
    #include <fstream>
    #include <stdio>
    using namespace std;
    
    char id[500000];
    char fileHandle[]=".txt";
    char* fileName;
    char input[100000];
    
    int main() {
    
    cout<<"PORTFOLIO EDITOR-FILE CREATION TEST"<<endl;
    cout<<"Basically you type in the id number, it gets used in the creation of a file called that number plus .txt"<<endl;
    cout<<"ENTER ID NUMBER"<<endl;
    cin>>id;
    cout<<"Now give me something to write into the file"<<endl;
    
    cin.ignore();
    cin.getline(input,99990,'\n');
    
    
    
    fileName=strcat(id,fileHandle);
    ofstream fout(fileName);
    fout<<input;
    fout.close();
    
    return 0;
    }
    I haven't compiled it so let me know

  8. #8
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    much thanks to the both of you, both methods work...i also found another method i ended up using after i updated my program this morning, if you insert the cin.clear(); command after each cin, it clears in input buffer so that any extra returns still residing there will not affect the program. It works just like cin.ignore(); except you place it after the cin command.
    PHP and XML
    Let's talk about SAX

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    362

    cin.clear()

    Typically, you would use this function to clear any error bits set when there is an input error.

    For example, you attempt to open a file that doesn't exist and wish to re-prompt the user for a valid file name. Wtihout using cin.clear();, the error bit will remain "set" and an attempt to re-input the file name will fail.

    To my knowledge, cin.clear() does not substitute for cin.ignore().

    You might want to try cin.flush() which flushes the buffer and inserts the '\n' char.

    I've also found that cin.ignore(x, '\0') works well with menus where 'x' is a simple int value depending on your needs, i.e 2, 5, whatever. (The cursor displays next to your menu prompt as opposed to jumping down to the next line.)
    Last edited by skipper; 05-28-2002 at 05:56 PM.
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  4. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21