2017-10-04 13 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; 
} 

ありがとうございました!

EDIT:fluffを削除しました。

明確にするために、プログラムはVSでコンパイルされます。それはファイルを開きますが、ファイルからの情報をエコーし​​ません。代わりに "終了するには任意のキーを押してください。"メッセージ。

+1

どのようなエラーメッセージが表示されましたか?それはコンパイルしましたか?おそらく '#include 'をインクルードする必要がありますか? – wally

+1

正確には動作しません。正確に動作しません。エディタとは関係ありません。おそらくファイルが間違っているかもしれません。コンパイラなど –

答えて

3

string input;cin >> input;です。これらのステートメントには<string>ヘッダーが必要ですが、(明示的に)ヘッダーは含めませんでした。いくつかの実装では、<iostream>には<string>ヘッダーが含まれているため、フリーライドで逃げることができます。しかし、あなたはすべきではありません。常に適切なヘッダを含める:上記ヘッダーなし

#include <string> 

Linux上のコードwill compile(あなたが使用しているものである)G ++を使用しますが、Windows上のVisual C++のを使用していません。それはstd::cinの代わりに標準入力から文字列を受け取るためにstd::getlineを使用すると言われています。

+0

これは完全に機能しました。あなたの助けをありがとう! –

関連する問題