confused with cin.getline()

This is a discussion on confused with cin.getline() within the C++ Programming forums, part of the General Programming Boards category; I have made a if statement, and I know that it is carried through because the first cout in it ...

  1. #1
    NotSoAvgProgrammer
    Join Date
    Jul 2007
    Location
    Virginia, U.S.
    Posts
    57

    confused with cin.getline()

    I have made a if statement, and I know that it is carried through because the first cout in it is executed, and one I put at end to test.
    Code:
    using namespace std;    //namespace std
    
    int i;
    string line;            //initiate a string
    vector<string> names;
    
    
    int main ()
    
        {
            char string[256];
    
            std::string user_input;
    
    
            cout<< "Enter one to add name, or zero to continue: ";
    
            cin>> i;
    
    
            if (i == 1)
                {
    
    
                    cout<< "please enter name: \n";
    
                    cin.getline(string, 256);
    
                    user_input = string;
    
                    ofstream people ("example.txt", ios::app);     //open file
    
                    people<< "\n" << user_input;       //write names
    
    
                    people.close();          //close file, so it can be opened in read mode
    
                    i = 0;
                    cout<< "test"   // comes throuch
                    cin >> i;         /// executes.
    
                }

    Whats wrong with the program?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,789
    You have not tested the file (error checking) to make sure it is opened successfully. You also need to logical-or (|) the ios::app with ios::out I believe.

    There is also a string version of the getline function that you could use instead of what you're using:
    Code:
    getline(cin,user_input);
    The explicit close of the people ofstream object isn't necessary but isn't hurting anything.

    Beyond that what are you expecting to happen with the code?
    Last edited by hk_mp5kpdw; 08-23-2007 at 08:13 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    13
    Yeah that doesn't exactly explain what error you get or what are you getting that you didn't expect?

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    the string variable conflicts with std::string.
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    the line you get in with getline is an empty string from the end of the line after you entered in the integer.
    Last edited by robwhit; 08-23-2007 at 10:57 PM.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    You should open the file directly, then check if the file was opened. Here is an example from one of my projects.

    Code:
        ifstream file;
        
        cout<<"Enter file name: ";
        cin.getline(filename,45);
        
        file.open(filename);
        
        if(file.is_open()){
        
           file >> x[n] >> y[n];
    Last edited by scwizzo; 08-23-2007 at 10:20 PM.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> You also need to logical-or (|) the ios::app with ios::out I believe.
    For an ofstream, ios::out is automatic.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.getline and msgrcv
    By osal in forum C++ Programming
    Replies: 2
    Last Post: 03-17-2005, 11:01 AM
  2. why wont this compile?!? :confused:
    By jdude in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2004, 12:13 AM
  3. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-04-2003, 01:21 PM
  4. problem with cin.getline()
    By Waldo2k2 in forum C++ Programming
    Replies: 8
    Last Post: 05-28-2002, 05:53 PM

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