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

#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;
             }
             
         }        
      
     }

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.

#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;
}

All lines in input1.txt file must ended with a carriage return for this code work fine. (sorry for my little english ;) )

I think it would be more elegant if you use functions to acheive certain key points in your program. Hey crazypitukillo, u have done the whole thing using C. I think simple x85 wants the whole thing in C++. Am I right?
-kreitcher

hey simplex85, why dont u use an array instead of declaring 10 variables in a row? You will also be able to loop efficiently and hence make ur program look better.
-kreitcher

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.

{
  calculatescore();
  average = total / 10;
}

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):

#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;
}

Yea, it's ugly. But impromptu code tends to be ugly, and it should be more than enough to help you along. :)

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.

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

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. :sad:


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

#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;
             }
             
         }        
      
     }

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;

Ok what u see above simplex is part of the code you have written.
Point #1 is that you have not passed on the variables q1 q2 etc to the function. How will the function know these variables exist. Unless you pass variables (or declare new ones ) u cant use them in a function. about the code bit I have pated, well i dont think you can assign values in that way. Syntax is wrong.
-kreitcher

And oh let me clarify. About the ELEGANT bit that is. All I was saying is that the program is easier to handle when u use FUNCTIONS. I have not said anyhting about C or C++. I personally agree with Dogtree. Why bring in C when work is going on in C++. No need to go down the ladder.
-kreitcher

Um well .. simplex, small bit of advice to you. Please dont start reading C++ from Bjarn Soustroup's book. He is a mega genius and wille xpect that sort of behaviour from you! I really dont know any good authors. Cant help you there sorry.
-kreitcher

This article has been dead for over six months. Start a new discussion instead.