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 June 9th, 2004, 11:51 PM
medo7777 medo7777 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 9 medo7777 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Outputting to a text file...

Hello,

I'm having trouble getting this bit of code to work properly..
What its supposed to do, is get all the data that is entered and output it into a sort of table, in a text file...
my problem tho, is that im getting really wierd outputs and i don't really know what's wrong.
Yes, i'm new to C.. and this is the 1st time i've tried doing anything like this.. so forgive my lack of knowledge

Any help would be greatly appreciated.

Thanks,
Mark


PHP Code:
/*
      file:  student.cpp
      date:  8th June 2004
*/

#include <conio.h>
#include <stdio.h>


int main ()
{
    
char  name[21];
    
int scorecount;
    
float averagetotal;
    
  
FILE *fp;
  
fp fopen("scores.txt","w");
  

  
printf("Enter Name: ");
  
gets(name);

  
fprintf(fp,"Name                   Score     Score     Score     Score     Score            Average\n");
  
fprintf(fp,"                           1         2         3         4         5                   \n");
  
fprintf(fp,"---------------------------------------------------------------------------------------\n");
  
  while(
name[0] != NULL)
    {
      
fprintf(fp,"%s"name);
      
total=0;
      
count=0;
      
printf("Enter Score: ");
      
scanf("%d", &score);
        while(
score != -1)
          {
            
total += score;
            ++
count;
              if(
count<5)
                 {
                  
printf("Enter Score: ");
                  
scanf("%d", &score);
                 }
                else
                  
score = -1;
          }
      
average total count;
      
printf("%.2f\n"average);
      
fflush(stdin);
      
printf("Enter Name: ");
      
gets(name);
    }
    
  
fprintf(fp,"%s            %d        %d        %d        %d        %d        %2.2f\n"namescore);


fclose(fp);
return 
0;


Reply With Quote
  #2  
Old June 10th, 2004, 12:15 AM
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2004
Posts: 1,676 Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 3 Days 8 h 23 m 46 sec
Reputation Power: 132
Sorry, I didn't try to build it -- but the following links jump to mind.

Reply With Quote
  #3  
Old June 10th, 2004, 12:20 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,252 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 5 Days 19 h 26 m 40 sec
Reputation Power: 1985
while(name[0] != NULL)

NULL is a zero pointer.
name[0] is a char .
name is a pointer to a char array, but it does have a non-NULL value.

You appear to be wanting to test for name not being an empty string. This could be done with these possible lines:
while(name[0] != '\0') /* is first char the null-terminator? */
while(strcmp(name,"") /* compare name with an empty string */
while(strlen(name)) /* if length isn't zero, is not empty */

strlen() and strcmp() both require the string.h header file.

Also, are you sure that you're opening the file in text mode. There's a global filemode variable that defines the default. Personally, I always explicitly open my files with "wt" or "wb".

And gets does not protect against buffer overflow, making it a security hole just waiting to happen, or at least a way to crash the program unexpectedly. It is preferable to use fgets with the stdin standard file. However, be aware that fgets does not remove the '\n' from the input string like gets does.

Last edited by dwise1_aol : June 10th, 2004 at 12:26 AM.

Reply With Quote
  #4  
Old June 10th, 2004, 05:40 AM
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jan 2004
Location: near St. Louis Illinois
Posts: 3,288 Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 20 h 37 m 22 sec
Reputation Power: 23
your program produces the wrong file because the fprintf() is in the wrong place.
Code:

	  fprintf(fp,"%s            %d        %d        %d        %d        %d        %2.2f\n", name, score); 

      average = total / count; 
      printf("%.2f\n", average); 
      fflush(stdin); 
      printf("Enter Name: "); 
     fgets(name,sizeof(name),stdin); 
    } 

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Outputting to a text file...

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