My Switch case codes executes without reason???!

javascript:tx('code')
Hey Guys, beginner here... for some reason when I run this program after I enter in the file name it starts to execute all the commands in the switch case without displaying the menu and allowing the user to enter in an option? Thank you for any help.
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
#include <iostream>
#include <string>
#include <fstream> // INcluding c++ Libraries
#include "book.h"
#include "Database.h"



using namespace std;
int menu();
int main()
{

	Book bk;
	Database database;
	string filename;

	cout << "Enter in filename with a .txt" << endl;
	getline(cin, filename);
	ifstream fileN(filename.c_str());
	if (!fileN.fail())
	{
		database.load(fileN);
		fileN.close();
		int Choice;
		while (Choice != 3)
		{
			Choice = menu();
			switch (Choice)
			{
			case 1:  // the code that will be executed if the user enters in"1"
				cin >> bk;
				database.add(bk);
				break;





			case 2:
				cout << "DATABASE:" << endl;
				database.displayall();

				break;

			case 3:
				cout << "-------------------------------" << endl;
				cout << " Program Terminated " << endl;
				cout << "-------------------------------" << endl;
				break;
			}

		}
		

	
	ofstream fout(filename.c_str());
	if (!fout.fail())
	{
		database.save(fout);
	}
	else
	{
		cout << "ERROR FILE CANT BE OPENED CHECK FILE TYPE" << endl;
	}

	fout.close();

}
else {
	cout << " Creating new file" << endl;
	ofstream new_file(filename.c_str());
	new_file.close();
	main();

}
return 0;
}
	


int menu()
{
	int Choice;
	cout << "ADMINISTRATOR MENU" << endl;
	cout << "1.CREATE Book" << endl; // displaying the options for the admin menu
	cout << "2.DISPLAY ALL STUDENTS RECORD" << endl;
	cout << "3.To EXIT Program" << endl;
	cin >> Choice;
	return Choice;

}
Last edited on

1
2
3
int Choice;
while (Choice != 3)
{

You have undefined behavior, because Choice is not given an initial value.

1
2
3
4
5
6
else {
    cout << " Creating new file" << endl;
    ofstream new_file(filename.c_str());
    new_file.close();
    main();
}

Calling main to go back to the beginning is illegal! Use a loop construct instead.

Other than that, I don't see any specific thing that would be causing a problem. You provide incomplete code, so it would be guesswork from here.

EDIT: Try to add
cin.ignore(); after every call to getline().

This will discard the remaining '\n' in the stream, which might be causing problems when mixing getline and cin.
Last edited on
Doesnt seem to fix the problem,

Here are my two other class cpp files:

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
#include "Database.h"
#include <iostream>
#include <algorithm>
#include <fstream>
#include <string>
using namespace std;

Database::Database()
{
	used = 0;
	capacity = 10;
	data = new Book[capacity];
}

Database::Database(const Database& other)
{
	used = other.used;
	capacity = other.capacity;
	data = new Book[capacity];
	copy(other.data, other.data + used, data);


}


Database::~Database()
{
	delete[]data; // deletes the pointer
}

void Database::operator=(const Database & other)
{
	if (&other == this) {
		return;
	}
	delete[]data;
	capacity = other.capacity;
	used = other.used;
	data = new Book[capacity];
	copy(other.data, other.data + used, data);
}

void Database::search(std::string name)
{
	int UserFound = 0;
	for (int i = 0; i < used; i++)
	{

		if (data[i].get_BookName() == name)
		{

			cout << "Book is part of Library Stock" << endl;
			data[i].output(cout);
			UserFound++;
		}
		if (UserFound == 0)
		{
			cout << " Book not in stock please ask Librarian" << endl;
		}


	}
}

void Database::add(const Book & emp)
{
}

void Database::displayall()
{
	for (int i = 0; i < used; i++)
	{
		data[i].output(cout);


	}
}

void Database::save(std::ostream & outs)
{
	for (int i = 0; i < used; i++)
	{
		outs << data[i];
	}

}

void Database::load(std::istream & ins)
{
	Book bk;
	while (ins >> bk)
	{

		data[used] = bk;
		used++;
	}

}


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
#include "book.h"
#include <iostream>
#include <string>

// Including standard libraries
using namespace std;


Book::Book()
{
	ID = 0;


}

Book::Book(string New_BookName, string New_AuthorName, string New_Genre, int New_BookNumber)
{
    Name = New_BookName;
	Author = New_AuthorName;
	Genre = New_Genre;
	ID = New_BookNumber;

}

Book::~Book()
{
}

void Book::output(ostream& outs)
{
	cout << "Please fill in the following fields" << endl;
	cout << "---------------------------------------------" << endl;
		outs << "Book: " << Name << endl;
		outs << "Author: " << Author << endl;
		outs << "Genre: " << Genre << endl;
		outs << "ID: " << ID << endl;
	

}

void Book::input(istream & ins)
{
	cout << "Name: " << endl;
	cin >> Name;
	cout << "Author: " << endl;
	cin >> Author;
	cout << "Genre: " << endl;
	cin >> Genre;
	cout << "ID: " << endl;
	cin >> ID;


}

ostream& operator <<(ostream& outs, Book& bk)
{
	bk.output(outs);
	return outs;
}
istream& operator >>(istream& ins, Book& bk)
{
	bk.input(ins);
	return ins;
}
Last edited on
Topic archived. No new replies allowed.