C++ 我不断遇到分割错误:11个错误,我可以';我不明白为什么。。。c++;
该程序编译正常,但有时会产生分段错误 该程序应允许用户输入学生姓名、理论(70%)和实践论文(30%)的分数。这些数据应该保存到一个文件中,最后程序应该显示/存储学生的姓名和分数C++ 我不断遇到分割错误:11个错误,我可以';我不明白为什么。。。c++;,c++,C++,该程序编译正常,但有时会产生分段错误 该程序应允许用户输入学生姓名、理论(70%)和实践论文(30%)的分数。这些数据应该保存到一个文件中,最后程序应该显示/存储学生的姓名和分数 #include <iostream> #include <fstream> void disp(int); using namespace std; void stunames(int n) { int count = 0; string names; cout
#include <iostream>
#include <fstream>
void disp(int);
using namespace std;
void stunames(int n) {
int count = 0;
string names;
cout << "Input student names :" << endl;
ofstream na("names.txt");
while( count <= n ) {
getline(cin,names);
na << names << endl;
count++;
}
na.close();
}
void theomarks(int size) {
double marks;
int count = 0;
ofstream tho("T.txt");
while( count < size ) {
cin >> marks;
if((marks > 100) ||(marks < 0)){
cout << "Invalid marks, Re-enter" << endl;
count = count-1;
}
else
tho << marks*.7 << endl;
count++;
}
tho.close();
}
void pracmarks(int size) {
ofstream pr("P.txt");
double marks;
int count = 0;
while( count < size ) {
cin >> marks;
if((marks > 100) ||(marks < 0)){
cout << "Invalid marks, Re-enter" << endl;
count = count-1;
}
else
pr << marks*.3 << endl;
count++;
}
pr.close();
}
void calc(int size) {
ifstream na("names.txt");
ifstream readr("T.txt");
ifstream mo("P.txt");
string x;
double pracc[1][size];
double theory[1][size];
cout << "NAME\t\tMARKS" << endl;
for(int row = 0; row < size; row++) {
for(int col = 0; col < 1; col++) {
mo >> pracc[row][col];
readr >> theory[row][col];
na >> x;
cout << x << "\t\t" << theory[row][col]+pracc[row][col];
}
cout << endl;
}
readr.close();
mo.close();
na.close();
}
int main() {
int no;
cout << "Input the number of student: " << endl;
cin >> no;
stunames(no);
cout << "Input Theory Paper Marks" << endl;
theomarks(no);
cout << "Input practical Paper Marks" << endl;
pracmarks(no);
calc(no);
return 0;
}
#包括
#包括
无效显示(int);
使用名称空间std;
虚空特技团(int n){
整数计数=0;
字符串名;
cout在表达式pracc[row][col]中;行和列范围混乱。行必须小于1;
最好使用::std::array而不是C样式的数组。它将在相应的时刻为您提供一个正确的调试断言,而不是突然出现的分段错误。您正在这样做
mo>>pracc[row][col];
但您的数组定义如下:
double pracc[1][size];
而行
大于1。因此,您将通过数组的边界。您可能需要
double pracc[size][1];
行和列看起来混淆了。作为一名学生,你在编程方面能做的最好投资就是花一些时间学习使用调试器并精通调试器。传奇程序员唐纳德·克努斯(Donald Knuth)曾被问到他最喜欢的计算机语言是什么,他的回答是“一种具有良好调试器的语言”。不,双倍prac[1][size]表示数组的大小2*size,prac[1][size]={{21},{67},….n};等等事实上,您的循环访问pracc[row][col],而row超过1,因此您的数组定义是错误的。或者您可以保持数组定义不变,并执行pracc[col][row]。任何一种方法都有效。请记住将答案向上投票,如果此答案“正确”,请单击复选标记。它会让其他人知道问题的答案。