C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 26th, 2003, 08:41 PM
kmr6655 kmr6655 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 7 kmr6655 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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]);


}

}

Reply With Quote
  #2  
Old April 27th, 2003, 12:44 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,390 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 22 h 36 m 15 sec
Reputation Power: 4080
>> 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

Reply With Quote
  #3  
Old April 27th, 2003, 07:15 AM
kmr6655 kmr6655 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 7 kmr6655 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #4  
Old April 27th, 2003, 07:28 AM
kmr6655 kmr6655 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 7 kmr6655 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > I/O error, extra text in output

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap