いいえ、毎月のデータ請求を計算するプログラムを作成する必要があります。 私が立ち往生している唯一の事は、ユーザーが何も入力せずに入力をクリックすると、エラーを出すことができないということです。 get line()を使ってみましたが、charを読み取ることができません。また、私は何か間違っていると思うので、私はどのようにエラーを出すことができますか?入力を行わずにEnterキーを押したときにエラーが発生する方法を見つけることはできません
私はまたstd::getline()
を使用する最も簡単な方法は、その2番目のパラメータはstd::string
も持つことである「getlineの」
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
// State the different plans
cout << "A - $29.99 per month with 5000 megabytes.\n";
cout << " $0.05 per additional megabyte, but only\n";
cout << " $0.03 per additional megabyte if 50% or\n";
cout << " more of the data was used on weekends\n";
cout << "B - $39.99 per month with 7500 megabytes.\n";
cout << " $0.01 per additional megabyte\n";
cout << "C - $49.99 per month for unlimited megabytes.\n";
// Ask what plan the customer bought
cout << "Enter A, B, or C to chose the plan the user has purchased: ";
char input;
getline(cin, input);
// Capitalize the input
input = toupper(input);
// Ignore anything after the first letter
cin.ignore(100, '\n');
はい、それは私が学んだことクラスで。したがって、私はまだこれらの概念を使用しながらこのコードを動作させる方法を見つけようとしています。 –
あなたはあなたの質問でそれを明確にしておくべきです。それでも文字単位の入力を使用したい場合は、一度に1文字ずつ入力を読み込むには 'get()'を使用してください。読み込まれた最初の文字が '\ n'だった場合、入力は空です。 –
これを試すことができますが、ユーザーが何も入力しないとエラーメッセージが表示されるようにするにはどうすればよいですか? –