错误输入不在此范围内显示

时间:2016-04-06 11:25:33

标签: c++

我正在编写一个管理学生信息和标记的程序。我的问题是我收到了错误,例如第23,26,29,32和53行在此范围内输入不显示。任何人都可以建议我做错了吗?

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

int main()
{
    system("cls");
    system("Project Assignment");
    system("colr 0f");
    cout << "Please enter choice" <<endl;
    cout << "1. Input new student" <<endl;
    cout << "2. Search for student by number" <<endl;
    cout << "3. Edit an exiting student marks" <<endl;
    cout << "4. Display all students" <<endl;
    cout << "5. Exit" <<endl;

    int choice;
    cin >> choice; 

    switch (choice){
    case 1: 
        input();
        break;
    case 2:
        search();
        break;
    case 3:
        edit();
        break;
    case 4:
        displayAll();
        break;
}

    void input();
    {
        system("cls");
        string fname;
        string lname;
        string filename;
        int mark;
        int studNum;

        cout << "Input student first Name:" ;
        cin >> fname;
        cout << "Input student last name: ";
        cin >> lname;
        cout << "Input student mark: ";
        cin >> mark;
        cout << "Input student number: ";
        cin >> studNum;
        string studNum2 = to_string(studNum);
        studNum2.append(".txt");

        ofstream newstudent(studNum2);
        newstudent <<fname <<" " <<lname <<" "<<mark <<" "<<studNum << endl;
        newstudent.close();




    }
    void search();
    {

    }
    void edit();
    {

    }
    void displayall();
    {

    }
}

2 个答案:

答案 0 :(得分:2)

我只是在这里猜测,但我在你的程序中看到了两个问题:第一个是你尝试使用嵌套函数,即函数在另一个函数中。 C ++标准不允许这样做,因为一些编译器可能允许它作为语言的扩展。

第二个问题是你实际上定义 main函数中的函数,你只能声明它们。当你这样做

int input();
{
}

首先声明input函数原型,然后你有一个嵌套但空的范围。此问题应导致源实际上无法使用链接器错误进行构建。

还有第三个问题,那就是你必须在调用函数之前声明函数原型

程序的固定版本应如下所示:

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

// Declare function prototypes, so the compiler knows these functions exist
void input();
void search();
void edit();
void displayall();

int main()
{
    ...
    // Now you can call the functions
    input();
    ...    
}

void input()  // Note: No semicolon here
{
    ...
}

void search()  // Note: No semicolon here
{
    ...
}

void edit()  // Note: No semicolon here
{
    ...
}

void displayall()  // Note: No semicolon here
{
    ...
}

答案 1 :(得分:0)

在使用之前,您应声明功能。特别是input()函数在首次使用main()后声明和定义。