2017-10-04 74 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

這很好用。非常感謝你的幫助! –

相關問題