if nested condition is met, calculate results not showing results C++

69 Views Asked by At

I want the program to display annual raise for years 1,2,3.

User enters year user enters annualSalary,

then if conditions are true the calculations are below.

But when I run it is not showing the calculation for any given year I input. Why? I went back over a few times and trying to determine what I am missing?

#include "stdafx.h"
#include <iomanip>
#include<iostream>
using namespace std;

int main()
{
    double annualSalary = 0; 
    int year = 0;
    double rate3 = 0.03; 
    double rate4 = 0.04; 
    double rate5 = 0.05; 
    double rate6 = 0.06;  
    double annualRaise = 0; 


    cout << fixed << setprecision(0); 

    cout << "enter current year (1 to 3) ";
    cin >> year;
    cout << "enter annual Salary";
    cin >> annualSalary; 


    if (year = 1)
        annualRaise = annualSalary * rate3;
    else if
        (year = 2)
        annualRaise = annualSalary * rate4;
    else if
        (year = 3)
        annualRaise = annualSalary * rate5; 

    return 0;
}
2

There are 2 best solutions below

2
Shadi On BEST ANSWER

Use == to compare not = and add a cout to see the result.

if (year == 1)
        annualRaise = annualSalary * rate3;
    else if
        (year == 2)
        annualRaise = annualSalary * rate4;
    else if
        (year == 3)
        annualRaise = annualSalary * rate5;

cout << annualRaise;
0
Bo Halim On

You need to add a print cout to see the result of your work :

if (year == 1)
{
    annualRaise = annualSalary * rate3;
    cout << "Salary : " << annualRaise << endl; // output the value
}


else if(year == 2)
{
    annualRaise = annualSalary * rate4;
    cout << "Salary : " << annualRaise << endl; // output the value
}



else if(year == 3)
{
    annualRaise = annualSalary * rate5;
    cout << "Salary : " << annualRaise << endl; // output the value
}


system("Pause"); //To be able to keep the console window open