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.
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;
}
#include <iostream>
#include <fstream>
usingnamespace 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()
{
}
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
int menu()
{
int choise;
staticbool 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;
}
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.