0

hello guys, super noob here!
id want to build a function which is adding number in array, and if number is out of expect, its return to main,
else, it go to info_add function.
its work fine if i type less than 3.
...but seems like adding number isnt work at all. i tryed to tpey 2 and 2 again. My expect is: i[1]'s value is 4 and function will stop,
but value wasnt increase at all.
i looking for what is cause, and figured out this isnt work at all.

i[a] += b;

any help?
Thanks and happy holiday!

void info_add();
void menu_start();
void info_check();
static int b,d,i[3]={0,};
void main(){
    info_check();
}

void info_check(){

....
printf("enter a number :");
scanf("%d", &a);
printf("enter b number :");
scanf("%d", &b);
printf("insert %d value in i[%d]\n",b,i[a]); //debug
i[a] += b;
    if (i[a] > 3) {
        printf("cant be add more than 3");
        menu_start();  // returning to menu
    }
else info_add();
}
4
Contributors
6
Replies
38
Views
7 Months
Discussion Span
Last Post by ChangWon
0

seems like my poor english is not understandable for you guys;
i want to stop add after array[3] as you said james, but this code isnt work at all. if i tryed to type value at first, its works exactly what i want, but when i type again, this array seems doesnt add value that i was typed value first.
and here i ask is why is this array didnt add value properly? did i typed wrong? or what?

Thanks

1

OK, don't worry about your English - we will communicate somehow!

Perhaps you are re-initiasing the array somewhere? Maybe the problem is in info_add? Without all the code it's hard to see.

1

Why you don't use an for loop for add values to the array,after that you can use your if to stop if the user add more than 3 values to the array ?

2

i want to stop add after array[3]

3 is an illegal index for your array, which is size 3, which means that the legal indexes are 0, 1, and 2 given line 4 above. a is the index, you have no check in place to make sure it is 0, 1, or 2, and it appears you are confusing the index a with the array value i[a] in line 16. Note that line 16 could crash the program if a is less than 0 or greater than 2. You also do not declare the variable a in the code that you showed. I assume it's above line 11, which isn't shown, but that's a guess.

As far as "adding", you appear to be adding on line 17, but there's another reference to "adding" on line 22. Not sure what you are trying to achieve, but I changed your program a little to try to (hopefully) make things a little clearer. I either guessed right or wrong as far as the goal. You can either see the code and say "Ah, that's what I meant" or "No, that's not what I meant" and change it and hopefully it will be easier to correct so everything's clear. Note that I didn't change any of the variable NAMES, though you might want to consider making them more descriptive.

#include <stdio.h>
#include <stdlib.h>

void info_add();
void menu_start();
void info_check();
static int b,d,i[3]={0};

int main()
{
    info_check();
    return 0;
}

void info_check()
{
    int a;
    printf("enter an array index (0 - 2) :");
    scanf("%d", &a);
    if(a < 0 || a >= 3)
    {
        printf("Illegal index: %d.  Index must be >= 0 and < 3.  Exiting program.\n", a);
        exit(0);
    }    
    printf("Enter a value to add to i[%d]: ", a);
    scanf("%d", &b);
    i[a] += b;
    printf("adding %d to i[%d]. i[%d] is now %d\n", b, a, a, i[a]); //debug

    if (i[a] > 3) 
    {
        printf("i[%d] can't be add more than 3\n", a);
        menu_start();  // returning to menu
    }
    else
    {
        info_add();
    }        
}

void info_add()
{
}

void menu_start()
{
}
0

Thankyou very much! AssertNull! and Phoxlox!
this is exactly what i looking for!
indeed this code is much clearly and easy!
HAPPY HOLIDAY FOLKS!!!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.