The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
I/O error, extra text in output
Discuss I/O error, extra text in output in the C Programming forum on Dev Shed. I/O error, extra text in output C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

April 26th, 2003, 08:41 PM
|
|
Junior Member
|
|
Join Date: Mar 2003
Posts: 7
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
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]);
}
}
|

April 27th, 2003, 12:44 AM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
>> 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 
|

April 27th, 2003, 07:15 AM
|
|
Junior Member
|
|
Join Date: Mar 2003
Posts: 7
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Thanks so much!
I'm wondering why we are initializing within a for loop?
Couldn't we just initialize without the loop?
Kathy
|

April 27th, 2003, 07:28 AM
|
|
Junior Member
|
|
Join Date: Mar 2003
Posts: 7
Time spent in forums: < 1 sec
Reputation 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!
Kathy
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|