cin.getline problem

closed account (o2TCpfjN)
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
int id[10][8] = {0},year[10][1] = {0},x;
char command ,name[10][26] = {'\0'},pg[10][6] = {'\0'};



cout << "Enter the student ID:";
cin >> id[0][8];

cout << "Enter the student Name:";
cin.getline(name[0], 26 , '\n');

cout << "Enter the programme:";
cin.getline(pg[0], 6 , '\n');


cout << "Enter the year:";
cin >> year[0][1];

cout << id[0][8] << name[0][26] << pg[0][6] << year[0][1] << endl;

system("PAUSE");
return EXIT_SUCCESS;
}




when I run the program have some problem


the program finally just like that:

Enter the student ID:1
Enter the student Name:Enter the programme:a
Enter the year:1
1 1



what wrong is it??
You are mixing input methods. Fix it with a call to ignore():
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
#include <limits>      // you will need this
#include <iostream>
#include <cstring>
using namespace std;

int main()
  {
  // BTW, better to keep arrays, initializations, and non-arrays all separate
  int id[10][8] = {0};
  int year[10][1] = {0};
  int x;

  char command;
  char name[10][26] = {'\0'};
  char pg[10][6] = {'\0'};

  cout << "Enter the student ID:";
  cin >> id[0][8];
  cin.ignore( numeric_limits <streamsize> ::max(), '\n' );  // get rid of that pesky Enter key

  cout << "Enter the student Name:";
  cin.getline(name[0], 26 , '\n');  // getline() automatically looses the Enter key

  cout << "Enter the programme:";
  cin.getline(pg[0], 6 , '\n');

  cout << "Enter the year:";
  cin >> year[0][1];
  cin.ignore( numeric_limits <streamsize> ::max(), '\n' );  // again, for below

  cout << id[0][8] << name[0][26] << pg[0][6] << year[0][1] << endl;

  // Please don't use system("PAUSE") if you can avoid it.
  cout << "Press ENTER to continue..." << flush;
  cin.ignore( numeric_limits <streamsize> ::max(), '\n' );

  return 0;  // I like EXIT_SUCCESS too... but you should #include <cstdlib> to use it
  }

Hope this helps.
Do these tutorials teach system("pause") because i don't think thats how beginners should be learning it :-/
It is a plague of lazyness and un-knowledge.

Many, many C and C++ books and classes teach stuff like that all the time.

Alas.
Topic archived. No new replies allowed.