繁体   English   中英

C++ 代码在 Gedit 中有效,但在 VS 中无效

[英]C++ code works in Gedit, but not in VS

我在 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>标头,但您没有(明确地)包含它。 在某些实现中,您可以免费乘坐,因为<iostream>包含<string>标头。 但你不应该。 始终包含适当的标题:

#include <string>

如果没有上述头文件,您的代码使用 g++(这是您正在使用的)在 Linux 上编译,但不能在使用 Visual C++ 的 Windows 上编译。 也就是说,使用std::getline来接受来自标准输入的字符串而不是std::cin

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM