hey guys could u check my code PLEASE its a code with a "text file"

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2009
Posts: 14
Reputation: jessejamesjjr is an unknown quantity at this point 
Solved Threads: 0
jessejamesjjr jessejamesjjr is offline Offline
Newbie Poster

hey guys could u check my code PLEASE its a code with a "text file"

 
0
  #1
Jul 5th, 2009
hey guys, i would greatly appreciate it if u wood help me out to see whats wrong with my codes.... heres my question and heres my code......

DONT 4GET TO MAKE THE TEXT FILE TO RUN IT !!!! <input.txt, output.txt> <fp_in, fp_out>

1. Write a program to average real numbers input from a text file called “input.txt”. Each line of the text file will contain a single number to be averaged. Collect and sum numbers from the file until there are no more numbers in the file, then compute the average. Write the sum, the number of entries in the file and the average to another text file called “output.txt”.

Create the following text file and use as your input file:
10
15
20
45
6
19
33
21


here are my codes:
  1. #include <iostream.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5. int main(int nNumberofArgs, char*psz[]){
  6.  
  7. FILE* fp_in;
  8. FILE* fp_out;
  9.  
  10. int Average;
  11. int sum = 0;
  12. int count = 0;
  13. int num;
  14.  
  15. fp_in = fopen("input.txt", "r");
  16. fp_out = fopen("output.txt", "w");
  17.  
  18. //Error Checking
  19.  
  20. if (fp_in = NULL)
  21. {
  22. printf("File does not exist or could not be found/n");
  23. system("PAUSE");
  24. exit;
  25. //return 0;
  26. }
  27.  
  28. if (fp_out = NULL)
  29. {
  30. printf("File does not exist or could not be found/n");
  31. system("PAUSE");
  32. exit;
  33. //return
  34.  
  35. }
  36. fscanf(fp_in, "%d", &num);
  37.  
  38.  
  39. while (num != EOF)
  40. {
  41. // printf("test2");
  42. count++;
  43. sum = sum + num;
  44. fscanf(fp_in, "%d", & num);
  45. }
  46.  
  47. Average = sum/count;
  48.  
  49. fprintf (fp_out, "The sum is equal %d", sum);
  50. fprintf (fp_out, "the number of entries is %d", count);
  51. fprintf (fp_out, "The average is %d", Average);
  52.  
  53. fclose(fp_in);
  54. fclose(fp_out);
  55.  
  56. system("PAUSE");
  57. return 0;
  58. }
Last edited by Ancient Dragon; Jul 5th, 2009 at 2:09 am. Reason: add code tags -- 9 posts and you still have not figured out how to use code tags.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,378
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: hey guys could u check my code PLEASE its a code with a "text file"

 
0
  #2
Jul 5th, 2009
You call that c++ code ??? My suggestion to you is to scrap it and rewrite in true c++ -- a program without all those C style functions calls.
Last edited by Ancient Dragon; Jul 5th, 2009 at 2:13 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,675
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: hey guys could u check my code PLEASE its a code with a "text file"

 
0
  #3
Jul 5th, 2009
9 posts and you can't find the correct forum? Your code is C, not C++.

That said, your logic is generally correct. I would not expect you to put the text in the output file, just the values.

In computing the average, remember that integer division gives an integer result - is that really what you (or your instructor) want?

Do you get any error or incorrect result when you run this? What is your real question?
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training

Re: hey guys could u check my code PLEASE its a code with a "text file"

 
0
  #4
Jul 5th, 2009
Not only the errors mentioned in the post above it also has syntax errors and other major errors too.

1>
  1. if(fp_in = NULL)
you are not assigning the pointer you need to check so its
  1. if(fp_in == NULL)
Same goes for fp_out

2>Why are you exactly using exit .When you are using it at least call the function properly with a return value.

3>This is a C++ forum and this post is first of all not C++ its C. The only C++ thing I see here is the header <iostream> wonder why you even cared to use it.???

4>Your starting line itself is hilarious
  1. int main(int nNumberofArgs, char*psz[])
Where are you even using the command line parameters ???

The main thing wrong with your code is "You don't know which language you are writing it in...!!!".So try to put in some more hard work.you definitely can improve.
Last edited by csurfer; Jul 5th, 2009 at 2:42 am.
I Surf in "C"....
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: hey guys could u check my code PLEASE its a code with a "text file"

 
0
  #5
Jul 5th, 2009
Ditto as to what CSurfer said.

Also you need an exist but empty file handler! Meaning file exists but empty, thus no data read thus BOOM! Divide by zero!
You need a zero case handler!

I'd also recommend to write clean code! Something teachers don't teach from the old C books. They teach signed data. In reality they should be teaching ALL DATA IS UNSIGNED, unless it is necessary for it to be signed! So your declarations shouldn't be integer, they should be unsigned int. And if that's a mouthful then...

  1. typedef unsigned int uint;
uint nCount = 0;

See there is no negative count of something counted! And if you're accumulating positive numbers then why use signed arguments?

"%u" vs "%d" etc.

And convert to floating point for a floating point-average.

OR
add 1/2 a count for a more approximate average.
  1. nTotal = (nTally + (nCount>>1)) / nCount;

There is one good thing in Java that should be adopted fore every C/C++ program written. If you had done the following, you would have immediately caught your comparison vs assignment error! ALWAYS put the constant first in your comparisons!
  1. if (NULL == fp_in)
  2. {
  3.  
  4. if (NULL == fp_out)
  5. {
if you had done...
  1. if (NULL = fp_in)
  2. {
The compiler would have caught it immediatly with an error!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC