我不断得到细分错误:11错误,我不知道为什么... c ++

rama41222

该程序可以编译,但是有时会产生分段错误。

该程序应让用户输入学生姓名,获得理论的分数(70%)和实用论文的分数(30%)。这些数据应保存到文件中,最后程序应显示/存储学生的姓名和标记。

#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;
}
阿尔莫

你在做

mo>>pracc[row][col];

但是您的数组已定义:

double pracc[1][size];

row超过1。因此,您将越过数组的边界。你可能想要

double pracc[size][1];

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我不知道为什么我得到这个文件,但是仍然显示错误

C ++重新定义变量,全局命名空间被污染,我不知道为什么

我不断遇到细分错误,不知道为什么

我不知道为什么命令提示符在Visual Studio中跳过对简单C ++程序的输入

C中的堆已损坏,我也不知道为什么

不知道为什么我在这里遇到细分错误

C ++ unordered_map emplace()函数抛出seg错误,我也不知道为什么

问题在C中,我不知道为什么main无法读取“ or_vectb”

C ++ Segfault,我不知道为什么?

我不知道为什么我得到“未定义的不是函数”错误

我的C程序正在生成分段错误,我不知道为什么

布尔变量正在变化,我不知道为什么-C语言

我不断收到此错误消息,但我不知道为什么

我的C ++应用程序无法链接,我也不知道为什么

我不知道为什么我得到了System.IndexOutOfRangeException?C#

在我的C程序中需要一些帮助,我遇到了段错误,但我不知道为什么

不知道为什么在 C 中洗牌时会出现分段错误

不知道为什么我收到 StopIteration 错误

SMTP 错误,我不知道为什么

我的 C 程序在新行上打印 -39,我不知道为什么

使用数组获取错误 C2440,我不知道为什么

不知道为什么我在 java 中得到这个数组索引越界错误

我在 CSS 中不断收到此错误,但我不知道为什么

我不知道是什么导致了错误 (C3074)

当我尝试编译这个 C 代码时,我得到了一个无限真实的 bool 错误,我不知道为什么

我不知道为什么我无法回答。(c/c++)

问题是我的 C# 应用程序,我不知道错误是关于什么的

Dockerfile 错误的目录。我不知道为什么

我得到错误'java.lang.String'并且不知道为什么