为什么我的结果在选择2(显示学生记录)后从文件中读取时显示垃圾?

时间:2016-04-05 20:18:06

标签: c++

        $window.open(LOCATION);

/////////////////////////////////////////////// //////////////////////////////我会喜欢那些对C ++更精通的程序员的帮助/// ////////////////////////////////////////////我被困在这里请任何人都可以帮助我。我是新的C ++。谢谢。

2 个答案:

答案 0 :(得分:0)

您在此处使用错误的打开模式 out.open("c:\\windows\\temp\\Student.txt",ios::app);

您需要使用ios::in,或者您也可以省略它,因为ifstream默认为ios::in

答案 1 :(得分:0)

让我们从main函数开始。

student* s;

你真的需要指针吗? 指针s未初始化。 在C ++中,您可以在不使用new的情况下声明对象(实例),例如:

  student s;

int n;
char ch;

更多未初始化的变量 您未在ch函数中使用main,请考虑将其删除。

以下语句在第一遍中使用未初始化的值。循环内容可能无法执行:

while(n!=4)

仅供参考,您可以将文本菜单声明为const字符数组,并将其写为一个块:

while(n!=4)
{
    const char menu_text[] =
        "\tWelcome to Student Record System\n"
        "\t==================================================\n"
        "\n\n\t1.Create Student Record\n"
        "\n\n\t2.Display Student Record\n"
        "\n\n\t3.CCNA Course Eligibility\n"
        "\n\n\t==================================================\n"
        "\n\n\tPlease Enter Your Choice (1-3) or 4 to exit: ";
    std::cout.write(menu_text, sizeof(menu_text) - 1U);  

您不需要将功能放在课堂上,他们可以独立。 void write_student(const student& s) {  // ... } 你可以这样执行:

if(n==1)
{
        write_student(s);
}

顺便说一下,您的s指针指向student类型,而不是student_record类型。在互联网上搜索" c ++切片"。

您可能想要:

if(n==1)
{
   // Remember, s doesn't need to be a pointer.
        s.input_student_information();
        s.write_student();
}

这些应该让你暂时待命。