c++if-statementmultiple-results

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


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;
}

Solution

  • 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;