getline function

May 22, 2017 at 6:50am
The programme is running but the getline function does not run and programme ends . programme exit without asking for input

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include <iostream>
#include<string>
#include<fstream>
using namespace std;

int main()
{
       ifstream input_oper;
   ofstream output_oper;

   char namefile[20];
string nameinfilein;
cout<<"name of the file : ";
cin>>namefile;
   cout<<"Enter file to open ";
   getline(cin,nameinfilein);

   output_oper.open(namefile);
    output_oper<<nameinfilein;

    return 0;
}
Last edited on May 22, 2017 at 6:52am
May 22, 2017 at 7:34am
It looks like there's a '\n' left in the input buffer...
Discarding it with std::cin.ignore() seems to solve the problem.
You could consider using std::string as a type for this variable too, as well as you do later: it should solve the issue automatically.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include<string>
#include<fstream>
using namespace std;

int main()
{
    // ifstream input_oper;

    cout << "name of the file : ";
    char namefile[20];
    cin >> namefile;
    std::cin.ignore();
    cout << "Enter file to open ";
    string nameinfilein;
    getline(cin, nameinfilein);
    std::cout << "You entered: '" << nameinfilein << "'\n";

    ofstream output_oper;
    output_oper.open(namefile);
    output_oper << nameinfilein;

    return 0;
}

May 22, 2017 at 1:42pm
Hello Ma92,

To expand on what Enoizat said.

First understand that "cin >>" is formatted input. That is there is some type checking based on the type of variable that "cin" is putting information into. This can be useful when your input is to a numeric variable and something other than a number is entered. Also note that "cin" will input to the first white space or "\n" (new line) that is encountered. So when "cin" reads from the keyboard or a file it will take everything except the "\n" which it leaves in the input buffer. Another "cin >>" will ignore the "\n" and read the next piece of information.

The problem occurs when a "std::getline()" follows a "cin >>". The "cin" leaves the "\n" in the input buffer, but "std::getline()" will extract the "\n" from the input buffer and continue on working with an empty string.

This is where the "cin.ignore()" comes in to clear the input buffer before a "std::getline()" can be used.

Now "cin.ignore()" does work, but the more preferred way is to include the header file "limits" and use std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');. This code can also be used as a way to pause the program and keep the console window open.

Your program does include the header file "string". I would suggest using "std::string" in place of the C-style char array. Since C++ 11 a "std::string" can be used to open a file.

On line 18 you open a file even though the file will close when the program ends it is good practice to close the file when you are finished using it.

Hope that gives you a better understanding,

Andy

May 23, 2017 at 5:30am
thanks to both it helps me lot Enoizat and Handy Andy

when i use string other than c-style char array to open file. it generates an error in complier
but at online compilation in cpp.shell it is running

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include<string>
#include<fstream>
using namespace std;

int main()
{
    // ifstream input_oper;

    cout << "name of the file : ";
    string namefile;
    cin >>namefile;
    std::cin.ignore();
    cout << "Enter content to file ";
    string nameinfilein;
    getline(cin, nameinfilein);
    std::cout << "You entered: '" << nameinfilein << "'\n";

    ofstream output_oper;
    output_oper.open(namefile);
    output_oper << nameinfilein;

    return 0;
}
Last edited on May 23, 2017 at 5:34am
May 23, 2017 at 10:17am
Could you please try the following code and post your output? I mean, the compiler error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <limits>
#include <string>
#include <fstream>

int main()
{
    std::cout << "Name of the file: ";
    std::string namefile;
    std::cin >> namefile;

    std::cout << "Content to insert into file: ";
    std::string nameinfilein;
    std::cin.ignore();
    std::getline(std::cin, nameinfilein);
    std::cout << "You entered: '" << nameinfilein << "'\n";

    std::ofstream output_oper(namefile);
    output_oper << nameinfilein;

    std::cout << "\nPress ENTER to continue\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    return 0;
}

May 23, 2017 at 10:44am
yes when i run this code in my pc complier generate error

when i use char namefile[10]; instead of string namefile; //Line 9 ,. it works ok generate no error
May 23, 2017 at 11:21am
Regarding the compiler error with string versus character array, most likely you are using an older compiler or it is not configured to use the latest C++ standard.

When using std::string namefile you may need to try this:
Current standard:
 
std::ofstream output_oper(namefile);


Older version of C++:
 
std::ofstream output_oper(namefile.c_str());



Generally you should prefer to use std::string as it is safer than using a character array.

Also, it's a good idea to update the compiler, or configure it to use at least C++11 or later.

May 24, 2017 at 1:45pm
Thanks chervil it solves my problem .
Topic archived. No new replies allowed.