2017-11-17 5 views
2

この問題では、データが読み込まれるファイル名を尋ねる必要があります。また、「def」に答えられた場合は、提供されているデフォルトファイルを使用し、ファイル内の行数を数えます。 次に、データファイルを一度に1行読み込み、wの値を次のように計算します。 -wが 'add'ならx + yを計算します -wが 'sub'ならxyを計算します -ifが ' x 'を計算するx * y - wが' div 'の場合、x/yを計算するファイルからオペレータを読み込み、操作を実行するC++

この結果を一度に1行ずつ画面に出力する。ファイル内のデータの

例:

MULT 4.25 4.56

のdiv 7.64 1.01

のdiv 6.51 2.46

MULT 8.90 6.16

MULT 7.40 8.53

サブ3.05 7.15

サブ9.51 6.16

サブ5.79 1.60

追加5.30 8.87

サブ8.09 1.65

そして、私のコード:私は最初の3つの部分が動作していたが操作を実行するためのwhileループは機能しません。どこが間違っていますか?ありがとう!

#include <iostream> 
#include <cstdlib> 
#include <fstream> 

using namespace std; 

int main(){ 

    ifstream input; 
    string filename; 
    cout<<"Enter the file name. Enter 'def' to use default file: "; 
    cin>>filename; 

    if(filename == "def"){ 
     input.open("M2-P2-data2Read.dat"); 
    } 
    else{ 
     input.open(filename.c_str()); 
    } 

    int number_of_lines = 0; 
    string line; 

    while (getline(input, line)) 
     ++number_of_lines; 
    cout << "Number of lines in file: " << number_of_lines; 

    string w; 
    double x, y; 

    input >> w; 

    while(input) { 
    if(w == "add") 
     double x, y; 
     input >> x >> y; 
     cout << "Solution: " << x+y << endl; 
    if(w == "sub") 
     double x, y; 
     input >> x >> y; 
     cout << "Solution: " << x-y << endl; 
    if(w == "mult") 
     double x, y; 
     input >> x >> y; 
     cout << "Solution: " << x*y << endl; 
    if(w == "div") 
     double x, y; 
     input >> x >> y; 
     cout << "Solution: " << x/y << endl; 
    input >> w; 

    } 


} 

答えて

0

あなたはif文の後にコードのブロックを作成するためにあなたの意思を表明しているにもかかわらず、コードが実際にブロックされていません。ライン

if(w == "add") 
    double x, y; 
    input >> x >> y; 
    cout << "Solution: " << x+y << endl; 

あなたは次の if文を変換する場合、あなたはあなたのコードは xy二度目に読み取ろうとする

if(w == "sub") 
{ 
    double x, y; 
} 
input >> x >> y; 
cout << "Solution: " << x-y << endl; 

取得、今

if(w == "add") 
{ 
    double x, y; // A declaration that does not have any impact on 
       // the rest of the lines. 
} 
input >> x >> y; // These variables are the x and y that were declared 
       // before the while statement. 
cout << "Solution: " << x+y << endl; 

に相当します操作を読み取ることなく、読み取る番号がないので失敗します。その後、すべての読み取り操作は失敗します。

if以降の文をブロックする必要があります。

if(w == "add") 
{ 
    input >> x >> y; 
    cout << "Solution: " << x+y << endl; 
} 

あなたはif文の外xyを読んで行を移動することで、あなたのコードビットを簡素化することができます。

input >> x >> y; 
if(w == "add") 
{ 
    cout << "Solution: " << x+y << endl; 
} 

else if(w == "sub") // Use else if instead of just if. 
{ 
    cout << "Solution: " << x-y << endl; 
} 

など

+0

私はそれを逃すためのダムを感じます。しかし、変更した後もまだ動作しません –

+0

@TylerEmmerich、おそらく 'getline'を使ってファイルの内容を読んだり、' input'をファイルの先頭を指し示すように変更したりしないコード行が原因です。 –

+0

それはそれでした!どうもありがとうございます –

0

私はこれがあなたが目指しているものだと思う:

#include <iostream> 
#include <cstdlib> 
#include <fstream> 
#include <string> 

int main() { 
    std::ifstream input; 
    std::string filename; 
    std::cout << "Enter the file name. Enter 'def' to use default file: "; 
    std::cin >> filename; 

    if (filename == "def") { 
     input.open("Sample.dat"); 
    } else { 
     input.open(filename.c_str()); 
    } 

    int number_of_lines = 0; 
    std::string line; 

    while (getline(input, line)) { 
     ++number_of_lines; 
    } 
    std::cout << "\nNumber of lines in file: " << number_of_lines << "\n\n"; 

    input.clear(); 
    input.seekg(0, input.beg);  

    std::string w; 
    double x = 0; 
    double y = 0; 

    while (input >> w >> x >> y) { 

     if (w == "add") { 
      std::cout << "Solution: " << x + y << std::endl; 
     } 

     if (w == "sub") { 
      std::cout << "Solution: " << x - y << std::endl; 
     } 

     if (w == "mult") { 
      std::cout << "Solution: " << x * y << std::endl; 
     } 

     if (w == "div") { 
      // Should test for denominator = 0 
      std::cout << "Solution: " << x/y << std::endl; 
     } 

    } 


    std::cout << "\nPress any key and enter to quit." << std::endl; 
    char c; 
    std::cin >> c; 

    return 0; 
} 
関連する問題