|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
>> 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 ![]() |
|
#3
|
|||
|
|||
|
Thanks so much!
I'm wondering why we are initializing within a for loop? Couldn't we just initialize without the loop? Kathy |
|
#4
|
|||
|
|||
|
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! Kathy |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > I/O error, extra text in output |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|