Hi, I'm having some difficulty with the getline command:

This is a discussion on Hi, I'm having some difficulty with the getline command: within the C++ Programming forums, part of the General Programming Boards category; Ok so im trying to write a simple program that stores user imput and I'm having problems with the getline ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    14

    Hi, I'm having some difficulty with the getline command:

    Ok so im trying to write a simple program that stores user imput and I'm having problems with the getline command. The code is as follows:
    Code:
    #include <iostream>
    #include <ostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
        int x;
        string p1;
        ofstream Prod ( "Example.txt", ios::app );
    
    do {
    
    
        cout<<"Input#1 "<<"\n";
        getline (cin, p1);
        Prod<< p1 <<"\n";
    
        cout<<"Input#2"<<"\n";
        cin>> p1;
        Prod<< p1 <<"\n";
        cin.ignore();
    
        cout<<"Input#3"<<"\n";
        cin>> p1;
        Prod<< p1 <<"\n";
        cin.ignore();
    
        p1 = ();
        cout<<"Repeat operation?:\n";
        cout<<"1)  Yes\n";
        cout<<"2)  No\n";
        cin>> x;
        } while (x != 2);
        
        cin.get();
    }
    When I run the program, it works until it asks me if I want to repeat the operation. If I choose to do so, it will cout Imput#1 without allowing me to input any information, and skips to Input#2 but this time allowing to type something.

    Any help is much appreciated! Thank you.

  2. #2
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    404
    You are so close you even used the right method on the other cin. Can you tell me what:

    Code:
    cout<<"Input#3"<<"\n";
    cin>> p1;
    Prod<< p1 <<"\n";
    cin.ignore();
    has in it that this code doesn't?

    Code:
    cout<<"Repeat operation?:\n";
    cout<<"1)  Yes\n";
    cout<<"2)  No\n";
    cin>> x;

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    14
    cin.ignore(); Thanks a million the program works great now...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. c program that accepts and executes commands?
    By Cimposter in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 02:58 PM
  3. List of Command
    By Peter_APIIT in forum C++ Programming
    Replies: 0
    Last Post: 08-08-2009, 09:47 PM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. exe files in -c- language
    By enjoy in forum C Programming
    Replies: 6
    Last Post: 05-18-2004, 04:36 PM

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