cin.ignore??????

there is something wrong with my code.
i am doing a c++ database. i used getline() to read the line I've inputted but when i used cin.ignore(), my program was "destroyed".

Here is my code:
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include <string>
#define MAX 100                
using namespace std;

struct StudentRec
{
  int id;
  string name;
  string course;
};

// Linear Database
StudentRec db[MAX];
int dbi = 0;                   

// Functions
bool dbIsEmpty()
{
  return (dbi == 0);
}

bool dbIsFull()
{
  if (dbi == MAX)
    return true;
  else
    return false;
}

void dbAdd(string rName, string rCourse, int rId)
{
  if (! dbIsFull())
  { // dbi is the index where an empty space is located.
    db[dbi].name   = rName;
    db[dbi].id     = rId;
    db[dbi].course = rCourse;
    dbi++;
  }
}

void dbDisplay()
{
  if (! dbIsEmpty())
  {
    for (int i = 0; i < dbi; i++)
    {
      cout << "Record #" << (i+1) << endl;
      cout << "\tID     : " << db[i].id << endl;
      cout << "\tName   : " << db[i].name << endl;
      cout << "\tCourse : " << db[i].course << endl;
      cout << endl;
    }
  }
  else
    cout << "Database is empty" << endl;
    cout << endl;
}

void dbPrintRecord(int i)
{
	  cout << "\nRecord #" << i + 1 << endl;
      cout << "\tID     : " << db[i].id << endl;
      cout << "\tName   : " << db[i].name << endl;
      cout << "\tCourse : " << db[i].course << endl;
      cout << endl;
}

int dbSearch(string key)
{
	for(int i = 0; i < dbi; i++)
		if(db[i].name == key)
			return i;
		return -1;
	
}
int main()
{
  string menu = "[a]dd [s]earch [p]rint e[x]it : "; 
  char cmd;

  while (cmd != 'x')
  {
    cout << menu;
    cin >> cmd;
    cout << endl;
    if (cmd == 'a')   
    {
      int id; 
      string name, course;
      cout << "Enter Name   : ";
      getline(cin,name);             //<-----THE ERROR STARTS HERE
      cin.ignore(1,' ');
      cout << "Enter Course : ";
      getline(cin,course);
      cin.ignore(100,'\n');
      cout << "Enter ID     : ";
      cin >> id;
      dbAdd(name, course, id);
      cout << endl;
    }
    else if (cmd == 'p')
      dbDisplay();
    else if (cmd == 's')
    {
		string searchkey;
		cout << "Search the record of whom? ";
		cin >> searchkey;
		cin.ignore(1,' ');
		
		int index = dbSearch(searchkey);
		if(index != -1)
			dbPrintRecord(index);
		else
			cout << "No Record Found" << endl;
	}
  }
  
  return 0;
}


I hope someone can help me with this
1
2
3
cin >> cmd; // this reads in cmd and leaves '\n' unprocessed
...
getline(cin,name); // this processes '\n' and returns an empty string 

If storing an empty string in name is not your desired outcome, use cin.ignore() (or the more robust cin.ignore(numeric_limits<streamsize>::max(), '\n'); right after cin >> cmd;.

The cin.ignores after getlines should probably all be removed.
Last edited on
Topic archived. No new replies allowed.