A problem in using getline?

Theres a problem when i choose the Choice B

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
 #include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;

struct reserve
{
    string name;
    string date;
    string operation;

};
int main()
{
    reserve res;
  fstream myfile;
  char choice;
 myfile.open("Reservation.txt",ios::in|ios::out|ios::app);
  choices:
  cout<<"\t\t\tReservation"<<endl;
  cout<<"Choice the letter of your choice:"<<endl;
  cout<<"[A]. View Reservation\n[B]. Add Reservation\n[C]. Remove Reservation\n[D]. Exit Program"<<endl;
  cout<<"\nChoice:";

  cin>>choice;

  if  (choice=='A'||choice=='a')
  {
     string output;
     myfile.seekg(0,ios::beg);

     cout<<"NAME:\t\tSUBJECT:\t\tOPERATION:\n";
     while (getline(myfile, output))
  {
	cout << "\n " << std::left << std::setw(15) << output;

	std::getline(myfile, output);
	std::cout << std::setw(4) << output;

  }
  else if(choice=='B'||choice=='b')
  {

        cout<<"Enter the name:";
        getline(cin,res.name);

        cout<<"\nEnter the date:";
        getline(cin,res.date);
        cout<<"\nEnter the operation:";
        getline(cin,res.operation);

        myfile<<res.name<<"\n"<<res.date<<"\n"<<res.operation<<"\n";

  }
  else if(choice=='C'||choice=='c')
  {

  }
  else if(choice=='D'||choice=='d')
  {
    return 0;
  }
  else
  {
      system("cls");
      goto choices;
  }

  myfile.open("Reservation.txt",ios::in|ios::out|ios::app);




}

Last edited on

LINE 42

Enter the name:
Enter the date:_




it jumps on the Enter date and didnt get the name idont know where i am wrong
Last edited on
Extract and discard the new line remaining in the input buffer after cin >> choice; on line 25.
Add cin.ignore( 10000, '\n' ) ; // line 26

More information: http://www.cplusplus.com/forum/general/63681/#msg344674
jl Borges,

I helps thank you very much.

But what is the meaning of the cin.ignore (10000, '\n');

how it fix it?


CoolAvocado
Let us say, we want to read a char (formatted input) and a string (using unformatted input) from stdin. We write:

1
2
3
4
char choice ;
std::string name ;
std::cin >> choice ;
std::getline( std::cin, name ) ;


We run the program and enter A<newline> on stdin.
std::cin >> choice ; reads 'A' into choice and leaves the <newline> in the input buffer.
The program continues with std::getline( std::cin, name) ;
The getline() sees that the input buffer is not empty, so it does not wait for any input to be entered. It returns immediately after reading an empty string, and extracting the <newline> in the buffer and throwing it away.

We can make the stream discard trailing white space left by the formatted input operation by:

1
2
3
4
5
char choice ;
std::string name ;
std::cin >> choice ;
std::cin.ignore( 10000, '\n' ) ;
std::getline( std::cin, name ) ;


std::cin.ignore( 10000, '\n' ) ; will extract and discard up to 10000 characters or until a '\n' is extracted and thrown away. The 10000 in the example is just a reasonably large enough value, because we don't expect that more than that many characters would need to be discarded.
Topic archived. No new replies allowed.