Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法打印出预期结果_C++ - Fatal编程技术网

C++ 无法打印出预期结果

C++ 无法打印出预期结果,c++,C++,当我编译下面的代码时,由于某种原因,学生/讲师的姓名、年龄和GPA/评分不会通过各自的printPerson函数返回 使用name变量,不会向控制台打印任何内容。 根据年龄、GPA/等级,console打印出一个负8位数字和一个负浮动 我没有看到什么 人 #pragma once #ifndef PERSON_H #define PERSON_H #include <iostream> #include <vector> #include <string>

当我编译下面的代码时,由于某种原因,学生/讲师的姓名、年龄和GPA/评分不会通过各自的printPerson函数返回

使用name变量,不会向控制台打印任何内容。 根据年龄、GPA/等级,console打印出一个负8位数字和一个负浮动

我没有看到什么

#pragma once
#ifndef PERSON_H
#define PERSON_H
#include <iostream>
#include <vector>
#include <string>


using std::string;

// Base class
class Person {

protected:
    string name;
    int age;

public:
    void setName(string name);
    void setAge(int age);
    virtual void do_work(int number) {};
    virtual void printPerson() {};
};

#endif;
#pragma一次
#ifndef人员
#定义人
#包括
#包括
#包括
使用std::string;
//基类
班主任{
受保护的:
字符串名;
智力年龄;
公众:
void setName(字符串名);
无效设置(整数);
虚空作功(整数){};
虚拟void printPerson(){};
};
#endif;
Person.cpp

#include "Person.h"
#include <iostream>
#include <vector>
#include <string>

using std::cout;
using std::cin;
using std::endl;


void Person::setName(string name) {
    name = name;
}

void Person::setAge(int age) {
    age = age;
}
#包括“Person.h”
#包括
#包括
#包括
使用std::cout;
使用std::cin;
使用std::endl;
void Person::setName(字符串名称){
名称=名称;
}
void Person::设置年龄(整数年龄){
年龄=年龄;
}
学生

#pragma once
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
#include <vector>
#include <string>
#include "Person.h"

using std::string;

class Student : public Person {

private:
    float gpa;


public:
    void setGPA(float gpa);
    float getGPA();
    void do_work(int number);
    void printPerson();
};

#endif;
#pragma一次
#ifndef学生会
#定义学生
#包括
#包括
#包括
#包括“Person.h”
使用std::string;
班级学生:公众人士{
私人:
浮动gpa;
公众:
无效设定gpa(浮动gpa);
float getGPA();
无效工作(整数);
无效打印人();
};
#endif;
Student.cpp

#include "Student.h"
#include <iostream>
#include <vector>
#include <string>


using std::string;
using std::cout;
using std::cin;
using std::endl;



 void Student::setGPA(float gpa) {
       gpa = gpa;
 }

 float Student::getGPA() {
       return gpa;
 }

 void Student::do_work(int number) {
       //cout << name << ".. " << number << "hours of homework.” << endl;
       cout << name;
 }

 void Student::printPerson() {
       cout << "Name : " << name << "Age :" << age << " GPA : " << getGPA() << endl;
}
#包括“Student.h”
#包括
#包括
#包括
使用std::string;
使用std::cout;
使用std::cin;
使用std::endl;
无效学生::设置gpa(浮动gpa){
平均绩点=平均绩点;
}
浮动学生::getGPA(){
返回gpa;
}
无效学生::做作业(整数){

//无法请修改设置程序的所有代码


由于本地字符串name参数和您的类变量相同,您希望参数传递正确,但实际情况并非如此。

要找出问题所在,代码太多了是。请向您的
set
方法发布一个可能重复的参数,该参数与正在设置的成员变量同名。这将把参数值设置为参数值,而不是设置成员变量。请调用其他参数。
#pragma once
#ifndef INSTRUCTOR_H
#define INSTRUCTOR_H
#include <iostream>
#include <vector>
#include <string>
#include "Person.h"

class Instructor : public Person {

private:
    float rating;

public:
    void setRating(float rating);
    float getRating();
    void do_work(int number);
    void printPerson();
};

#endif;
#include "Instructor.h"
#include <iostream>
#include <vector>
#include <string>


using std::string;
using std::cout;
using std::cin;
using std::endl;

void Instructor::setRating(float rating) {
    rating = rating;
}

float Instructor::getRating() {
    return rating;
}

void Instructor::do_work(int number) {
    cout << name << "graded papers for" << number << "hours." << endl;
}

void Instructor::printPerson() {
    cout << " Name : " << name << " Age : " << age << " Rating : " << getRating() << endl;
}
#pragma once
#ifndef UNIVERSITY_H
#define UNIVERSITY_H
#include <iostream>
#include <vector>
#include <string>
#include "Person.h"
#include "Building.h"
#include "Student.h"


using std::cout;
using std::string;
using std::vector;

class University {

public:
    string name;
    vector<Person*> persons;
    vector<Building> buildings;

public:
    void printAllBuildings();
    void printAllPersonsRecord();
};

#endif;
#include "University.h"
#include <iostream>
#include <vector>
#include <string>


using std::string;
using std::cout;
using std::cin;
using std::endl;

void University::printAllBuildings() {
    cout << " Building Details : " << endl;
    for (int j = 0; j < buildings.size(); j++) {
       buildings[j].printBuilding();
    }
}

void University::printAllPersonsRecord() {
    cout << " Persons Details : " << endl;
    for (int i = 0; i < persons.size(); i++) {
       persons[i]->printPerson();
    }
}
#pragma once
#ifndef BUILDING_H
#define BUILDING_H
#include <iostream>
#include <vector>
#include <string>
#include "Person.h"

class Building {
public:
    string name;
    int size;
    string address;

public:
    void printBuilding();
};

#endif;
#include "Building.h"
#include <iostream>
#include <vector>
#include <string>


using std::string;
using std::cout;
using std::cin;
using std::endl;

void Building::printBuilding() {
    cout << " Name : " << name << " Address : " << address << endl;
}
#include "University.h"
#include "Person.h"
#include "Student.h"
#include "Instructor.h"

#include <iostream>
#include <vector>
#include <string>


using std::string;
using std::cout;
using std::cin;
using std::endl;

int main() {

    Student student;
    Instructor instructor;

    student.setName("deepak");
    student.setAge(12);
    student.setGPA(12.0);

    instructor.setName("rajdev");
    instructor.setAge(23);
    instructor.setRating(5.0);

    Building building;

    building.name = "block1";
    building.size = 2000;
    building.address = "noida sector-2";

    Building building2;

    building2.name = "block2";
    building2.size = 4000;
    building2.address = "noida sector-2";

    University university;

    university.name = "Oregon State University";
    university.persons.emplace_back(&student);
    university.persons.emplace_back(&instructor);
    university.buildings.push_back(building);
    university.buildings.push_back(building2);

    university.printAllBuildings();

    university.printAllPersonsRecord();

    int choice;
    bool isValidMainChoice = false;
    while (!isValidMainChoice) {
       cout << "Kindly choose one of the option from follwoing list of operations or Menu" << endl;
       cout << "1 : Prints names of all the buildings" << endl;
       cout << "2 : Prints names of everybody at the university" << endl;
       cout << "3 : Choose a person to do work" << endl;
       cout << "4 : Exit the program" << endl;
       cin >> choice;
       cout << "The value you entered is " << choice << endl;
       if (choice == 1) {
          university.printAllBuildings();
       }
       else if (choice == 2) {
          university.printAllPersonsRecord();
       }
       else if (choice == 3) {
          int personChoice;
          bool isInputValid = false;
          while (!isInputValid) {
             cout << "Kindly choose the one of the following option to provide person's details." << endl;
             cout << "5 : Student" << endl;
             cout << "6 : Instructor" << endl;
             cin >> personChoice;
             if (personChoice == 5) {
                isInputValid = true;
                string studentName;
                bool isValidName = false;
                while (!isValidName) {
                    cout << " Kindly enter Name of the student :" << endl;
                    cin >> studentName;
                    if (studentName.length() == 0) {
                       cout << " Name must not be blank. Kindly re-enter the student's name." << endl;
                    }
                    else {
                       isValidName = true;
                    }
                }
                int age1 = 0;
                bool isValidAge1 = false;
                while (!isValidAge1) {
                    cout << " Kindly enter age of the student :" << endl;
                    cin >> age1;
                    if (age1 < 0 || age1 > 100) {
                       cout << " Age must be geter than 0 or lessa then 100. Kindly re-enter the student's age." << endl;
                    }
                    else {
                       isValidAge1 = true;
                    }
                }
                float gpa;
                bool isValidGPA = false;
                while (!isValidGPA) {
                    cout << " Kindly enter GPA of the student :" << endl;
                    cin >> gpa;
                    if (gpa < 0.0 || gpa > 4.0) {
                       cout << " GPA must be geter than 0.0 or less then 4.0. Kindly re-enter the Student GPA." << endl;
                       isValidGPA = false;
                    }
                    else {
                       isValidGPA = true;
                    }
                }
                Student student;
                student.setName(studentName);
                student.setAge(age1);
                student.setGPA(gpa);
                university.persons.emplace_back(&student);
                university.printAllPersonsRecord();
             }
             else if (personChoice == 6) {
                isInputValid = true;
                string instructorName;
                bool isValidName = false;
                while (!isValidName) {
                    cout << " Kindly enter Name of the instructor :" << endl;
                    cin >> instructorName;
                    if (instructorName.length() == 0) {
                       cout << " Name must not be blank. Kindly re-enter the instructor's name." << endl;
                    }
                    else {
                       isValidName = true;
                    }
                }

                float rating;
                bool isValidRating = false;
                while (!isValidRating) {
                    cout << " Kindly enter rating of the instructor :" << endl;
                    cin >> rating;
                    if (rating < 0.0 || rating > 5.5) {
                       cout << " rating must be geter than 0.0 or less then 5.5. Kindly re-enter the instructor rating." << endl;
                       isValidRating = false;
                    }
                    else {
                       isValidRating = true;
                    }
                }
                int age2 = 0;
                bool isValidAge2 = false;
                while (!isValidAge2) {
                    cout << " Kindly enter age of the instructor :" << endl;
                    cin >> age2;
                    if (age2 < 0 || age2 > 100) {
                       cout << " Age must be geter than 0 or lessa then 100. Kindly re-enter the instructor's age." << endl;
                    }
                    else {
                       isValidAge2 = true;
                    }
                }
                Instructor instructor;
                instructor.setName(instructorName);
                instructor.setAge(age2);
                instructor.setRating(rating);
                university.persons.emplace_back(&instructor);
             }
             else {
                cout << "The value you entered is incorrct.Please r-enter the values." << endl;
             }
          }
       }
       else if (choice == 4) {
          isValidMainChoice = true;
          cout << " You are exits from system. Thanks You !!" << endl;
       }
    }
    return 0;
};
void Person::setName(string name) {
    //before-edit: name = name;
    this->name = name; //OR Person::name = name;
}