簡體   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