I keep looking at this code, and cant find out the problem...
I made this as part of a library of math functions. It asks the user to enter the number of numbers to subtract, then enter them. It should work, but for some reason doesn't.

void sub()
{
    int nums=0;
    int trks=0;
    int alnms[500];
    int ttls=0;
    int trkss = 1;
    
    printf("\n\nHow many numbers to subtract?\n");
    scanf("%d", &nums); 
    printf("\nEnter the numbers on each line\n");
    do
    {
        trks++;
        scanf("%d", &alnms[trks]);
    }
    while (trks<nums);

    trks=0;
    ttls=alnms[1];
    do
    {
        trks++;
        trkss++;
        ttls = ttls-alnms[trkss];
    }
    while (trks<nums);
    printf("\n\nThe total is: %d\n\n", ttls);

}

In your first do while loop the condition you have used is while( trks < nums ) WHen the program control enters the loop the value of trks is already 1 and the condition you are using is while( trks < nums ) .
So if the user enters that he wants to sum up 4 integers, you end up just askign 3 input from the user due to your faulty loop condition.

Just to let you konw, do while loops are confusing..better go with a for loop which makes the appearance of bugs in the program less.

In your first do while loop the condition you have used is while( trks < nums ) WHen the program control enters the loop the value of trks is already 1 and the condition you are using is while( trks < nums ) .
So if the user enters that he wants to sum up 4 integers, you end up just askign 3 input from the user due to your faulty loop condition.

Just to let you konw, do while loops are confusing..better go with a for loop which makes the appearance of bugs in the program less.

Even though its one, it should be fine, because

trks++;
        scanf("%d", &alnms[trks]);

will store it in alnms[1] then 2 etc. so it shouldnt affect it. Anyways, the problem is it always gives a negative number, thats the problem.

IF you give input like:
1 2 3 4 5

Your subtraction statement

sum = sum - alms[i] ;

will generate
-1
-1 - 2 = -3
-3 - 3 = -6

So the ans will be -ve if you supply only +ve numbers. Is that the ccase ? Or is the answer -ve even when the numbers are -ve.

Like
1 -3 4 -5 etc.

Please post a sample run of your program with input data as well as the answer.

Please post a sample run of your program with input data as well as the answer.

Alright i tried with 2 numbers and this is how it went:

How many numbers to subtract?
2

Enter the numbers on seperate lines.
10
5

The total is:
-5

So i thought it was just backwards so i tried switching the input, and got -10, so that isnt it...

Nevermind, SOS was right, the problem was because it was 1 going in, sorry :(

Nevermind, SOS was right, the problem was because it was 1 going in, sorry :(

Would you please elaborate so that the other people readign this thread would be enlightened and gain some knowledge?

Hoping for your explanation, so that other beginners get the help they want.

This article has been dead for over six months. Start a new discussion instead.