C++ 代码在 Gedit 中有效,但在 VS 中无效(它只打印 "press any key to exit . . ." 消息)

C++ code works in Gedit, but not in VS (it just prints the "press any key to exit . . ." message)

我在 Windows 上的 Visual Studio 中编写了一个程序,程序编译正确,但没有在控制台上显示所需的输出。但是,如果我在 Linux 上编译并 运行 Gedit 中的程序,则会显示正确的输出并且一切正常。为什么是这样?代码如下:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
string input;

cout << "College Admission Generator\n\n";

cout << "To begin, enter the location of the input file (e.g. C:\yourfile.txt):\n";
cin >> input;


ifstream in(input.c_str());

if (!in)
{
    cout << "Specified file not found. Exiting... \n\n";
    return 1;
}

char school, alumni;
double GPA, mathSAT, verbalSAT;
int liberalArtsSchoolSeats = 5, musicSchoolSeats = 3, i = 0;

while (in >> school >> GPA >> mathSAT >> verbalSAT >> alumni)
{

    i++;

    cout << "Applicant #: " << i << endl;
    cout << "School = " << school;
    cout << "\tGPA = " << GPA;
    cout << "\tMath = " << mathSAT;
    cout << "\tVerbal = " << verbalSAT;
    cout << "\tAlumnus = " << alumni << endl;

    if (school == 'L')
    {
        cout << "Applying to Liberal Arts\n";

        if (liberalArtsSchoolSeats > 0)
        {

            if (alumni == 'Y')
            {

                if (GPA < 3.0)
                {
                    cout << "Rejected - High school Grade is too low\n\n";
                }

                else if (mathSAT + verbalSAT < 1000)
                {
                    cout << "Rejected - SAT is too low\n\n";
                }

                else
                {
                    cout << "Accepted to Liberal Arts!!\n\n";
                    liberalArtsSchoolSeats--;
                }
            }

            else
            {
                if (GPA < 3.5)
                {
                    cout << "Rejected - High school Grade is too low\n\n";
                }

                else if (mathSAT + verbalSAT < 1200)
                {
                    cout << "Rejected - SAT is too low\n\n";
                }

                else
                {
                    cout << "Accepted to Liberal Arts\n\n";
                    liberalArtsSchoolSeats--;
                }
            }
        }

        else
        {
            cout << "Rejected - All the seats are full \n";
        }
    }

    else
    {
        cout << "Applying to Music\n";

        if (musicSchoolSeats>0)
        {
            if (mathSAT + verbalSAT < 500)
            {
                cout << "Rejected - SAT is too low\n\n";
            }

            else
            {
                cout << "Accepted to Music\n\n";

                musicSchoolSeats--;
            }
        }

        else
        {
            cout << "Rejected - All the seats are full\n";
        }
    }
    cout << "*******************************\n";
}
return 0;
}

澄清一下,该程序确实在 VS 中编译。它打开文件,但不回显文件中的任何信息,而只是打印“按任意键退出...”。留言。

您有 string input;cin >> input;。这些语句需要 <string> header 但您没有(明确地)包含它。在某些实现中,您可以免费乘车,因为 <iostream> 包含 <string> header。但你不应该。始终包含适当的 header:

#include <string>

如果没有上述 header,您的代码 will compile on Linux using g++ (which is what you are using) but not on Windows using Visual C++. That being said use std::getline 将接受来自标准输入的字符串而不是 std::cin