#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    7
    Rep Power
    0

    Question I/O error, extra text in output


    Hello everyone,

    Below is a small program that ends when -999 is entered, then a summary is printed. However, my program is printing the -999 in the summary and then prints out random characters.

    Can anyone tell me how to stop the -999 from being output in the summary, as well as the other characters? I thought that the carot added to the scanf would prevent the -999 from printing. Or is that just text?

    As always, your help is appreciated!

    #include <stdio.h>
    void main (void)



    {

    int x,account[5];
    float balance[5];
    char name[5][30];


    printf("Enter client account number, name and balance.\n");

    printf("Enter -999 to end input.\n\n");


    for (x=0; x <5 ; x++)


    {

    printf("? ");


    scanf("%i[^-999]",&account[x]);

    if(account[x] == -999)

    {
    fflush(stdin);
    break;

    }


    scanf("%30s",name[x]);


    scanf("%f",&balance[x]);



    }



    printf("\n\nACCOUNT \tLASTNAME \tBALANCE\n");



    for (x=0; x<5; x++)

    {

    printf("%i %20s %14.2f \n", account[x], name[x], balance[x]);


    }

    }
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    >> my program is printing the -999 in the summary and then prints out random characters.
    The reason I suspect you're getting random stuff is because you haven't explicitly initialized the arrays account, balance or name.

    >> I thought that the carot added to the scanf would prevent the -999 from printing
    scanf() is used for accepting user input, not printing. Your ^-999 filter doesn't work the way you think, so you might as well remove it. With that said, I would handle it by putting code to break out of the printing loop, if -999 is encountered.

    After implementing those two suggestions, your code should look something like this :
    Code:
    #include <stdio.h>
    #include <string.h>
    int main (void)
    {
        int x,account[5];
        float balance[5];
        char name[5][30];
    
        /* First initialize your arrays */
        for (x = 0; x < 5; x++) {
            account[x] = 0;
            balance[x] = 0;
            strcpy(name[x], "");
        }
    
        printf("Enter client account number, name and balance.\n");
        printf("Enter -999 to end input.\n\n");
    
        for (x=0; x <5 ; x++)
        {
            printf("? ");
            scanf("%d",&account[x]);
            if(account[x] == -999)
            {
                fflush(stdin);
                break;
            }
            scanf("%.30s",name[x]);
            scanf("%f",&balance[x]);
        }
    
        printf("\n\nACCOUNT \tLASTNAME \tBALANCE\n");
        for (x=0; x<5; x++)
        {
            if (account[x] == -999)
                break;
            printf("%i %.30s %14.2f \n", account[x], name[x], balance[x]);
        }
        return 0;
    }
    Please note that I typed the above code off the top of my head, so there may be some minor syntax errors. HTH :)
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    7
    Rep Power
    0
    Thanks so much! :D

    I'm wondering why we are initializing within a for loop?

    Couldn't we just initialize without the loop? :confused:

    Kathy
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    7
    Rep Power
    0
    It works great now. Moving the if statement in the third loop to above the printf statement got rid of the -999, and the extra garbage that came with it.

    One of these days I'm going to get this stuff! :o

    Kathy

IMN logo majestic logo threadwatch logo seochat tools logo