getline loop

hello,

my code keeps generating a loop when it is not supposed to. i need to get a view strings into a class so i can use it later.

this it the function it keeps on going wrong

1
2
3
4
5
6
7
8
9
10
11
12
  void Add_Book()
{
	cout<<"enter the name of the book you want to add, DO NOT USE SPACES"<<endl;
	cin.getline(info.name,30);
	cout<<"enter the bookcode of the book"<<endl;
	cin>>info.code;
	cin.ignore();
	cout<<"enter the author, DO NOT USE SPACES"<<endl;
	cin.getline(info.author,30);
	cout<<"enter the editor, DO NOT USE SPACES"<<endl;
	cin.getline(info.editor,20);
}


here is the class

1
2
3
4
5
6
7
8
9
10
class RecBookinfo{
	public:
		char name[30];
		int code;
		char author[30];
		char editor[30];
		int reserved;
		char reservedate[8];
		char reservename[20];
	}info;


there are no syntax errors or warnings when compiling.
my code keeps generating a loop when it is not supposed to.
What does that mean? I don't see a loop in the code you provided.
that it is outputing my main over and over again.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int menu()
{
	int choise;
	bool check = false;
		while (check == false){
		cout<<"choose an oprion by pressing the number, then press enter"<<endl;
		cout<<"1:  add book"<<endl;
		cout<<"2:  search book"<<endl;
		cout<<"3:  check reservements"<<endl;
		cout<<"4:  make reservement"<<endl;
		cout<<"5:  exit program and save changes"<<endl;
		cin>>choise;
		if (choise>0 && choise<6){
			check=true;
			return choise;
		}
		else 
		cout<<"please insert a number between 1 and 5 to make your choise"<<endl;
	}
	return 0;	
}


the outputs in here get outputed unlimitedly
use the cin.ignore after the cin.getline(info.name,30);
before the cin>>info.code;

if that does not work give us the main function code as it could be happening there.
that does not work.

i hope with
the main function code
you mean all of the code. if so:

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
#include <iostream>
#include <fstream>

using namespace std;

int menu();
void Add_Book();
void Search_Book();
void Reserve_Check();
void Add_Reservement();

class RecBookinfo{
	public:
		char name[30];
		int code;
		char author[30];
		char editor[30];
		int reserved;
		char reservedate[8];
		char reservename[20];
	}info;

int main(){
	ofstream Books;
	ofstream Code;
	ofstream Author;
	ofstream Editor;
	ofstream Reserved;
	ofstream ReserveDate;
	ofstream ReserveName;
	int choise;
	bool exit = false;
	Books.open ("bin/Books.txt");
		if (Books==NULL){
			cout<<"error opening file: /bin/Books.txt (file not found) "<<endl;
			return 0; }
	Code.open ("bin/Code.txt");
		if (Code==NULL){
			cout<<"error opening file: /bin/Code.txt (file not found) "<<endl;
			return 0; }
	Author.open ("bin/Author.txt");
		if (Author==NULL){
			cout<<"error opening file: /bin/Author.txt (file not found) "<<endl;
			return 0; }
	Editor.open ("bin/Editor.txt");
		if (Editor==NULL){
			cout<<"error opening file: /bin/Editor.txt (file not found) "<<endl;
			return 0; }
	Reserved.open ("bin/Reserved.txt");
		if (Reserved==NULL){
			cout<<"error opening file: /bin/input.txt (file not found) "<<endl;
			return 0; }	
	ReserveDate.open ("bin/ReserveDate.txt");
		if (ReserveDate==NULL){
			cout<<"error opening file: /bin/ReserveDate.txt (file not found) "<<endl;
			return 0; }	
	ReserveName.open ("bin/ReserveName.txt");
		if (ReserveName==NULL){
			cout<<"error opening file: /bin/ReserveName.txt (file not found) "<<endl;
			return 0; }
			while (exit == false){
		choise=menu();
		switch (choise){
			case 1:
				Add_Book();
				break;
			case 2:
				Search_Book();
				break;
			case 3:
				Reserve_Check();
				break;
			case 4:
				Add_Reservement();
				break;
			case 5:
				exit=true;
				break;
			default:
				break;
		}
	}
	return 0;
}

int menu()
{
	int choise;
	bool check = false;
		while (check == false){
		cout<<"choose an oprion by pressing the number, then press enter"<<endl;
		cout<<"1:  add book"<<endl;
		cout<<"2:  search book"<<endl;
		cout<<"3:  check reservements"<<endl;
		cout<<"4:  make reservement"<<endl;
		cout<<"5:  exit program and save changes"<<endl;
		cin>>choise;
		if (choise>0 && choise<6){
			check=true;
			return choise;
		}
		else 
		cout<<"please insert a number between 1 and 5 to make your choise"<<endl;
	}
	return 0;	
}

void Add_Book()
{
	cout<<"enter the name of the book you want to add, DO NOT USE SPACES"<<endl;
	cin.getline(info.name,30);
	cin.ignore();
	cout<<"enter the bookcode of the book"<<endl;
	cin>>info.code;
	cout<<"enter the author, DO NOT USE SPACES"<<endl;
	cin.getline(info.author,30);
	cout<<"enter the editor, DO NOT USE SPACES"<<endl;
	cin.getline(info.editor,20);
}

void Search_Book()
{
	
}
	
void Reserve_Check()
{
	
}

void Add_Reservement()
{
	
}


I'm getting errors relating to comparing an ofstream type to an int type in your if statements line 34 onwards.

Also choise is actually spelt choice but that is not code related.
Last edited on
i dont get theme because i actually have the files in that place and in the map called bin
Last edited on
ok I think I see the problem you enter the while loop here

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
while (exit == false)
	{
		choise = menu();
		switch (choise)
		{
			case 1:
				Add_Book();
				break;
			case 2:
				Search_Book();
				break;
			case 3:
				Reserve_Check();
				break;
			case 4:
				Add_Reservement();
				break;
			case 5:
				exit = true;
				break;
			default:
				break;
		}
	}
	return 0;
}


then it goes into menu function if the number is in range 1- 5 it returns choice then return and switches to correct case but it also assigns choice the number, once it returns from the function add book or whatever it jumps back and hit the end brace of while loop then check jumps back into the menu function.

your problem lies in the fact that your bool check is in fact a new variable in a smaller scope in the menu function and is not used at all when entering into menu function
Last edited on
everytime you enter the menu function bool check is set to false make it a static and that should work
actually that wont work either as it will remain true once set
ok I think if you change the while loop in menu function into a do while loop and also make the boolcheck a static that will 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
int menu()
{
	int choise;
	static bool check = false;
	do 
	{
		cout << "choose an oprion by pressing the number, then press enter" << endl;
		cout << "1:  add book" << endl;
		cout << "2:  search book" << endl;
		cout << "3:  check reservements" << endl;
		cout << "4:  make reservement" << endl;
		cout << "5:  exit program and save changes" << endl;
		cin >> choise;
		if (choise>0 && choise<6)
		{
			check = true;
			return choise;
		}
		else
		{
			cout << "please insert a number between 1 and 5 to make your choise" << endl;
			check = false; // this line too
		}
	}
	while (check == false);
	return 0;
}
Last edited on
thank you !! I will change it to see how it works out in the next informatics lesson.
On second thoughts I don't know if I diagnosed correctly. Keep in mind that may not be correct. as it was quite confusing with things like this in it.


while (check == false)
while (exit == false)


Use the bools appropriately. what you have here is the opposite of false. Use the bools as they are if its a simple matter of false or true. Something like this.

 
while (check)


It make for easier to read code instead of backtracking trying to find out what's actually true or false. The human mind can only keep track of 5-9 things at one time and this just complicates it.

Keep in mind you will have to alter what is actually true and what is actually false as you appear to have the opposite.
Last edited on
Topic archived. No new replies allowed.