hello
hello

Reputation: 5

How do I write multiple output from a loop?

I got this program to calculate the grades to its corresponding letter grade, and I got it to loop as many times as the user wants to output. However, for some reason it only writes the last known output to its text file. Can anyone tell me what I am doing wrong here?

#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>

int weighted1 = 0;
int weighted2 = 0;
int weighted3 = 0;
int weighted4 = 0;
int weighted_average = 0;

const int MAX = 20;

int flag = 0;
int choice;
double sum = 0;
double average = 0;
char name [10];
char letter;
char file [MAX];

int num = 0;
int count = 0;

int main( )
{

//Initiate input/output stream
std::ifstream in_stream;
std::ofstream out_stream;

in_stream.open("grade.txt");
out_stream.open("finalgrade.dat");

double first, second, third, fourth;
in_stream >> first >> second >> third >> fourth >> name;
//std::cout >> " 1: " >> first >> " 2: " >> second >> 
double grade = 0.0;
grade = (first + second + third + fourth)/4;

//Gives user the choice of reading student records from keyboard or file
bool menu = true;
while (menu != false)
{
      std::cout << "Would you like to open as keyboard or file?" << '\n';
std::cout << "1. keyboard" << '\n';
std::cout << "2. file" << '\n';

std::cin >> choice;

switch (choice)
{
//Enter the number students the grades will enter
case 1:
std::cout << "How many students? ";
std::cin >> num;
for(count =0; count < num; count++) 
{
    {
    std::cout << "Student's Name: ";
    std::cin >> name;
    }
do
{
flag = 0;
std::cout << "Please input your first exam grade and press enter: \n";
    std::cin >> first;
    if ((first < 0) || (first > 100))

    {
        std::cout << "You've entered invalid data!" << '\n';
        flag = 1;
    }
}while (flag == 1);

do
{
flag = 0;
std::cout << "Please input your second exam grade and press enter: \n";
    std::cin >> second;
    if ((second < 0) || (second > 100))

    {
        std::cout << "You've entered invalid data!" << '\n';
        flag = 1;
    }
}while (flag == 1);

do
{
flag = 0;
std::cout << "Please input your third exam grade and press enter: \n";
    std::cin >> third;
    if ((third < 0) || (third > 100))

    {
        std::cout << "You've entered invalid data!" << '\n';
        flag = 1;
    }
}while (flag == 1);

do
{
flag = 0;
std::cout << "Please input your final exam grade and press enter: \n";
    std::cin >> fourth;
    if ((fourth < 0) || (fourth > 100))

    {
        std::cout << "You've entered invalid data!" << '\n';
        flag = 1;
    }

}while (flag == 1);

//Formulas that calculate student average
grade = (first + second + third + fourth)/4;
sum = first + second + third + fourth;
average = sum/4;

//Letter grade and it's weighted averages
 letter = 'A';
 letter = 'B';
 letter = 'C';
 letter = 'D';
 letter = 'F';

 if(grade >= 90)
 { 
 letter = ('A');
 std::cout<<letter<<'\n';
 }
 else if(grade >= 80)
 {
 letter = ('B');
 std::cout<<letter<<'\n';
 }
 else if(grade >= 70)
 {
 letter = ('C');
 std::cout<<letter<<'\n';
 }
 else if(grade >= 60)
 {
 letter = ('D');
 std::cout<<letter<<'\n';
 }
 else if (grade < 60)
 {
 letter = ('F');
 std::cout<<letter<<'\n';
 }
 weighted1 = (first  * .20);
 weighted2 = (second * .20);
 weighted3 = (third  * .20);
 weighted4 = (fourth * .40);
 weighted_average = (weighted1 + weighted2 + weighted3 + weighted4);

 //Output
 std::cout << "Exam Grades: " <<  first << "," << second << "," << third << "," << fourth << '\n';
 std::cout << "This is the average for " << name << ": " << weighted_average << '\n';
 std::cout << "This is the letter grade: " << letter << '\n';
  }
  {
//Writing the grade into grades.txt
  for(count =0; count < num; count++)   
  {
    std::ofstream myfile;
    myfile.open ("grades.txt");
    myfile << "Writing this to a file: ";
    myfile << name << ' ';
    myfile << weighted_average << ' ';
    myfile << letter << '\n';
    myfile << "****";
    myfile.close();

  }
  break;
  }

//Here we open "grade.txt" to output grade to screen
case 2:

in_stream.open("grade.txt");
out_stream.open("finalgrade.dat");

 letter = 'A';
 letter = 'B';
 letter = 'C';
 letter = 'D';
 letter = 'F';

 if(grade >= 90)
 letter = ('A');

 else if(grade >= 80)
 letter = ('B');

 else if(grade >= 70)
 letter = ('C');

 else if(grade >= 60)
 letter = ('D');

 else if (grade < 60)
 letter = ('F');

 weighted1 = (first  * .20);
 weighted2 = (second * .20);
 weighted3 = (third  * .20);
 weighted4 = (fourth * .40);
 weighted_average = (weighted1 + weighted2 + weighted3 + weighted4);

std::cout << "Enter file name: ";
     std::cin >> file;
if(file != "grade.txt")
{
     std::cout << std::fixed << "The average grade for: " << name << '\n';
     std::cout << "average in grade.txt is: "<< weighted_average << std::setprecision(2) << '\n';
     std::cout << "and the letter grade is: " << letter << '\n'; 
}
else
{
    return 0;
}

in_stream.close();
out_stream.close();
}

return 0;
}
}

Upvotes: 0

Views: 2930

Answers (2)

Michelle
Michelle

Reputation: 2900

EDIT: The more serious issue here is that you're only storing the last input. You should create an object to store all of the data for each student (e.g. a Student object), create an array of students, and loop through that array to print the information after you have all of your input. I've updated the code below to what it would look like for an array of objects.

If you don't know any object-oriented programming concepts, you could also put each piece of data (name, letter grade, average, etc.) in an array, where the 0th element in each would all represent one student, the 1st would represent another, etc. This isn't good practice; it's a much better idea to create an object to store the information about a student.

Original: You're overwriting your file instead of appending to it by opening and closing it inside every iteration of the loop.

Instead, open your file before the loop and close it after, like this:

{
  //Writing the grade into grades.txt
  std::ofstream myfile;
  myfile.open ("grades.txt");
  for(count =0; count < num; count++)   
  {
    myfile << "Writing this to a file: ";
    myfile << students[count].name << ' ';
    myfile << students[count].weighted_average << ' ';
    myfile << students[count].letter << '\n';
    myfile << "****";
  }
  myfile.close();
}

Upvotes: 1

Moises Hidalgo
Moises Hidalgo

Reputation: 133

if your trying to output many things i suggest you

Iterate through the loop adding your intended output to a variable, after the loop finishes output that variable Example

var output

 while(true) {
     add to output
 }

 print output

Upvotes: 0

Related Questions