cout not working?

hey i have a small problem in my program that i cant figure out.

the compiler dose not seem to give any errors whatsoever about it.

here is the code of the program now on line 25 i called the function calcIncChange() if you go on line 55 where the function is defined and read the function. you can see that i have told it to print out something with the cout statement but it dose not do that i need to know why.

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
#include <iostream>
#include <ctype.h>
#include <cstdlib>
#include <limits>

using namespace std;

// Declaring Functions.
void getAnnualIncome(int[]);
void calcIncChange(int[],int[]);

// Declaring a global variable.
int year = 0;

main(){
    cout << "\t\t\t***** ABC Company Income Sheet *****\n\n\n";

    // Declaring the variables and arrays that will be used in the program.
    int annualIncome[10] = {0},incChange[10] = {0};

    // Calling the function to get the annual income for 10 years from the user.
    getAnnualIncome(annualIncome);

    // Calling the function to calculate increase or decrease in the annual income of the company for each year.
    calcIncChange(annualIncome,incChange);

}

// Function to get annual income for 10 years.
void getAnnualIncome(int annualIncome[]){
    // NOTE: year is a global variable so i am giving the value 2000 to year in this function and will reset the value to 2000 in the next function.
    year = 2000;

    // Prompting the user to enter the company's annual income for 10 years one by one.
    cout << "Please enter the income for past 10 years <In Million Only>: " << "\n\n";

    for(int i = 0; i < 10; i++){
        cout << "Income for year " << year << ": \t";
        // This While loop checks if the character entered is an alphabet and takes the apporpriate action.
        while( !(cin >> annualIncome[i]) ){
            cout << "Invalid Entry Please re-enter for " << year << ": ";
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }
        // This while loop checks if the number is negetive and takes apporpriate action.
        while(annualIncome[i] < 0){
            cout << "Invalid Entry please re-enter for " << year << ": ";
            cin >> annualIncome[i];
        }
        year++;
    }
}

// Function to calculate increase or decrease in the annual income of the company for each year.
void calcIncChange(int annualIncome[],int incChange[]){
    year = 2000;

    for(int j = 0; j < 10; j++){
        if(annualIncome[j] > annualIncome[j++]){
            incChange[j] = annualIncome[j] - annualIncome[j++];
            cout << "Income decrease in year " << year << ": \t" << incChange[j] << " million";
        }
        else if(annualIncome[j] < annualIncome[j++]){
            incChange[j] = annualIncome[j++] - annualIncome[j];
            cout << "Income increase in year " << year << ": \t" << incChange[j] << " million";
        }
    }
}
The couts works fine, the problem is that it never enters the if statements that contain the couts.

In line 59
if(annualIncome[j] > annualIncome[j++]){
Here you are comparing annualIncome[j] to annualIncome[j] and then incrementing j. Since they are equal, the if statments are not satisfied and you are not going to output that cout.

Since you already increment j in the for loop, I don't think this was your intent. I recommend replacing this function with the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void calcIncChange(int annualIncome[],int incChange[]){
    year = 2000;

    for(int j = 0; j < 10; j++){
        if(annualIncome[j] > annualIncome[j+1]){
            incChange[j] = annualIncome[j] - annualIncome[j+1];
            cout << "Income decrease in year " << year << ": \t" << incChange[j] << " million";
        }
        else if(annualIncome[j] < annualIncome[j+1]){
            incChange[j] = annualIncome[j+1] - annualIncome[j];
            cout << "Income increase in year " << year << ": \t" << incChange[j] << " million";
        }
    }
}


If we increment j inside the if statements, then we really have no idea what j is going to be at the end of each loop and it starts to get messy, just check the value of j+1 not j++.

Edit: if you REALLY want to increment j like this, then use ++j. This will increment j and then return its value. So line 59 would compare annualIncome[j] with annualIncome[j+1] and j would remain one higher than before.
Last edited on
worked thanks. i thought that j++ and j+1 would mean the same thing because i was comparing annualIncome[j] with annualIncome[j+1].but j++ did not do the same thing.

thanks again apreciated
Topic archived. No new replies allowed.