| | |
My logic must be ALL wrong , help on Average.
![]() |
•
•
Join Date: Jun 2005
Posts: 3
Reputation:
Solved Threads: 0
I am doind a lab for my class and the guidlines is suppoes to consist of an input file with student names and 10 quiz grades. The output file is suppose to produce the same format as the input file but one extra column, and that extra column is suppose to represent the average of their quiz grades.
here's my input file named input1.txt:
Dill James 13 18 17 15 19 20 20 16 14 19
King Damon 18 16 11 19 12 12 13 15 18 14
Thomas Dan 13 14 12 15 18 19 19 19 12 20
here's my input file named input1.txt:
Dill James 13 18 17 15 19 20 20 16 14 19
King Damon 18 16 11 19 12 12 13 15 18 14
Thomas Dan 13 14 12 15 18 19 19 19 12 20
C Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <iomanip> #include <string> using namespace std; void calculatescore(); int main () { string studentname; int total; int q1,q2, q3, q4, q5, q6, q7 ,q8 ,q9 ,q10; int average; ifstream infile; ofstream outfile; infile.open("input1.txt"); outfile.open("output.txt"); outfile << fixed << showpoint << setprecision(2); outfile << left << setw(20) << "Student Name" << setw(1) << q1 << setw(1) << q2 << setw(1) << q3 << setw(1) << q4 << setw(1) << q5 << setw(1) << q6 << setw(1) << q7 << setw(1) << q8 << setw(1) << q9 << setw(1) << q10 << setw(1) << average; while (infile >> studentname); { { calculatescore(); average = total / 10; } outfile << left << setw(10) << studentname << setw(1) << q1 << setw(1) << q2 << setw(1) << q3 << setw(1) << q4 << setw(1) << q5 << setw(1) << q6 << setw(1) << q7 << setw(1) << q8 << setw(1) << q9 << setw(1) << q10 << setw(1) << average; }// end while infile >> studentname>> q1>> q2>> q3>> q4>> q5 >> q6 >> q7 >> q8 >> q9 << q10; infile.close(); outfile.close(); return 0; } void calculatescore() { int total; int num = q1, q2, q3, q4, q5, q6, q7, q8 ,q9, q10; string studentname; int counter; ifstream infile; infile >> studentname >> q1 >> q2 >> q3 >> q4 >> q5 >> q6 >> q7 >> q8 >> q9 >> q10; counter = 0; while (counter < 10) { total = 0; infile >> num; while (num > 0); { total = total + num; counter++; infile >> num; } } }
•
•
Join Date: Jun 2005
Posts: 4
Reputation:
Solved Threads: 0
Are necessary using the functions on the <iostream.h> header? I prefer the functions in <stdio.h> for files management. With these functions I present this code that realize your lab job.
All lines in input1.txt file must ended with a carriage return for this code work fine. (sorry for my little english
)
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #define MAXSTR 80 int main () { FILE *fpin, *fpout; int q1, q2, q3, q4, q5, q6, q7 ,q8 ,q9 ,q10; float average; char str[MAXSTR + 1], firstname[MAXSTR], secondname[MAXSTR]; if ((fpin = fopen ("input1.txt", "r")) == NULL) { fprintf (stderr, "Error reading input1.txt"); exit (EXIT_FAILURE); } if ((fpout = fopen ("output.txt", "w")) == NULL) { fprintf (stderr, "Error writing output.txt"); exit (EXIT_FAILURE); } while (1) { fgets (str, MAXSTR, fpin); if (feof (fpin)) break; sscanf (str, "%s %s %d %d %d %d %d %d %d %d %d %d", firstname, secondname, &q1, &q2, &q3, &q4, &q5, &q6, &q7, &q8, &q9, &q10); average = (q1 + q2 + q3 + q4 + q5 + q6 + q7 + q8 + q9 + q10)/10.0; fprintf (fpout, "%s %s %d %d %d %d %d %d %d %d %d %d %.2f\n", firstname, secondname, q1, q2, q3, q4, q5, q6, q7, q8, q9, q10, average); } fclose (fpin); fclose (fpout); return EXIT_SUCCESS; }
) •
•
Join Date: Jun 2005
Posts: 4
Reputation:
Solved Threads: 0
Kreitcher, think You that's always is necessary to program in C++? why is this more elegant? I think that depend of the problem to solve, but this is my point and I respect other view point. In this particular case, I prefer functions in the header <stdio.h> to work with files because are simpler to me than functions in iostream.h Respect to manage arrays, I do not use them because I think that the proffesor no seen yet
> why is this more elegant?
C-style I/O doesn't recognize the std::string class, so you end up having to jump through error prone hoops to get it to work, or you need to use C-style strings, which are inherently error prone. That alone is reason to use iostreams. If you don't understand how to use iostreams, or you like C-style I/O better, that's okay. But please don't try to convince other people to take a big step backward in safe programming practice when they're still learning the basics.
@simplex85:
You have issues with your program. First, you try to print uninitialized variables. That results in undefined behavior, and you really want to avoid undefined behavior. Since printing out the scores when you haven't read any is nonsensical, you can remove that part of the program.
> while (infile >> studentname);
Notice the trailing semicolon. That means that the loop will read names until there's an input error, or the end of the file is reached. In other words, it's not doing what you think it is. Remove the semicolon.
There's no need for braces here.
I don't know why you thought that calculatescore does anything meaningful. You would be better off avoiding functions until you know how to make them communicate with main.
> int q1,q2, q3, q4, q5, q6, q7 ,q8 ,q9 ,q10;
When you number variables like this, it's a sure sign that an array would be better.
Here's your program with the requisite fixes (off the top of my head, sorry for any errors):
Yea, it's ugly. But impromptu code tends to be ugly, and it should be more than enough to help you along.
C-style I/O doesn't recognize the std::string class, so you end up having to jump through error prone hoops to get it to work, or you need to use C-style strings, which are inherently error prone. That alone is reason to use iostreams. If you don't understand how to use iostreams, or you like C-style I/O better, that's okay. But please don't try to convince other people to take a big step backward in safe programming practice when they're still learning the basics.
@simplex85:
You have issues with your program. First, you try to print uninitialized variables. That results in undefined behavior, and you really want to avoid undefined behavior. Since printing out the scores when you haven't read any is nonsensical, you can remove that part of the program.
> while (infile >> studentname);
Notice the trailing semicolon. That means that the loop will read names until there's an input error, or the end of the file is reached. In other words, it's not doing what you think it is. Remove the semicolon.
C Syntax (Toggle Plain Text)
{ calculatescore(); average = total / 10; }
I don't know why you thought that calculatescore does anything meaningful. You would be better off avoiding functions until you know how to make them communicate with main.
> int q1,q2, q3, q4, q5, q6, q7 ,q8 ,q9 ,q10;
When you number variables like this, it's a sure sign that an array would be better.
Here's your program with the requisite fixes (off the top of my head, sorry for any errors):
C Syntax (Toggle Plain Text)
#include <cstdlib> #include <fstream> #include <iostream> #include <string> int average(int scores[10]); int main() { std::string name, temp; int scores[10]; std::ifstream in("infile"); std::ofstream out("outfile"); if (!in || !out) { std::cerr << "File open failure\n"; std::exit(EXIT_FAILURE); } while (in >> name) { in >> temp; name += ' ' + temp; out << name << ' '; for (int i = 0; i < 10; i++) { in >> scores[i]; out << scores[i] << ' '; } out << average(scores) << '\n'; } } int average(int scores[10]) { int sum = 0; for (int i = 0; i < 10; i++) sum += scores[i]; return sum / 10; }
•
•
Join Date: Jun 2005
Posts: 4
Reputation:
Solved Threads: 0
Dogtree, I find that it is necessary to leave that the programmer is an intelligent being and that he will make the best desición in case you show him alternative of as making the things. I am not saying that simplex85 changes C++ for C, I am simply showing him another way to make the things in this particular case. For example, do you agree on hiding the programmer the "infamous" instruction goto? or do you prefer to tell him the advantages and disadvantages of such an instruction? There is a difference between to impose points of view and to let people to make her own decisions. The programming is one of the fields where can arrive to the same thing for very different roads
That's all well and good, but you didn't answer the original question. You just threw away the code given and posted a C-style solution with the implication that it was somehow better, without explaining why, when there are very good arguments for avoiding such a solution until one knows what is gained and what is lost with the decision.
•
•
Join Date: Jun 2005
Posts: 3
Reputation:
Solved Threads: 0
Well i'm taking C++ as a summer class and stuff is moving by real quick and i'm tryin my best to stay on course and some of this stuff you guys talk about, i am unfamiliar with. And yes this is a C++ program, i really like to thank you guys for the help and input.
Like i am not familiar with "std::" or what an array is yet, atleast for this lab, my professor does have certain guidelines. I'll try to work with the input you guys gave me.
But i am having the most trouble just tryin to figure out on how to read the input file and make changes to them properly.
Thanks
Like i am not familiar with "std::" or what an array is yet, atleast for this lab, my professor does have certain guidelines. I'll try to work with the input you guys gave me.
But i am having the most trouble just tryin to figure out on how to read the input file and make changes to them properly.
Thanks
•
•
Join Date: Jun 2005
Posts: 3
Reputation:
Solved Threads: 0
I am getting an error about undeclared variables for q1 in my void calculatescore() function.
I guess what i just realize my problem is how do I assign the numbers in my input file properly in my code to be able to calculate them? So what exactl is the logic behind it? Cuz this book seems to assume that i should know more.
here's my input1.txt file:
Dill James 13 18 17 15 19 20 20 16 14 19
King Damon 18 16 11 19 12 12 13 15 18 14
Thomas Dan 13 14 12 15 18 19 19 19 12 20
I guess what i just realize my problem is how do I assign the numbers in my input file properly in my code to be able to calculate them? So what exactl is the logic behind it? Cuz this book seems to assume that i should know more.
here's my input1.txt file:
Dill James 13 18 17 15 19 20 20 16 14 19
King Damon 18 16 11 19 12 12 13 15 18 14
Thomas Dan 13 14 12 15 18 19 19 19 12 20
C Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <iomanip> #include <string> using namespace std; void calculatescore(); int main () { string studentname; int total; int q1,q2, q3, q4, q5, q6, q7 ,q8 ,q9 ,q10; int average; ifstream infile; ofstream outfile; infile.open("input1.txt"); outfile.open("output.txt"); void calculatescore(); outfile << fixed << showpoint << setprecision(2); outfile << left << setw(20) << "Student Name" << setw(1) << q1 << setw(1) << q2 << setw(1) << q3 << setw(1) << q4 << setw(1) << q5 << setw(1) << q6 << setw(1) << q7 << setw(1) << q8 << setw(1) << q9 << setw(1) << q10 << setw(1) << average; while (infile >> studentname) { calculatescore(); average = total / 10; outfile << left << setw(10) << studentname << setw(1) << q1 << setw(1) << q2 << setw(1) << q3 << setw(1) << q4 << setw(1) << q5 << setw(1) << q6 << setw(1) << q7 << setw(1) << q8 << setw(1) << q9 << setw(1) << q10 << setw(1) << average; }// end while infile >> studentname>> q1>> q2>> q3>> q4>> q5 >> q6 >> q7 >> q8 >> q9 >> q10; infile.close(); outfile.close(); return 0; } void calculatescore() { int total; //****this is where it says i have an error int num = q1, q2, q3, q4, q5, q6, q7, q8 ,q9, q10; string studentname; int counter; ifstream infile; infile >> studentname >> q1 >> q2 >> q3 >> q4 >> q5 >> q6 >> q7 >> q8 >> q9 >> q10; counter = 0; while (counter < 10) { total = 0; infile >> num; while (num > 0); { total = total + num; counter++; infile >> num; } } }
![]() |
Similar Threads
- Collins: Why this scientist believes in God (Geeks' Lounge)
- need a little help with standard deviation (C)
- Virtual reailty (Geeks' Lounge)
- Problem with update data to database (JSP)
- SQLCommandBuilder is Dumb? (C#)
- syntax error (C)
- my security point....... (IT Professionals' Lounge)
Other Threads in the C Forum
- Previous Thread: .exe crashing on executing output !!
- Next Thread: Problem with Win32 Accelerators
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o ide inches incrementoperators intmain() iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions systemcall test unix urboc user variable voidmain() wab win32api windows.h





