If else if

Having trouble with the output for this program. Need it to prompt an error when inputting a negative number or anything over 9. when i try this, it gives me a runtime check failure 3.

#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;

int main()
{
string cust_name;
int jetpack_num;
double jetpack_cost;
double jetpack_cost2;
double total_cost;
double tax;



cout<< "Welcome to APSU jetpack sales." <<endl<< endl;
cout<< "We sell jetpacks." <<endl<<endl;
cout<< "Jetpacks are on sale for:"<<endl;
cout<< "33.00 each if at least 3 are purchased." <<endl;
cout<< "22.00 each if at least 3 but fewer than 6 are purchased." <<endl;
cout<< "11.00 each if at least 6 but fewer than or equal to 9 are purchased."<<endl<<endl;
cout<< "You may purchase at most 9 jetpacks."<<endl<<endl;
cout<< "Jetpack purchase for ";
getline (cin, cust_name);
cout<<endl;
cout<< "Enter number of jetpacks to purchase: ";
cin>> jetpack_num;
cout<< endl;

if (jetpack_num<3&&jetpack_num>0)
jetpack_cost=33.00;
else if (jetpack_num>=3&&jetpack_num<6)
jetpack_cost=22.00;
else if (jetpack_num>=6&&jetpack_num<=9)
jetpack_cost=11.00;
else if (jetpack_num<0)
cout<<"Error! Number of jetpacks cannot be negative\n";
else if (jetpack_num>9)
cout<< "Error! Maximum number allowed to purchase is 9\n";


jetpack_cost2= jetpack_cost*jetpack_num;
tax= .095*jetpack_cost2;
total_cost= jetpack_cost2+tax;

cout<< endl<<endl;
cout<< "This order was submitted by "<< setw(40)<<cust_name<<endl<<endl;
cout<< "Number of jetpacks Purchased: " <<setw(21)<< jetpack_num<<setprecision(2)<<fixed<<endl;
cout<< "Cost of a single jetpack: " <<setw(25)<<jetpack_cost<<setprecision(2)<<fixed<<endl;
cout<< "Cost of jetpacks: " <<setw(33)<<jetpack_cost2<<setprecision(2)<<fixed<< endl;
cout<< "Tax: "<<setw(46)<<tax<<setprecision(2)<<fixed<<endl;

cout<< "Total Cost of Order: " <<setw(30)<<total_cost<< endl<<endl<<endl;

cout<< "Thank you for your order."<<endl<<endl<<endl;




system("PAUSE");
return 0;
}
try setting initial values for the variables. Also put spaces before and after &&
if (jetpack_num<3 && jetpack_num>0)

Also, you are not checking for the condition (jetpack_num == 0);

And beware, the program will not stop even if the user enters a number <0 or >9. The program will continue after giving the error message. Something you may want to change.
Last edited on
i just did what you suggested besides setting initial values. i know this would work, but as this is an assignment for a class, the program has read in user input.

and i noticed that. the error message appears above the random output that is shown after the runtime check failure crap. how do i end the program after the user enters a number <0 or >9?
Last edited on
to end the program if user enters a number <0 or >9, you can do this.
1
2
3
4
5
else if ((jetpack_num<0) || (jetpack_num>9)) {
 cout<<"Error! Number of jetpacks should be between 0 and 9\n";
 system("PAUSE");
 return 0;
}
Thanks so much!
Topic archived. No new replies allowed.