c++输出错误
C++ Output Errors
由于你们的缘故,这个程序的输出是固定的。除了studentNumber。我看到评论说我从来没有给它设置值,这让我很困惑。
void process_file(ifstream& input)
{
int thisStudent = 0;
StudentRecord student = StudentRecord();
while (thisStudent++ < CLASS_SIZE)
{
student.input(input, thisStudent);
student.computeGrade();
student.output();
}
}
不会将studentNumber设置为0,然后在每次执行循环时加上+1。
// Author:
// Assignment 8
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
ofstream outputfile("output.txt");
const int MAX_FILE_NAME = 35;
const int CLASS_SIZE = 5;
class StudentRecord
{
public:
void input( ifstream& input,int studentid);
void computeGrade();
void output();
private:
int studentNumber;
double exam1;
double exam2;
double exam3;
double exam4;
double average;
char grade;
};
void open_input(ifstream& input, char name[]);
void process_file(ifstream& input);
int main()
{ char again;
char file_name[MAX_FILE_NAME + 1];
ifstream input_numbers;
cout << "This program can calculate the exam average and grade forn"
<< "each student.n" << endl;
system("pause");
do
{
system("cls");
open_input(input_numbers, file_name);
process_file(input_numbers);
input_numbers.close();
cout << "nDo you want to process another file (Y/N)? ";
cin >> again;
cin.ignore(256, 'n');
} while ( again == 'y' || again == 'Y');
cout << "nEnd of Program!" << endl;
outputfile << "nnThanks for using GradeCalc!f";
outputfile.close();
return 0;
}
void process_file(ifstream& input)
{
int thisStudent = 0;
StudentRecord student = StudentRecord();
while (thisStudent++ < CLASS_SIZE)
{
student.input(input, thisStudent);
student.computeGrade();
student.output();
}
}
void open_input(ifstream& input, char name[])
{ int count = 0;
do
{ count++;
if (count != 1)
{ cout << "naInvalid file name or file does not exist. Please try again."
<< endl;
}
cout << "nEnter the input file name (maximum of " << MAX_FILE_NAME
<< " characters please)n:> ";
cin.get(name, MAX_FILE_NAME + 1);
cin.ignore(256, 'n');
input.clear();
input.open(name,ios_base::in);
} while (input.fail() );
}
void StudentRecord::input(ifstream& input, int studentid)
{
input >> exam1 >> exam2 >> exam3 >> exam4;
}
void StudentRecord::computeGrade()
{
average = (exam1 + exam2 + exam3 + exam4) / 4 ;
if (average >= 90)
grade = 'A';
else if (average >= 80)
grade = 'B';
else if (average >= 70)
grade = 'C';
else if (average >= 60)
grade = 'D';
else if (average < 60)
grade = 'F';
}
void StudentRecord::output()
{
cout << "nnThe record for student number:" << setw(8) << studentNumber << endl;
cout << "The exam grades are:" << setw(8) << exam1 << exam2 << exam3 << exam4 << endl;
cout << "The numeric average is:" << setw(8) << average << endl;
cout << "and the letter grade assigned is:" << setw(8) << grade << endl;
}
好吧,studentNumber是垃圾,因为你从来没有给它一个值。所以它只是在内存中已经存在的那个位置。
考试成绩打印错误,因为c++中的逗号不像你想象的那样,这也是为什么添加一个endl;
给你一个错误。
格式我将让你自己计算。您应该考虑仔细阅读输出,或者至少做一些试验和错误。
其中一个错误是:
cout << "The exam grades are:" << setw(8) << exam1, exam2, exam3, exam4;
我想你是这个意思:
cout << "The exam grades are:" << setw(8) << exam1 << exam2 << exam3 << exam4 << endl;
CLASS_SIZE
被定义为5
,所以这个循环:
while (thisStudent++ < CLASS_SIZE)
将迭代6次。
也cout << "The exam grades are:" << setw(8) << exam1, exam2, exam3, exam4;
这将输出exam1
,然后对其余变量求值且不做任何操作。
70 80 90 95 95 85 90 80 75 85 70 80 55 85 50 70 45 50 40 35
有空格吗?如果是,请忽略它们。input >> exam1 >> exam2 >> exam3 >> exam4;
将把空间加载到一个考试变量中。
—edit for MooingDuck—
#include <iostream>
#include <sstream>
using namespace std;
int main() {
cout << "main() ENTRY" << endl;
stringstream s1(ios_base::in | ios_base::out),
s2(ios_base::in | ios_base::out);
int i = -1;
s1 << "111 222";
s1 >> i; cout << i << endl;
s1 >> i; cout << i << endl;
s2 << "111 222";
s2 >> noskipws;
s2 >> i; cout << i << endl;
s2 >> i; cout << i << endl;
return 0;
}
输出:main() ENTRY
111
222
111
0
相关文章:
- 显示错误输出的简单数组排序程序
- Dijkstra 中的错误输出
- 自定义runtime_error,如果我在 #what 中使用#c_str(),则错误输出始终为空
- 流字符串错误输出
- C++ 异常处理错误输出
- C ++中的错误输出,i = 65530,当我们打印i的值时,它给出-6状态65530
- SSE和iostream:浮点类型的错误输出
- C++将 popen 错误输出读取为字符串
- 部分透视/高斯消除 - 交换列而不是产生错误输出的行
- C++,向量:获得包含 0 的错误输出
- 数组函数错误输出
- CC1plus 错误:输出文件名指定两次
- 字符串错误输出
- 子斯特的错误输出
- 对大量c++错误输出完全一无所知.我认为这与预定义的函数和迭代器有关
- 简单程序的错误输出
- XCode调试器lldb中断,没有错误输出,但程序运行良好
- std::string::append(std::string)错误输出
- 查看大错误输出的顶行
- 使用结构的错误输出