0

Hi everyone, I was wondering if anyone could help me figure out this issue I am having with a program I have written. The problem is after you type in the number of days you want to rent the car for if it is less than 0 or greater than 30 it is supposed to loop back to What class of car do you want and give an message such as invalid entry. I have tried all type of loops, while loops, do while loops if, if , and I still cannot figure it out the program is written below. Thanks in advance for your help it has been kicking my butt. John

#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main (){

    cout.setf(std::ios_base::fixed);   // Using precision function to limit decimal output to 2
    cout.setf(std::ios_base::showpoint);
    cout.precision(2);




    char carType = 0;   //  character type will be car type selected
    int days = (0 <= 30);


    cout << "Welcome to the Car Rental Program.\n";

    do {

        cout << "What class of car do you want (A, B, C, or D)? ";  // Asking user what type of car they would like to choose.
        cin >> carType;   // User enters car type
        switch (carType){
        case 'a':
        case 'A':
            cout << "How many days will you keep the car (no more than 30 days)? ";  // Asking user number of days car is needed
            cin >> days;
            cout << "Your price will be $" << 30*days*1.06 << ". Is this OK (Y or N)?";
            cin >> carType;
            break;
        case 'b':
        case 'B':
            cout << "How many days will you keep the car (no more than 30 days)? ";
            cin >> days;
            cout << "Your price will be $" << 40*days*1.06 << ". Is this OK (Y or N)?";
            cin >> carType;
            break;
        case 'c':
        case 'C':
            cout << "How many days will you keep the car (no more than 30 days)? ";
            cin >> days;
            cout << "Your price will be $" << 55*days*1.06 << ". Is this OK (Y or N)?";
            cin >> carType;
            break;
        case 'd':
        case 'D':
            cout << "How many days will you keep the car (no more than 30 days)? ";
            cin >> days;
            cout << "Your price will be $" << 80*days*1.06 << ". Is this OK (Y or N)?";
            cin >> carType;
            break;
        default :
            cout << "Invalid Entry please try again." << carType<< "\n"; // If user selects character other than a, b, c, d, in lower case or caps will get error message
        }
    }   while (carType !='Y' && carType != 'y');  // if user chooses Y they will get a thank you message can either be caps or lower case.

        cout << "Thank You!";


    return 0;
}

Thanks again for all the help,

John

Edited by Dani: Formatting fixed

2
Contributors
1
Reply
4
Views
7 Years
Discussion Span
Last Post by restrictment
0

Please use code tags, and all of the "text" in the switch statement is not needed. You just need to change the amount within the switch. you also have a few unneeded headers. Also, your decimal allocation code is a bit long, and you can shorten that up to one line.

As for adding a loop, a simple do-while loop would suffice.

Here is your code with these corrections implemented. Please take the time to figure out the changes I made, as they were quite simple, but important/effective. =]

#include <iostream>
#include <iomanip>
using namespace std;
int main (){
	cout << fixed << setprecision(2); // Using precision function to limit decimal output to 2
	char carType; // character type will be car type selected
	int days, amount;
	cout << "Welcome to the Car Rental Program.\n";
	do {
		cout << "What class of car do you want (A, B, C, or D)? "; // Asking user what type of car they would like to choose.
		cin >> carType; // User enters car type
		switch (carType){
case 'a':
case 'A':
	amount = 30;
	break;
case 'b':
case 'B':
	amount = 40;
	break;
case 'c':
case 'C':
	amount = 55;
	break;
case 'd':
case 'D':
	amount = 80;
	break;
default :
	cout << "Invalid Entry please try again." << carType << "\n"; // If user selects character other than a, b, c, d, in lower case or caps will get error message
		}
		do{
		cout << "How many days will you keep the car (no more than 30 days)? ";
		cin >> days;
		}while(days > 30 || days < 1);
		cout << "Your price will be $" << amount*days*1.06 << ". Is this OK (Y or N)?";
		cin >> carType;
	}while (carType !='Y' && carType != 'y'); // if user chooses Y they will get a thank you message can either be caps or lower case.
	cout << "Thank You!\n\n";
	return 0;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.