I have a new question on how to make the program start from the beginning

This is what I have so far. So basically I need Fine to display whatever number as long as it's below 5 and if it's 5 or greater than 5 it has to display 5. Thanks in advance!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  switch (booktype)
	{
		case Pback	: cout << "Enter the number of days overdue: \n";
					cin  >> daysoverdue;
						while (daysoverdue < 0)																		
							{ 
								cout << "Please enter a number greater than or equal to zero: ";
								cin >> daysoverdue;
							}
						fine = daysoverdue * .25;
						if (fine >= 5)
						{
							cout << "Your fine is: " << fine;
						}

					break;
Last edited on
1
2
3
fine = daysoverdue * 0.25 ;
if( fine > 5.0 ) fine = 5.0 ; 
std::cout << "Your fine is: " << fine << '\n' ;

Hey thank you for responding. I need help making my program restart. By that I mean making it start from the beginning if I put in Y for Yes towards the bottom.
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
#include <iostream>
#include <cmath>
#include <string>

using namespace std;


string name;																										// Variables
string ad;
string booktitle;

char restart;

int booktype;

double cardnum, charges, daysoverdue, fine;

enum BookType {Pback = 1, PbackB, Mag, HBook};
int main()
{
	cout << "Enter your library card number:" << endl;
	cin >> cardnum;
	if (cardnum >= 0000 && cardnum <= 9999)																			// Makes sure the card number is above 0000 and below 9999 and if not it it makes you enter it again
	{}

	else
	{
		cout << "\nYour card number is incorrect.\n" << "Please enter it again: " << endl;
		cin >> cardnum;
	}

	cardnum = cardnum;

	cin.ignore();
	cin.clear();

	cout << "\nEnter your name:" << endl;
	getline(cin, name);

	cout << "\nEnter your address:" << endl;
	getline(cin, ad);

	cout << "\nEnter the title of the book:" << endl;
	getline(cin, booktitle);



	/*cout << "\nIs this information correct?\n" << "\n"<< "Card Number: " << cardnum << "\n"<< "Name: " << name << "\n"<< "Address: " << ad << "\n"<< "Book Title: " << booktitle << endl;
	system("pause");*/


	int Booktype();

	cout << "\n1. PaperBack-Regular\n";
	cout << "2. PaperBack-Bestseller\n";
	cout << "3. Magazine\n";
	cout << "4. Hardcover\n";
	cout << "\nEnter number corresponding to the type of book(1 - 4): \n";
	cin >> booktype;
	while (booktype > 4)																							// Make sure the number entered corresponds to one of the numbers shown in the menu.		
	{
		cout << "Enter a number corresponding to the type of book(1 - 4): \n";
		cin >> booktype;
	}
	cin.ignore();
	cin.clear();

	switch (booktype)
	{
		case Pback	: cout << "Enter the number of days overdue: \n";
					cin  >> daysoverdue;
						while (daysoverdue < 0)																		// Make sures the number entered is above 0.
							{ 
								cout << "Please enter a number greater than or equal to zero: ";
								cin >> daysoverdue;
							}
						fine = daysoverdue * .25;
							if (fine > 5.0) fine = 5.0;
							{
								cout << "\nYour fine is: $" << fine << endl;
							}

					break;
		case PbackB	: cout << "Enter the number of days overdue: \n";
					  cin >> daysoverdue;
						while (daysoverdue < 0)
							{
								cout << "Please enter a number greater than or equal to zero: ";
								cin >> daysoverdue;
							}
						fine = daysoverdue * .50;
							if (fine > 10.0) fine = 10.0;
							{
								cout << "\nYour fine is: $" << fine << endl;
							}

					  break;
		case Mag	: cout << "Enter the number of days overdue: \n";
					  cin >> daysoverdue;
						while (daysoverdue < 0)
							{
								cout << "Please enter a number greater than or equal to zero: ";
								cin >> daysoverdue;
							}
						fine = daysoverdue * .25;
						if (fine > 4.0) fine = 4.0;
						{
							cout << "\nYour fine is: $" << fine << endl;
						}
					  break;
		case HBook	: cout << "Enter the number of days overdue: \n";
					  cin >> daysoverdue;
						while (daysoverdue < 0)
							{
								cout << "Please enter a number greater than or equal to zero: ";
								cin >> daysoverdue;
							}
						fine = daysoverdue * .30;
						if (fine > 20.0) fine = 20.0;
						{
							cout << "\nYour fine is: $" << fine << endl;
						}
					  break;

		system("pause");

	}

	cout << "Would you like to perform another transaction? (Y/N)" << endl;
	cin >> restart;

	system("pause");
	return 0;


	}
Hi I added
1
2
3
4
5
6
cout << "Would you like to perform another transaction? (Y/N)" << endl;
	cin >> restart;
	if (restart == 'Y' || restart == 'y');
	{

	}


But I still need help make the program start from the beginning again.
Without a rewrite of your code (Strongly suggested) You could simply put

1
2
3
if (restart=='y'){
        main();
	}


starting @ line 134.

Your switch needs a default, and you should get rid of the global variables and learn to do functions.




A code rewrite could look something like this, but I'm going to warn you... though this works, I cheated a little. This is not the best code, however it'll give you an idea on functions.

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
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

// Prototypes

int getNumber(int);
string getName(string);
string getAdress(string);
string getTitle(string);
void getType();

int main()
{

// Variables are now local
string name;																										
string ad;
string booktitle;
char restart;
int cont=1;
int cardnum=0;

while (cont==1)
{
    cardnum = getNumber(cardnum);
    cin.ignore();
	name=getName(name);
	ad=getAdress(ad);
	booktitle=getTitle(booktitle);
	getType();
	cout << endl;

    /*cout << "\nIs this information correct?\n" << "\n"<< "Card Number: " << cardnum << "\n"<< "Name: " << name << "\n"<< "Address: " << ad << "\n"<< "Book Title: " << booktitle << endl;
	system("pause");*/
	cout << "Would you like to perform another transaction? (Y/N)" << endl;
	cin >> restart;
	if (restart!='y'){
        cont=0;
	}
}
	return 0;
	}

// Functions start here

int getNumber(int cardnum){
    cout << "Enter your library card number:" << endl;
	cin >> cardnum;
	if (cardnum<0 || cardnum>9999){
		cout << "\nYour card number is incorrect.\nPlease enter it again: " << endl;
		getNumber(cardnum);
	}
	else {
            return cardnum;
	}
}

string getName(string name){
    cout << "\nEnter your name:" << endl;
	getline(cin, name);
	return name;
}

string getAdress(string ad){
cout << "\nEnter your address:" << endl;
	getline(cin, ad);
	return ad;
}

string getTitle(string booktitle){
    cout << "\nEnter the title of the book:" << endl;
	getline(cin, booktitle);
	return booktitle;
}

void getType(){
    int booktype,daysoverdue=0;
    double fine =0.0;
    //enum BookType {Pback = 1, PbackB, Mag, HBook};

    cout << "\n1. PaperBack-Regular\n";
	cout << "2. PaperBack-Bestseller\n";
	cout << "3. Magazine\n";
	cout << "4. Hardcover\n";
	cout << "\nEnter number corresponding to the type of book(1 - 4): \n";

	cin >> booktype;

	if (booktype <1 || booktype >4)
        {
		cout << "Incorrect value: 1-4 please  ";
		getType();
		}
		else{

	switch (booktype){
                case 1:
                     cout << "Enter the number of days overdue: \n";
					 cin  >> daysoverdue;
						while (daysoverdue < 0)																		// Make sures the number entered is above 0.
							{
								cout << "Please enter a number greater than or equal to zero: ";
								cin >> daysoverdue;
							}
						fine = daysoverdue * .25;
							if (fine > 5.0) fine = 5.0;
							{
								cout << "\nYour fine is: $" << fine << endl;
							}

					break;
                case 2:
                     cout << "Enter the number of days overdue: \n";
					 cin >> daysoverdue;
						while (daysoverdue < 0)
							{
								cout << "Please enter a number greater than or equal to zero: ";
								cin >> daysoverdue;
							}
						fine = daysoverdue * .50;
							if (fine > 10.0) fine = 10.0;
							{
								cout << "\nYour fine is: $" << fine << endl;
							}

					  break;
                case 3:
                     cout << "Enter the number of days overdue: \n";
					 cin >> daysoverdue;
						while (daysoverdue < 0)
							{
								cout << "Please enter a number greater than or equal to zero: ";
								cin >> daysoverdue;
							}
						fine = daysoverdue * .25;
						if (fine > 4.0) fine = 4.0;
						{
							cout << "\nYour fine is: $" << fine << endl;
						}
					  break;
                case 4:
                     cout << "Enter the number of days overdue: \n";
                     cin >> daysoverdue;
						while (daysoverdue < 0)
							{
								cout << "Please enter a number greater than or equal to zero: ";
								cin >> daysoverdue;
							}
						fine = daysoverdue * .30;
						if (fine > 20.0) fine = 20.0;
						{
							cout << "\nYour fine is: $" << fine << endl;
						}
					  break;

					  default: cout << "Just give me all your cash!" << endl;
}
}
}
Last edited on
Thank you for the suggestions I was just about to make everything neat and tidy. I added the restart and now it keeps restarting even when I don't enter Y for yes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main()
{
    char do_this_once_again ;

    do
    {
        // do something

        std::cout << "would you like to do this once again? (y/n)? " ;
        std::cin >> do_this_once_again ;
    }
    while( do_this_once_again == 'Y' || do_this_once_again == 'y' ) ;
}
Thank you so much pearlyman and JLBorges!!!!
pearlyman wrote:

Without a rewrite of your code (Strongly suggested) You could simply put
1
2
3
if (restart=='y'){
        main();
	}


No. Recursive calls to main are NOT allowed in C++.
Last edited on
Topic archived. No new replies allowed.