【问题标题】:c++ grade calculator console errorc++等级计算器控制台报错
【发布时间】:2011-10-09 10:27:19
【问题描述】:

我在运行程序时在控制台中遇到分段错误 我在 ubuntu(linux) 中使用代码块

编译时出现 0 个错误和 0 个警告

pastebin 中的代码 http://pastebin.com/wgSHPQjc

这里是代码

#include <iostream>
#include <string>
using namespace std;

class Year
{
    public:

Year(string sone, string stwo, string sthree, string sfour, string sfive, string ssix, string sseven, string seight)  // constructor
{
        subjectName[0] = sone;
        subjectName[1] = stwo;
        subjectName[2] = sthree;
        subjectName[3] = sfour;
        subjectName[4] = sfive;
        subjectName[5] = ssix;
        subjectName[6] = sseven;
        subjectName[7] = seight;

        sum = 0;
        percentage = 0;
}
void nameOfSem(string semName) // semster name
{
    name = semName;
    cout << "Enter your " << name  << " marks"<< endl;
}
//no of subjects in semster and  store marks in an array
void readMarks(int noOfSubjects)
{
    subjects = noOfSubjects;
    for(int i=0; i<subjects; i++)
    {
        cout << subjectName[i]; // print out subject name stored in the array
        cin >> yearName[i]; // input from keyboard of marks
        // while loop so that user enters marks from 0 to 100
        while (yearName[i]<0 || yearName[i] > 100 )
        {
            cout << "incorrect input please enter again: ";
            cin >> yearName[i];
        }
    }
}

// function for calculating avarage
void avarage()
{
    for(int j=0; j<subjects; j++) // addtion of marks (addtion of array)
    {
        sum += yearName[j];
    }
    cout << "the total  is : " << sum << endl;
    percentage = float(sum) / float(subjects);
    cout << "The percentage is :  " << percentage << endl;
}

            int sum; // for storing sum of marks

            string name; //  for storing name of the semister
            int subjects; // for storing number of subjects in semister
            float percentage; // calculating percentage in the sem
             int yearName[]; // array for string marks
             string subjectName[]; // array for storin g subject names

};


// main function
int main()
{
    cout << "Welcome to xxx uni " << endl;

  // constructor for storing subjects name in the array
   Year first("Appiled Physics ", "Electronic Devices circuits ", "Basic electrical Engineering ", "C & Data Structures", "English ", "Mathematical Methods ", "mathematics 1 ", "Engineering Drawing ");

  // name of the sem
   first.nameOfSem("First Year");

   //no of subjects and storing marks in the array
   first.readMarks(8);

   //calculating avarage
   first.avarage();




/*
   Year two( " " , " ", " ")
   second year object

   */
    return 0;
}

【问题讨论】:

  • 您是否尝试在调试器中运行它? (例如 gdb)SEGFAULT 在哪一行?

标签: c++ console segmentation-fault codeblocks


【解决方案1】:
int yearName[]; // array for string marks
string subjectName[]; // array for storin g subject names

如果您的编译器符合标准,则这两行不应编译。

使用std::vector

std::vector<int> yearName;
std::vector<string> subjectName;

然后使用push_back函数给它们添加元素。

Year(string sone, string stwo, string sthree, /*...*/)  
{
        subjectName.push_back(sone);
        subjectName.push_back(stwo);
        subjectName.push_back(sthree);
        //so on    
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-11
    • 2015-05-02
    • 2021-12-22
    相关资源
    最近更新 更多