If/else statement not assigning number to variable
Mar 21, 2016 at 4:37pm UTC
I am creating a program that calculates the cost of a dog license depending on if the dog is fixed, and the weight of the dog. When I run the program, I can not get the correct variable assigned to "cost" to calculate total. Can someone tell me where I am going wrong?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
// Cassandr Hamric
//Topic 7 Horse Stable
//This Program calculates the pounds of food to feed a horse given their weight category.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
int y=0, Y=0, n=0, N=0, years, cost, total;
float weight;
string name, size;
char fixed;
cout<<"Dog License (Spayed / Neutered)" <<endl;
cout<<"-------------------------------------------------------------" <<endl;
cout <<left;
cout <<setw(20)<<"Size (Pounds)" <<setw(10)<<"Fixed"
<<setw(10)<<"Not Fixed" <<endl; // Row headings
cout<<"-------------------------------------------------------------" <<endl;
cout <<setw(20) << "Small 0-15" <<setw(10) <<"$50" <<setw(10) <<"$75" <<setw(10) <<endl;
cout<<setw(20) << "Medium 16-45" <<setw(10) <<"$100" <<setw(10) <<"$150" <<setw(10) <<endl; // Dog Pricing
cout<<setw(20) << "Large 46+" <<setw(10) <<"$150" <<setw(10) <<"$200" <<setw(10) <<endl;
cout<<"-------------------------------------------------------------" <<endl;
cout <<"Is your dog spayed/neutered? Y/N " ;
cin >>fixed;
if (fixed != 'Y' && fixed != 'y' && fixed != 'N' && fixed != 'n' ) // Validate input allowing only YyNn answers
{
cout <<"Invalid entry. Please restart program & Enter Y or N " <<endl;
system("pause" );
return 0;
}
cout <<"Enter your dog's weight in pounds " ; //Get Dogs Weight
cin >>weight;
if (fixed == Y && fixed == y)
{
if (weight <= 15){
cost =50 ;}
else if (weight <=45){
cost = 100 ;}
else if (weight >=46) {
cost = 150 ;}
} // assign cost depending on spayed/neutered and weight
if ( fixed == N && fixed == n)
{
if (weight <=15){
cost = 75;}
else if (weight <=45){
cost = 150;}
else if (weight >=46){
cost = 200;}
}
cout <<"How many years would you like your license for?" ; //Get years license good for
cin >> years;
total = cost * years; // calculate total of order
cout <<"The total for your order is " <<total <<endl; //output total of order
cout <<"What is your Dog's Name? " ; //Get dogs name
cin >> name;
cout <<name <<"'s License will be arriving in the mail shortly" <<endl;
system("pause" );
return 0;
}
Mar 21, 2016 at 4:43pm UTC
(fixed == Y && fixed == y)
Take a good long look at this line of code.
Mar 21, 2016 at 5:27pm UTC
Did you also consider the actual logic of that line? How can fixed equal 'Y' and be equal 'y' at the same time? Perhaps you meant to use the OR operator instead?
Topic archived. No new replies allowed.