0

このプログラムで、対応する文字の等級に対する等級を計算し、ユーザーが出力したい回数だけループするようにしました。ただし、何らかの理由で、最後の既知の出力のみをテキスト ファイルに書き込みます。ここで私が間違っていることを誰かに教えてもらえますか?

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

2 に答える 2

0

あなたが多くのものを出力しようとしているなら、私はあなたをお勧めします

ループを反復処理して、目的の出力を変数に追加します。ループが終了したら、その変数を出力します。

var output

 while(true) {
     add to output
 }

 print output
于 2013-08-02T16:51:02.403 に答える