So i just wrote a program reimplemented in C++ that i originally wrote in VB.net. Only difference is that this in terminal while that VB.net project is gui. But anyways how can i keep this if statement going until the user choices the right selection? Because i entered 4 the if statement caught it was out of the valid range, the else program also caught it but the program just terminanted.
//This is the main file for the program. The program is a reimplmentation of an old vb.net project i did last year. no need for arrays in this project just using descion structures and there is also no need to use classes as i am practicing descion structures.
//(C) John Nelson 2012
#include <iostream>
#include <fstream>
usingnamespace std;
int main(){
//create a writer to save to a text file
ofstream writer;
writer.open("rental.text");
//this choice will be used for the descion structure for the user to input what car he wants.
int choice = 0;
double days;
//these constant variables are the fixed price cost it is to rent these viechials
constdouble gp40mc = 180;
constdouble F40PH = 150;
constdouble Spaceship = 1080;
double total;
cout << "Hello What Veichal do you want to rent enter a number 1,2,3 and make a selection." << endl << "1.GP40MC." << endl << "2.F40ph:." << endl << "3.Spaceship:" << endl;
cin >> choice;
if (choice >= 1 && choice <= 3);
else{
cout << "You did not enter the right choice please enter it again";
cout << "Hello What car do you want to rent enter a number 1,2,3 and make a selection." << endl << "1.GP40MC." << endl << "2.F40ph:." << endl << "3.Spaceship:" << endl;
cin >> choice;}
if (choice == 1){
cout << "How many days are you going to be renting the GP40MC for? There is no limit on the amount of days but the cost to rent it is $180 a day gas is included as well!";
cin >> days;
total = days * gp40mc;
cout << "The total cost of your rental is $" << total << endl << "Thank you for shopping here your receipt is also in the text file good day sir!" << endl;
writer << "Your Receipt: " <<endl << "Vehcial Rental: GP40MC Days Rental:" << days << "Final Cost:" << total << endl;}
elseif (choice == 2){
cout << "How many days are you going to be renting the F40PH for? There is no limit on the amount of days but the cost to rent it is $150 a day gas is included as well!";
cin >> days;
total = days * F40PH;
cout << "The total cost of your rental is $" << total << endl << "Thank you for shopping here your receipt is also in the text file good day sir!" << endl;
writer << "Your Receipt: " <<endl << "Vehcial Rental: F40PH Days Rental:" << days << "Final Cost:" << total << endl;}
elseif (choice == 3){
cout << "How many days are you going to be renting the Spaceship for? There is no limit on the amount of days but the cost to rent it is $1080 a day gas is included as well!";
cin >> days;
total = days * Spaceship;
cout << "The total cost of your rental is $" << total << endl << "Thank you for shopping here your receipt is also in the text file good day sir!" << endl;
writer << "Your Receipt: " <<endl << "Vehcial Rental: Spaceship Days Rental:" << days << "Final Cost:" << total << endl;
};
}
You got some weird spacing. For selections like this, a switch statement would probably be better (even if you want to use "if", you are doing it in a rather strange fashion).
Example:
1 2 3 4 5 6 7 8 9 10
switch(value) {
case 1:
do_something();
break;
case 2:
do_something_else();
break;
default:
none_of_the_above();
}
You can only "switch" on integers though (like char, short, int, long...)
As for the other thing: Just wrap the whole thing in a loop.