I've written a program in Visual Studio on Windows, and the program compiles correctly, but does not display the desired output to the console. However, if I compile and run the program in Gedit on Linux, the correct output displays and everything works. Why is this? Code is below:
#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;
}
To clarify, the program does compile in VS. It opens the file, but does not echo any information from the file, and instead just prints the "press any key to exit . . ." message.
You have string input;
and cin >> input;
. These statements require the <string>
header but you did not include it (explicitly). In some implementations you can get away with free rides since <iostream>
includes the <string>
header. But you should not. Always include the appropriate header:
#include <string>
Without the above header your code 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 to accept strings from the standard input instead of std::cin
.