Program skipping lines of code for no apparent reason

I'm currently writing a program to manage a database of math textbooks for a class I'm taking, however the program is doing some funky things and I have no ideal why.

The function in question:
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
  void addEntry()
{
	bool flag = true;
	char bookNum[10],nameAssigned[40];
	int i;
	for (i=0;i<100;i++)
	{
		if (isalnum(books[i][0])||(books[i][0] == UnknownNullValue))
		{
			flag = false;
			break;
		}
	}
	if (flag)
	{
		cout << "This array is already full, please remove or edit an entry" << endl;
		return;
	}
	cout <<  "Enter the book number" << endl;
	cin.get(bookNum,10);
	cin.ignore(80,'\n');
	cout << "Enter the name of the person the book is assigned to" << endl;
	cin.get(nameAssigned,40);
	strcpy(books[i],bookNum);
	int j;
	for(j = 0;j<10;j++)
	{
		if(books[i][j]=='\0')
			break;
	}
	books[i][j]=' ';
	j++;
	for(int k = 0;k<40;k++)
	{
		books[i][j+k] = nameAssigned[k];
	}
	cout << books[i] << " was successfully assigned" << endl;
}


First issue: after the "cout << "Enter a book number"" it skips the cin.get() right after it
Second issue: skips all the other code and only starts again at the last line before the closing brackets

Entire Program:
[spoiler]
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
char books[100][50];
void viewAll();
void addEntry();
void removeEntry(int lenght);
void bubSort(int length);
int binarySearch(int size, char key[50]);
void editEntry(int length);
char UnknownNullValue;
int main()
{
	char subject[30];
	char fname[30];
	bool flag = true;
	int j=0;
	//char firstChar;
	int choice=-1;
	cout << "Enter the subject for the books." << endl;
	cin.get(subject,26);
	strcpy(fname,subject);
	ifstream infile;
	infile.open("ForStuff.txt",ios::in);
	infile >> UnknownNullValue;
	infile.close();
	infile.open(strcat(fname,".txt"),ios::in);
	cout << "What do you want to do with the list of " << subject << " books?" << endl;
	for (int i = 0; i<100;i++)
	{
		infile.get(books[i],50);
	}
	//infile >> firstChar;
	if (isalnum(books[0][0])||(books[0][0] == UnknownNullValue))
		cout << "(There are no previous entries in the file)" << endl;
	//infile.close();
	//infile.open(fname,ios::in);
	cout << "1 to view all entries" << endl << "2 to add an entry" << endl << "3 to remove an entry" << endl << "4 to edit an entry" << endl;
	cin >> choice;
	int k;
	for( k = 0;k<100;k++)
	{
		if(isalnum(books[k][0])||(books[k][0] == UnknownNullValue))
			break;
	}
	bubSort(k);
	switch(choice)
	{
	case 1:
		viewAll();
		break;
	case 2:
		addEntry();
		break;
	case 3:
		removeEntry(k);
		break;
	case 4:
		editEntry(k);
		break;
	}
	infile.close();
	bubSort(k);
	ofstream outfile;
	outfile.open(strcat(fname,".txt"),ios::out);
	for(int j=0;j<100;j++)
		outfile << books[j];
	return 0;
}
void viewAll()
{
	for (int i=0;i<100;i++)
	{
		if (isalnum(books[i][0])||(books[0][0] == UnknownNullValue))
			break;
		cout << books[i] << endl;
	}
}
void addEntry()
{
	bool flag = true;
	char bookNum[10],nameAssigned[40];
	int i;
	for (i=0;i<100;i++)
	{
		if (isalnum(books[i][0])||(books[i][0] == UnknownNullValue))
		{
			flag = false;
			break;
		}
	}
	if (flag)
	{
		cout << "This array is already full, please remove or edit an entry" << endl;
		return;
	}
	cout <<  "Enter the book number" << endl;
	cin.get(bookNum,10);
	cin.ignore(80,'\n');
	cout << "Enter the name of the person the book is assigned to" << endl;
	cin.get(nameAssigned,40);
	strcpy(books[i],bookNum);
	int j;
	for(j = 0;j<10;j++)
	{
		if(books[i][j]=='\0')
			break;
	}
	books[i][j]=' ';
	j++;
	for(int k = 0;k<40;k++)
	{
		books[i][j+k] = nameAssigned[k];
	}
	cout << books[i] << " was successfully assigned" << endl;
}
void removeEntry(int length)
{
	char bookNum[10];
	int i;
	cout << "What book number do you want to remove" << endl;
	cin.get(bookNum,10);
	i = binarySearch(length,bookNum);
	if (i==-1)
	{
		cout << "No entries found with that number" << endl;
		return;
	}
	for (int j = 0;j<50;j++)
		books[i][j]=NULL;
	cout << "Entry removed." << endl;
}
void bubSort(int length)
{
	char temp[50];
    for(int i=0; i<=length; i++)
	{
		for(int j=0; j<length; j++)
		{
			//Swapping element in if statement    
			if(strcmp(books[j],books[j+1])>0)
			{
				strcpy(temp,books[j]);
				strcpy(books[j],books[j+1]);
				strcpy(books[j+1],temp);        
			}
		}         
	} 
}
int binarySearch(int size, char key[10])
{
	int start=1, end=size;
	int mid=(start+end)/2;
	char nums[100][10];
	for (int i = 0;i<100;i++)
	{
		int j;
		for ( j =0;j<=10;j++)
			if(books[i][j] == ' ')
				break;
		int k;
		for( k=0;k<j;k++)
			nums[i][k]=books[i][k];
		nums[i][k+1]='\0';
	}
	while(start<=end&&strcmp(nums[mid],key)!=0)
	{
		if(strcmp(nums[mid],key)<0)
		{
			start=mid+1;
		}
		else
		{
			end=mid-1;
        }
		mid=(start+end)/2;
     }// While Loop End

	if(strcmp(nums[mid],key)==0)
		return mid; //Returning
    else
		return -1;//Returning
}// binarySearch Function Ends Here
void editEntry(int length)
{
	char bookNum[10];
	int i;
	char newName[40];
	cout << "What book number do you want to edit" << endl;
	cin.get(bookNum,10);
	cin.ignore(80,'\n');
	i = binarySearch(length,bookNum);
	if (i==-1)
	{
		cout << "No entries found with that number" << endl;
		return;
	}
	cout << "The current entry is: " << books[i] << endl << "What new name do you want to put in?" << endl;
	cin.get(newName,40);
	int j;
	for(j = 0;j<10;j++)
	{
		if(books[i][j]=='\0')
			break;
	}
	books[i][j]=' ';
	j++;
	for(int k = 0;k<40;k++)
	{
		books[i][j+k] = newName[k];
	}
	cout << books[i] << " was successfully edited" << endl;
}

[/spoiler]

(Unrelated: Preview post doesn't seem to be working for me >.<)
(And spoilers don't work)
(Also sorry if my code is eye-bleeding, the book our class uses was printed in '98 and our teacher knows nothing)
Last edited on
First off, I would like to say:
Get a new book. C++ made some major advancements with C++11. In fact, last I checked, the ISO plans on releasing another update (which is currently referred to as C++14) unless they already have. I know you are not in control of it directly, but you should bring that up to whoever makes the decisions at your school.

Secondly;
There is a known issue with using std::cin.get()
In fact, somebody else has asked a question regarding a similar issue on stackoverflow. Here is what the answer says:

If you're using getline after cin >> something, you need to flush the newline out of the buffer in between.

My personal favourite for this if no characters past the newline are needed is cin.sync(). However, it is implementation defined, so it might not work the same way as it does for me. For something solid, use cin.ignore(). Or make use of std::ws to remove leading whitespace if desirable:

1
2
3
4
5
6
7
8
9
10
11
12
int a;

cin >> a;
cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n'); 
//discard characters until newline is found

//my method: cin.sync(); //discard unread characters

string s;
getline (cin, s); //newline is gone, so this executes

//other method: getline(cin >> ws, s); //remove all leading whitespace 


Source: http://stackoverflow.com/questions/10553597/cin-and-getline-skipping-input

To sum it up, if you want to use std::cin.get(); you should put std::cin.ignore(); right before it.
Last edited on
Thank you very much! Now I have 1/4 of the program done. :) Now I need to figure out what's wrong with the removeEntry function, viewAll function, and editEntry function >.<

Let's do the viewAll function first since it's problem reoccurs.
1
2
3
4
5
6
7
8
9
void viewAll()
{
	for (int i=0;i<100;i++)
	{
		if ((books[i][0]==0)||(books[i][0] == UnknownNullValue)||(books[i][0]=='\0')||(books[i][0]==NULL))
			break;
		cout << books[i] << endl;
	}
}


The issue lies in the if statement's logic check. I'm trying to have it go and break out of the loop if the first character is a null or doesn't exist. In my IDE when I set a breakpoint by there it lists books[insert something random here][0] = 0 so I have that there, and it works, but a little too well. It also breaks on normal letters too.

While trying to figure that I encountered another problem. When I get the original input from the file in
1
2
3
4
5
6
	for (int i = 0; i<100;i++)
	{
		infile.sync();
		infile.get(books[i],50);
		infile.ignore(80,'\n');
	}

it only gets the first line and assigns it to books[0] and doesn't read any other input.
Last edited on
Your viewAll() function will stop displaying books after it finds the first empty slot. You probably need to heck all the slots and just skip the ones that are empty. Also, the check more complicated than it needs to be. The slot is invalid if it starts with a null character. Taken together the code should be:
1
2
3
4
5
6
7
8
void viewAll()
{
	for (int i=0;i<100;i++) {
		if ((books[i][0] !=0) {
			cout << books[i] << endl;
		}
	}
}
Ok thanks, I fixed that, and another problem with my bubble sort, but the input problem still stands. It's still only getting input from the first line of the file into the first line of the array. My data file is as follows:
13-67 John Smith
24-98 Jane Doe
54-12 John Jacob

However, if I step through it line by line, it only gets the first line of input, and just gets a blank value for the rest.

The code in question once again:
1
2
3
4
5
6
7
infile.open(strcat(fname,".txt"),ios::in);
for (int i = 0; i<100;i++)
{
	infile.sync();
	infile.get(books[i],50);
	infile.ignore(80,'\n');
}
I tried messing around with the code in the loop, but to no avail, it still doesn't work.
Topic archived. No new replies allowed.