2011-02-01 10 views
2

私のプログラムは以下の通りですが、w、d、b、またはqで始まる単語を入力すると、文字dの代わりにdepositというようにプログラムは繰り返しています最初の質問は「あなたは引き出し、預金などしたいですか?私はcin >>をgetline(cin、user_request)に変換しようとしましたが、それはエラーです。私はこれを解決する方法は、getlineにcinを変換することですが、どのように適切にするかわからないことを確信しています。もし誰かが手伝ってくれたら。このプログラムをcin文で行う方法はありますか? :cinの代わりにgetlineを使う>>

#include <iostream> 
#include <string> 
using namespace std; 

int main() 
{ 
    char user_request; 
    string user_string; 
    double account_balance, dollars_withdraw, dollars_deposit; 

    account_balance = 5000; 
    user_request= user_string[0]; 


    while(account_balance =>0) 
    { 
     cout << "Would you like to Withdraw, Deposit, Check your balance or Quit?" 
      << endl; 
     cin >> user_request; 
     if (user_request == 'w' || user_request== 'W') 
     { 
      cout << "How much would you like to withdraw?" << endl; 
      cin >> dollars_withdraw; 
     if (dollars_withdraw > account_balance) 
      cout << "Invalid transaction" << endl; 
     else 
      account_balance = account_balance - dollars_withdraw; 
      cout << "Your new balance is $" << account_balance << endl; 
     } 

     if (user_request == 'd' || user_request== 'D') 
     { 
      cout << "How much would you like to deposit?" << endl; 
      cin >> dollars_deposit; 
      account_balance= account_balance + dollars_deposit; 
      cout << "Your new balance is $" << account_balance << endl; 
     } 
     if (user_request == 'b' || user_request == 'B') 
     { 
      account_balance= account_balance; 
      cout << "Your available balance is $" << account_balance << endl; 
     } 
     if (user_request == 'q' || user_request == 'Q') 
      break; 
    } 

    cout << "Goodbye" << endl; 



    return 0; 
} 
+0

私たちが修正しようとしていることを私たちが知るように、あなたの質問に誤りを貼り付けてください。 –

答えて

4

user_requestは、これだけで7回から「入金」がループに入るchar、別のcin >> user_request;文を満足させる入力における各非空白文字、ですが'd'はifステートメントと一致しますが、毎回プロンプトメッセージが表示されます。

あなたはgetline(cin, user_request)ます仕事、その後、std::stringするuser_requestを変更する必要がありますが、あなたは(すなわち[0])例えばするuser_requestの最初の文字を比較する必要があります'w'(文字リテラル)の代わりに"w"(文字列リテラル)。

入力要求が機能しているかどうかを確認することも、ファイルの終わりの状態がループを永遠に「回転」させるようにすることをお勧めします。

プロンプトを変更して入力したい文字が表示されるようにすることもお勧めします。そうしないと、おそらく入力します。 「小切手残高」では「b」ではなく「c」である。ただ、cout何かのように:だから

Would you like to [W]ithdraw, [D]eposit, Check your [b]alance or [Q]uit? 

、すべてのアップ:

std::string user_request; 

if (not getline(cin, user_request)) // or !getline(...) if your compiler lacks "not" 
    break; 

if (user_request.empty()) 
    continue; // avoid [0] on an empty line, which may crash the program 

if (user_request[0] == "w" || user_request[0] == "W") 
    ... 
+0

彼はすでにstd :: stringを含んでいます(user_stringはstd :: string型です) – Alan

+0

@Alan:そうです、歓声です。 –

+0

@Alan:彼は 'char 'だった' getline(cin、user_request) 'をやっていました。彼はエラーが何であるか教えてくれませんでしたが、 'getline'で' char'を使用するのはおそらくエラーでした。 –

0

はいのstd :: getlineの、入力ストリーム、とstd ::文字列、またはcharをとる*

ます1つの文字を渡そうとしているので、エラーが発生しています。

FWIWでは、getline()をstd :: stringで使用できます。

+0

'std :: string'を使用するには、[' std :: getline() '](http://www.cplusplus。 'std :: istream :: getline()'メンバ関数ではなく、com/reference/string/getline /)の自由な関数です。 'std :: string'をとるメンバ関数のオーバーロードはありません。 – sth

+0

ああ、ありがとう、それは私が意味するものです。 – Alan

2

いずれか(空白で区切られた)その意志入力ワード

std::string input; 
if(std::cin >> input) { 
    if(! input.empty() && input[0] == 'd' || input[0] == 'D') 
     // ... 
} 

又はstd::getline()と行全体取り、標準からstd::stringを取る:場合には、もちろん

std::string line; 
if(std::getline(std::cin, line)) { 
    if(! line.empty() && line[0] == 'd' || line[0] == 'D') 
     // ... 
} 

std::stringstd::cinから入力すると、ユーザーが複数の単語を入力すると、質問に記載されている複数の文字の場合と同じ「問題」が発生します。

0

私はそれが>>に来ると、それはしようとすると、「失敗国家」と入力しますときcinは少し気難しいあり、前述したように

cin >> user_string; 
user_request= user_string[0]; 
1

cin >> user_request; 

を交換しようとします間違った型の値を読み込みます。他にも多くの障害モードがありますので、コンソールからの入力を読み取るには、getlinestringstreamsの組み合わせを使用することをお勧めします。興味のある方は、Stanfordの入門C++プログラミングコースhas an entire chapter on how to do thisをオンラインでご利用いただけます。coutとファイルI/Oの基本から、コンソールからデータを正しく安全に取得する方法までを網羅しています。

希望すると便利です。

1

あなたは文字列変数を宣言し使用し、その後、

getline (cin,user_guest); //This is correct. 

しかし、あなたは変数のcharを使用する場合、それはまた別の問題です。 getlineは適用されません。これを使用してください:

char user_guest; 
cin.get(user_request); 
関連する問題