Where are my "getchar()" commands throwing me off?

If the user is to press 'q' or 'Q' then enter to quit, rather than exiting at the first push of enter, the program makes thee user presses enter two more times before the screen closes. What can I change in 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186

#include <iostream>
#include <string>
#include<fstream>

using namespace std;

//constant array size
const int ARRAY_SIZE = 1000;

//function prototypes
int loadData(string);
int searchByAuthor(int, string);
int searchByTitle(int, string);
string bookTitle[ARRAY_SIZE];
string bookAuthor[ARRAY_SIZE];
void showAll(int);


int main ()
{
    //declar & initialize variables
    string path;
    string search;
    char choice = 0;
    
    //prompt user to enter input& store in path
    cout << "Welcome to Chase's Library Database." <<endl;
	cout << "Please enter the name of the backup file: ";
    cin >> path;
    
    //call loadData to count number of files
    int n = loadData(path);
    
    //for the case that the filename was invalid
    if(n == -1)
    {
        cout << "Cannot recognize the filename entered";
        
		//pause
		getchar();
		getchar();
		return 0;
    }
    
    //displays # of files returned by loadData
	cout << endl << n << " items were found in the library." << endl << endl;
    
	cout << "Enter Q to (Q)uit, Search (A)uthor, Search (T)itle, (S)how All: ";
    //get user's choice
	cin >> choice;
    
	//pause
	getchar();

    //switch on user's choice
    switch(choice) {
    case 'A':
    case 'a':
        cout << "Please enter the author's name you wish to search: ";
        getline(cin, search);
        cout << searchByAuthor(n,  search);
        cout << " titles found" << endl;
        break;
	
	case 'Q':
    case 'q':
        break;
        return 0;
	
	case 'S':
    case 's':
		showAll(n);
        break;
    
	case 'T':
    case 't':
        cout << "Please enter the title of the book you wish to search: ";
        getline(cin, search);
        cout << searchByTitle(n,  search);
        cout << " titles found" << endl;
        break;
            
    }
    //stops program from exiting before user prompts
	getchar();
	getchar();
	return 0;
}


//function to search for the title
int searchByAuthor(int count, string search) {
    
    //declare variable
    int found = 0;
    
    //counting loop
    for (int c = 0;  c < count; c++) {
        
        //look for a title that matches string search while the search is less than the end of the file
        if(bookAuthor[c].find(search, 0) < bookAuthor[c].npos){
        
            //when found print out the title with the author
            cout << bookTitle[c] << " (" << bookAuthor[c] << ")" << endl;
            
            //keep counting up
            found++;
     }
    }
    
    //return the number found to int main()
    return found;
}


//function to search for the title
int searchByTitle(int count, string search) {
    //delcare variable
    int found = 0;
    
    //counting loop 
    for (int c = 0;  c < count; c++)
    {
        //look for a title that matches string search while the search is less than the end of the file
        if(bookTitle[c].find(search, 0) < bookTitle[c].npos){
            
            //when found print out the title with the author
            cout << bookTitle[c] << " (" << bookAuthor[c] << ")" << endl;
            
            //keep counting up
            found++;
     }
    }
    
    //return the number found to int main()
    return found;
}


//function to show the titles and authors on the same line
void showAll(int count) {
    //count up through the array
    for(int c = 0; c < count; c++) 
    {
        //print out the titles and then the authors
        cout << bookTitle[c] << " (" << bookAuthor[c] << ")" << endl; 
    }
}


//function to load the file
int loadData(string path) {
    ifstream file;
    file.open(path.c_str());
     
    //if there is no file 
    if(!file) {
         
        //return back to ask for the file
        return -1;
     }
     
    //declare local variable 
    int c;
     
    //use variable to count through the arrays
    for(c = 0; c < ARRAY_SIZE && !file.eof(); c++) {
         
         //get the title
         getline(file, bookTitle[c]);
         
         //get the author
         getline(file, bookAuthor[c]);
     }
     
     //return the number c to int main()
     return c;
}








edit: never mind. I had my break in the wrong spot.
Last edited on
Topic archived. No new replies allowed.