-3

I am working on a program that simulates a bank. When I go to the first case 'A', instead of just printing OK or ERROR I am getting this weird output and I can't figure out why. Can someone please explain that?

#include <stdio.h>
#include <stdlib.h>
typedef struct{
    int ballance;
    int status;
    char name[10];
}client;
int main()
{
    client cli[10];
    int n, number,i;
    char c;
    int money;
    for(i=0;i<10;i++){
        cli[i].status=0;
    }
    scanf("%d",&n);
    fflush(stdin);
    scanf("%c%d",&c,&number);
    do{
    switch(c){
        case 'A':{
            if(cli[number-1].status==0){
            scanf("%s",&cli[number-1].name);
            cli[number-1].status=1;
            printf("OK\n");
        }else{
            printf("ERROR\n");
        }
        break;
    }
        case 'B':{
            if(cli[number-1].status!=0){

            cli[number-1].status=0;
            printf("OK\n");
        }else{
            printf("ERROR\n");
        }
        break;
    }
        case 'C':{
            if(cli[number-1].status!=0){

            printf("%s %d",cli[number-1].name, cli[number-1].ballance);

        }else{
            printf("ERROR\n");
        }
        break;
    }
    case 'D':{
            if(cli[number-1].status!=0){
            scanf("%d",&money);
            cli[number-1].ballance=cli[number-1].ballance+money;
            printf("%s %d",cli[number-1].name, cli[number-1].ballance);

        }else{
            printf("ERROR\n");
        }
        break;
    }
    case 'E':{
            scanf("%d",&money);
            if(cli[number-1].status!=0&&money<=cli[number-1].ballance){
            scanf("%d",&money);
            cli[number-1].ballance=cli[number-1].ballance+money;
            printf("%s %d",cli[number-1].name, cli[number-1].ballance);

        }else{
            printf("ERROR\n");
        }
        break;
    }
        }
        n--;
    }while(n>=0);
    return 0;
}
Thanatos
  • 42,585
  • 14
  • 91
  • 146
blidt
  • 119
  • 9
  • 2
    What output are you getting exactly? What input are you giving? Also, why are you trying to [flush an input stream](http://stackoverflow.com/a/2979217/721269)? That has no defined meaning. – David Schwartz Mar 16 '14 at 03:48
  • Can you please narrow the problem down to just a few lines of code, and also add some output? This code is very confusing. – motoku Mar 16 '14 at 03:57
  • 1
    You need to spend more time debugging your code before asking for help. Insert diagnostic `printf`s and/or attach a debugger to trace your program's execution. – Jonathon Reinhart Mar 16 '14 at 03:58
  • I suggest you adopt a formatting style that's easier to read. Try 'Allman style'. – Liam M Mar 16 '14 at 04:01
  • @DavidSchwartz Well, i enter the number of operations i am going to make, lets say 1, then 'A', to open a new account, followed by the client number (number) and the initial deposit amount (money). What i am getting is "OK\n" and "ERROR\n" when it was supossed to print either one message or the other, but not both. – blidt Mar 16 '14 at 04:03
  • @ÁngelCáceresLicona, please take a look at the updated answer. – feihu Mar 16 '14 at 04:17
  • @ÁngelCáceresLicona, one more advice, you should give more information when you ask the question, e.g., what input? – feihu Mar 16 '14 at 04:24
  • @feihu thanks a lot, and i will follow your advice. – blidt Mar 16 '14 at 04:27

1 Answers1

1

I tried your program, when n = 1, the program would output OK and ERROR. The statement in while should be changed to:

}while(n>0);

And if the input n is above than 1, you should move the scanf statement into while loop:

scanf("%d",&n);
do{
fflush(stdin);
scanf("%c%d",&c,&number);

Hope that help.

One more thing, debugger is always your best friend, please spend more time to debug, it would give you the best answer most of the time.

feihu
  • 1,935
  • 16
  • 24
  • well, i tried that and it works for the n=1 case only. I want the switch statement to the executed n times. – blidt Mar 16 '14 at 04:17
  • a link to a good debugger tutorial would be highly apreciated, i have tried to learn debugging code (on the codeblocks website) and idon't quite get it. – blidt Mar 16 '14 at 04:29
  • @ÁngelCáceresLicona, google `debug tutorial`, you would get a lot of useful information. – feihu Mar 16 '14 at 06:19