【问题标题】:My code works in Qt Creator but not in Cloud9我的代码在 Qt Creator 中有效,但在 Cloud9 中无效
【发布时间】:2018-04-11 02:52:58
【问题描述】:

所以我创建了一个链接列表,它应该从文件中打印出姓名、年龄、专业和 GPA。

当我在 Qt Creator 中运行它时似乎可以正常工作,但是当我尝试在 cloud9 中运行它时,它似乎并没有成功。

例如,findStudent 函数没有找到学生,而是显示为“未找到学生”。

它似乎也不喜欢我格式化它的方式,节点中的所有内容(姓名、年龄、专业、gpa)看起来都打印在彼此之上。

它会这样做,除非我将每个信息的信息标记到最右边,但这并不是我真正想要的,因为在 cloud9 之外它看起来并不那么好。

studentlist.h:

#ifndef STUDENTLIST_H
#define STUDENTLIST_H
#include <iostream>
#include <iomanip>
#include <fstream>
#include <stdlib.h>

using namespace std;

struct StudentNode{
    string name;
    int age;
    string major;
    double gpa;
    StudentNode *link;
};

void createLinkedList(StudentNode *&head);
void findStudent(StudentNode *&head, string find);
void removeStudent(StudentNode *&head);
double calculateGPA(StudentNode *&head);
void printList(StudentNode *&head);


#endif // STUDENTLIST_H

studentlist.cpp:

#include "studentlist.h"

void createLinkedList(StudentNode *&head){
    StudentNode *last = NULL;
    StudentNode *newNode = NULL;

    fstream inFile;

    inFile.open("inFile.txt");

    if (inFile.fail()){

        cout << "file failed to open" << endl;
        exit(1);
    }
    else{

        string newLine;

        getline(inFile, newLine);
        newNode = new StudentNode;
        getline(inFile, newNode->name);
        inFile >> newNode->age;
        getline(inFile, newLine);
        getline(inFile,newNode->major);
        inFile >> newNode->gpa;
        newNode->link = NULL;
        head = newNode;
        last = newNode;

        while(!inFile.eof()){
            getline(inFile, newLine);
            getline(inFile, newLine);
            newNode = new StudentNode;
            getline(inFile, newNode->name);
            inFile >> newNode->age;
            getline(inFile, newLine);
            getline(inFile,newNode->major);
            inFile >> newNode->gpa;
            newNode->link = head;
            head = newNode;
        }

    }

    inFile.close();
}

void findStudent(StudentNode *&head, string find){

    StudentNode *current = head;

    if(current->link ==  NULL){
        cout << "Student Not Found" << endl;
    }
    else if(current->name == find){
        cout << "Student Found:" << endl;
        cout <<left<< setw(15);
        cout <<current->name <<setw(7);
        cout << current->age<<setw(15);
        cout << current->major << setw(4);
        cout << current->gpa;
    }
    else{
        findStudent(current->link, find);
    }
}

void removeStudent(StudentNode *&head){
    StudentNode *temp_ptr;

    temp_ptr = head->link;

    cout << "Front Node Deleted:" << endl;

    cout << left << setw(15);
    cout << "Name" <<setw(7) << "Age" <<setw(15)
         << "Major" <<setw(4) << "GPA" <<setw(15) << endl;

    cout <<head->name <<setw(7);
    cout << head->age<<setw(15);
    cout << head->major << setw(4);
    cout << head->gpa;

    delete head;

    head = temp_ptr;

    temp_ptr = NULL;
}

double calculateGPA(StudentNode *&head){

    StudentNode *current = head;
    double calculatedGPA;
    int num_students=0;

    while(current!= NULL){

        calculatedGPA += current->gpa;
        current = current->link;
        num_students++;
    }

    cout << fixed << setw(3) << setprecision(2);
    return calculatedGPA/num_students;
}

void printList(StudentNode *&head){
    StudentNode *current = head;

    cout << left << setw(15) << "Name"
         <<setw(7)<<"Age" << setw(15)<< "Major"
        << setw(7) << "GPA" << endl;


    while(current!= NULL){

        cout << setw(15) << current->name<< setw(7);
        cout << current->age << setw(15);
        cout << current->major << setw(7);
        cout << current->gpa << endl;

        current = current->link;
    }
}

main.cpp:

#include <iostream>
#include  "studentlist.h"

using namespace std;

int main(int argc, char *argv[])
{
    StudentNode *head;

    createLinkedList(head);

    printList(head);

    cout << endl;
    findStudent(head, "Anna White");
    cout << endl << endl;

    removeStudent(head);
    cout << endl << endl;

    cout << "Updated Student List: " << endl;
    printList(head);
    cout << endl;

    cout << "GPA: " << calculateGPA(head) << endl;

    return 0;
}

QT 中的输出:

Name           Age    Major          GPA
Paul Johnson   18     Physics        3.7
Anna White     19     English        3.2
John Smith     20     Math           3.5
Anthony Rogers 21     Art            3.1
Cynthia Morris 24     History        3.6

Student Found:
Anna White     19     English        3.2

Front Node Deleted:
Name           Age    Major          GPA
Paul Johnson   18     Physics        3.7

Updated Student List:
Name           Age    Major          GPA
Anna White     19     English        3.2
John Smith     20     Math           3.5
Anthony Rogers 21     Art            3.1
Cynthia Morris 24     History        3.6

GPA: 3.35
Press <RETURN> to close this window...

在 Cloud9 中输出而不重新格式化:

Name           Age    Major          GPA    
       3.7    cs
       3.2    lish
          3.5    
           3.1    
       3.6    

Student Not Found


Front Node Deleted:
Name           Age    Major          GPA 
       3.7 ysics

Updated Student List: 
Name           Age    Major          GPA    
       3.2    lish
          3.5    
           3.1    
       3.6    

GPA: 3.35

cloud9 中重新格式化的输出:

Name            Age     Major           GPA
Paul Johnson    18      Physics         3.7
Anna White      19      English         3.2
John Smith      20      Math            3.5
Anthony Rogers  21      Art             3.1
Cynthia Morris  24      History         3.6

Student Not Found


Front Node Deleted:
Name            Age     Major           GPA
Paul Johnson    18      Physics         3.7

Updated Student List: 
Name            Age     Major           GPA
Anna White      19      English         3.2
John Smith      20      Math            3.5
Anthony Rogers  21      Art             3.1
Cynthia Morris  24      History         3.6

GPA: 3.35

【问题讨论】:

    标签: c++ qt-creator cloud9


    【解决方案1】:

    FindStudent: 有几个问题

    首先,在评估列表中的名称之前,您要检查当前节点是否位于列表末尾。这可能是你的错误。如果列表中只有一个项目,或者开始搜索的项目位于列表末尾,findStudent 将看到节点的链接成员为空,并在评估 current-&gt;name==find 之前返回。

    其次,findStudent 是递归的。如果列表足够大,findStudent 会造成堆栈溢出。 (而且你的程序会崩溃)。

    您的代码,已修复:

    StudentNode* findStudent(StudentNode *head, const string& find) {
        StudentNode *current = head;
    
        while ((current != NULL) && (current->name != find)) {
            current = current->link;
        }
    
        if (current != NULL) {
            cout << "Student Found:" << endl;
            cout <<left<< setw(15);
            cout <<current->name <<setw(7);
            cout << current->age<<setw(15);
            cout << current->major << setw(4);
            cout << current->gpa;
        }
        else {
            cout "Student not found" << endl;
        }
    
        return current;
    }
    

    您遇到的另一个错误:

    double calculateGPA(StudentNode *&head){
    
        StudentNode *current = head;
        double calculatedGPA;  // UNINITIALIZED VARIABLE!
        int num_students=0;
    
        while(current!= NULL){
    
            calculatedGPA += current->gpa;
    

    确保将caculatedGPA 初始化为0

        double calculatedGPA = 0;
    

    第三,所有函数都将列表头作为对指针的引用传递。通过指针或引用传递,但不能同时传递。改进的函数签名:

    StudentNode* createLinkedList();
    StudentNode* findStudent(const string& find);
    StudentNode* removeStudent(StudentNode *head); // removes the head and returns the list's new head:
    
    double calculateGPA(StudentNode* head);
    void printList(StudentNode* head);
    

    【讨论】:

    • 感谢您的快速响应,您提供的修复实际上是我在使用递归之前所做的,但我开始怀疑自己。我还没有学到很多关于递归的知识,所以我不确定什么时候最好使用它,这对我来说非常笨拙。我一定会坚持你提供的意见,这对我来说似乎很可靠。我仍然不知道为什么它在 qt-creator 中输出正常但在 cloud9 中输出错误。
    • 您需要学习如何使用调试器。 (或在您的代码中添加更多打印语句)。在一种环境中正常工作,但在另一种环境中不正确是未初始化变量或导致未定义行为的其他错误的症状。
    • 在 calculateGPA 中发现了另一个错误。往上看。您是否在启用所有警告的情况下进行编译?
    • 我启用了它们,但它没有给我任何警告。由于我使用的 inFile.txt,它在 cloud9 中的格式不正确。我在想也许它正在从 .txt 文件中获取空间,但我只是在猜测。我所做的是直接在 cloud9 中重新创建了文件,并且成功了。如果下次遇到类似问题,我会提供 .txt。我仍然很好奇你发现了什么错误,有什么可以提高我的编码技能的。
    猜你喜欢
    • 1970-01-01
    • 2018-05-15
    • 1970-01-01
    • 2020-08-09
    • 1970-01-01
    • 2012-10-28
    • 2018-02-22
    • 2019-05-18
    • 2013-01-16
    相关资源
    最近更新 更多