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;
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.
#include <iostream>
#include <cmath>
#include <string>
usingnamespace 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;
}
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.
#include <iostream>
#include <string>
#include <cstdlib>
usingnamespace 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;
}
}
}
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.
#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' ) ;
}