我在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中编译。它打开文件,但不回显文件中的任何信息,而只是打印&#34;按任意键退出。 。 &#34;消息。
答案 0 :(得分:3)
您有string input;
和cin >> input;
。这些语句需要<string>
标题,但您没有(明确地)包含它。在某些实现中,您可以免费乘坐游戏,因为<iostream>
包含<string>
标头。但你不应该。始终包含适当的标题:
#include <string>
没有上面的标题你的代码will compile在Linux上使用g ++(这是你正在使用的)但在使用Visual C ++的Windows上没有。话虽如此,使用std::getline接受来自标准输入的字符串而不是std::cin
。