cin.getline() not responding

Hi!
My English grammar is not good. Avoid any grammar mistake.
I am trying to create a program in c++ which will store a message from user and display it latter. But cin.getline() function is refusing to response. I am using dev-c++ 5.11 version of c++ compiler. The source code is;
#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
int main()
{
int n,x,j;
char me[255];
ifstream rev;
ofstream store;
cout<<"1:Enter your message\t2: Read your message\n";
cout<<"Enter your choice";
cin>>n;
switch(n)
{
case 1:
cout<<"Enter your message(Max character 253)\n";
cin.getline(me,255);
cout<<"Skiped";
store.open("Message.txt");
store<<me;
store.close();
break;
case 2:
cout<<"Accessing file....";
rev.open("Message.txt");
rev>>me;
cout<<rev;
rev.close();
break;
default:
cout<<"Error";
}
getch();
return 0;

}
Last edited on
This should work:
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
#include<iostream>
#include<conio.h>
#include<fstream>

using namespace std;

int main()
{
  int choice ,x,j;
  char me[255];
  char dummy;

  ifstream rev;
  ofstream store;
  cout<<"1:Enter your message\t2: Read your message\n";
  cout<<"Enter your choice: ";
  choice = getch() - '0';
  cout << "You have choosen: " << choice << "\n\n";
  switch(choice)
  {
  case 1:
    cout<<"Enter your message(Max character 253)\n";
 
    cin.getline(me,255);
    cout<<"Skiped";
    store.open("Message.txt");
    store<<me;
    store.close();
    break;
  case 2:
    cout<<"Accessing file....";
    rev.open("Message.txt");
    rev>>me;
    cout << me;
    rev.close();
    break;
  default:
    cout<<"Error";
  }
  getch();
  return 0;

} 
what about dummy? How this program work?
dummy is actually useless and can be deleted.
Basically I have inserted this to get the user choice.
choice = getch() - '0';
If you press 1 getch() will return 49 and to get the value 1 you need to subtract 48 which is the same as '0'
Thanks Thomas1965
Topic archived. No new replies allowed.