2017-10-04 43 views
-2

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

#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编译。它打开文件,但不会回显文件中的任何信息,而只是打印“按任意键退出...”。信息。

+1

你看到了什么错误信息?它编译了吗?也许你只需要包括'#include '? – wally

+1

它在哪里不起作用,它究竟如何不起作用,与编辑器无关,可能是错误的文件,也可能是编译器等 –

回答

3

您有string input;cin >> input;。这些语句需要<string>标题,但您没有明确包含它。在某些实施中,您可以免费乘坐,因为<iostream>包含<string>标头。但你不应该。始终包含相应的头:

#include <string> 

没有使用Visual C++上面的头你使用G ++(这是你使用的是什么),在Linux代码will compile但Windows。这就是说使用std::getline接受来自标准输入的字符串而不是std::cin

+0

这很好用。非常感谢你的帮助! –

相关问题