| | |
hey guys could u check my code PLEASE its a code with a "text file"
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2009
Posts: 14
Reputation:
Solved Threads: 0
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:
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:
C++ Syntax (Toggle Plain Text)
#include <iostream.h> #include <stdlib.h> #include <stdio.h> int main(int nNumberofArgs, char*psz[]){ FILE* fp_in; FILE* fp_out; int Average; int sum = 0; int count = 0; int num; fp_in = fopen("input.txt", "r"); fp_out = fopen("output.txt", "w"); //Error Checking if (fp_in = NULL) { printf("File does not exist or could not be found/n"); system("PAUSE"); exit; //return 0; } if (fp_out = NULL) { printf("File does not exist or could not be found/n"); system("PAUSE"); exit; //return } fscanf(fp_in, "%d", &num); while (num != EOF) { // printf("test2"); count++; sum = sum + num; fscanf(fp_in, "%d", & num); } Average = sum/count; fprintf (fp_out, "The sum is equal %d", sum); fprintf (fp_out, "the number of entries is %d", count); fprintf (fp_out, "The average is %d", Average); fclose(fp_in); fclose(fp_out); system("PAUSE"); return 0; }
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.
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?
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Not only the errors mentioned in the post above it also has syntax errors and other major errors too.
1> you are not assigning the pointer you need to check so its Same goes for fp_out
2>Why are you exactly using
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
4>Your starting line itself is hilarious
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.
1>
c++ Syntax (Toggle Plain Text)
if(fp_in = NULL)
c++ Syntax (Toggle Plain Text)
if(fp_in == NULL)
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
c++ Syntax (Toggle Plain Text)
int main(int nNumberofArgs, char*psz[])
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"....
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...
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.
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!
if you had done...
The compiler would have caught it immediatly with an error!
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...
C Syntax (Toggle Plain Text)
typedef unsigned int uint;
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.
C Syntax (Toggle Plain Text)
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!
C Syntax (Toggle Plain Text)
if (NULL == fp_in) { if (NULL == fp_out) {
C Syntax (Toggle Plain Text)
if (NULL = fp_in) {
![]() |
Similar Threads
- Split a text file based on criteria problem (VB.NET)
- Creating a Login Text file (VB.NET)
- Help with creating text file with "" (C#)
- Reading text file ... (C)
- Storing Words from Text File in Array (C++)
- read text file (C)
- read text file (C#)
- Read in a string from a text file (C)
Other Threads in the C++ Forum
- Previous Thread: infix to postfix problem
- Next Thread: substitution cipher help
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets







My suggestion to you is to scrap it and rewrite in true c++ -- a program without all those C style functions calls.