无法获取数组

时间:2017-04-15 12:58:45

标签: c++ arrays

以下数组的总和未正确显示,而是显示大量数字。我做错了什么。

void putdata(){
        int mark_sum;
        cout << name << " " << age << " ";
        for(int i=0;i<6;i++){
            mark_sum += marks[i];
        }
        cout << mark_sum << " ";
        cout <<cur_id - no_of<< endl;
        no_of--;
    }

在这个函数里面标记[]数组包含一些整数元素,在得到它之后得到错误的总和。

这是完整的,

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

//classes
//classes
class Person{
public:
    virtual void getdata() = 0;
    virtual void putdata() = 0;
protected:
    string name;
    int age;
};

class Professor : public Person{
public:
    Professor() {
    }
    void getdata(){
        cin >> name;
        cin >> age >> publications;
        cur_id++;
        no_of++;
    }
    void putdata(){
        cout << name << " ";
        cout << age << " ";
        cout << publications << " ";
        cout <<cur_id - no_of<< endl;
        no_of--;
    }
private:
    int publications;
    static int cur_id;
    static int no_of;
};

class Student : public Person{
public:
    Student(){
    }
    void getdata(){
        cin >> name >> age;
        for(int i=0;i<6;i++){
            cin >> marks[i];
        }
        cur_id++;
        no_of++;
    }
    void putdata(){
        int mark_sum;
        cout << name << " " << age << " ";
        for(int i=0;i<6;i++){
            mark_sum += marks[i];
        }
        cout << mark_sum << " ";
        cout <<cur_id - no_of<< endl;
        no_of--;
    }
private:
    int marks[6];
    static int cur_id;
    static int no_of;
};

//static variables
int Student::cur_id = 0;
int Professor::cur_id = 0;
int Student::no_of = -1;
int Professor::no_of = -1;

//main
int main(){

    int n, val;
    cin>>n; //The number of objects that is going to be created.
    Person *per[n];

    for(int i = 0;i < n;i++){

        cin>>val;
        if(val == 1){
            // If val is 1 current object is of type Professor
            per[i] = new Professor;

        }
        else per[i] = new Student; // Else the current object is of type Student

        per[i]->getdata(); // Get the data from the user.

    }

    for(int i=0;i<n;i++)
        per[i]->putdata(); // Print the required output for each object.

    return 0;

}

我输入了这个,

  

4   1   沃尔特56 99   2   Jesse 18 50 48 97 76 34 98   2   Pinkman 22 10 12 0 18 45 50   1   怀特58 87

我的预期输出将是这个,

  

Walter 56 99 1 Jesse 18 403 1 Pinkman 22 135 2 White 58 87 2

1 个答案:

答案 0 :(得分:2)

定义时,应将'mark_sum'初始化为零。

在putdata函数的第一行,而不是int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QFileSystemWatcher watcher; watcher.addPath(QStringLiteral("qrc:/watchedFile.txt")); QQmlApplicationEngine* engine = new QQmlApplicationEngine; engine->rootContext()->setContextProperty("cppWatcher", &watcher); engine->load(QUrl(QStringLiteral("qrc:/main.qml"))); return app.exec(); } ,只需编写import QtQuick 2.7 import QtQuick.Controls 2.0 ApplicationWindow { visible: true width: 640 height: 480 Text { id: text text:"TEXT" } Connections { target: cppWatcher onFileChanged: { text.text = "CHANGED" } } } 即可。

问题是如果你没有初始化整数变量,你可能不会使用它,因为你不知道它的值。

这里有更多细节: Define integer (int); What's the default value?