I'm trying to make it so that when the user enters anything below 0 mins it'll say that its invalid and not give a price. I know I'm supposed to use an if statement, I just don't know where I'm supposed to place it. I've tried different places, all not giving me the results that I need.
#include <iostream>
usingnamespace std;
int main() {
float a = 39.99;
float b = 59.99;
float c = 69.99;
int min;
int choice;
cout <<"What is your package plan? \n"
<< "1. $39.99 \n"
<< "2. $59.99 \n"
<< "3. $69.99 \n";
cin >> choice;
cout<< "How many minutes have you gone over?"<<endl;
cin>>min;
float price;
switch(choice)
{
case 1:
price = a + (.45 * min);
cout<< "your price is "<<price<<endl;
break;
case 2:
price = b + (.45 * min);
cout<< "your price is "<<price<<endl;
break;
case 3:
price = c + (.45 * min);
cout<< "your price is "<<price<<endl;
break;
default:
cout<<"You chose an invalid section from the list or have chosen an invalid number of minutes. "<<endl;
}
return 0;
Lines 6 - 8 would work better as a "constexpr" or at least a "const". These variables should not be changed by the program. Also double is preferred over float.
Line 9 it is always a good idea to initialize your variables.
Lines 32, 37 and 42 the ".45" should be a constant variable. It makes this easier to change in the future.