でforまたはwhileループ私はプログラミングIクラスの次の代入があります。各アイテムの価格のためのプロンプト配列されて一つは2つの、最大変数、(プログラミングI)
を。各商品の価格をpricesという配列に格納します。ユーザーが$ 1,000または5個のアイテムに達したら、どれくらいの額を消費したか、何個のアイテムを購入したか、残っている金額、平均価格などをプリントアウトします( )。
購入した商品の価格の表を印刷します。
私の元の試みは非常にマングルされました:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int cost = 1;
int total = 0;
int i = 0;
const int MAXNUM = 5;
int prices[MAXNUM];
while (total < 1000 && cost !=0 && i < MAXNUM)
{
for (int x = 0; i < MAXNUM; i++)
{
cout << "Please enter the price of the item you are purchasing:"<< endl << i+1 << "." <<"$";
cin >> cost;
total+=cost;
}
// total += cost;
}
/*
// cout << \n << "Name: ";
// cin >> name;
} */
cout << "You spent $" << total << " by purchasing " << /* **************** << */ " items, you have $" << 1000-total <</* " leftover, and your average price was $" << total/ ******** << */ "." << endl;
return 0;
}
だから私は、最初からやり直すとこれまでのところ、私はこれを持っている:今
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
const int NUMELS = 5;
int i, prices[NUMELS];
string items [NUMELS];
for (i = 0; i < NUMELS; i++)
{
cout << "Enter the name of the product: ";
cin >> items[i];
cout << "Enter the price of the product: $";
cin >> prices[i];
}
cout << endl;
for (i = 0; i < NUMELS; i++)
cout << items[i]<< " cost you $"<< prices[i]<< endl;
return 0;
}
私の最大の困難は、持ってしようとしていますプログラムのチェックをして、$ 1000(私は過去にプログラムを書いています)を超えないようにしてください。また、ユーザーが5つ以上のアイテムを購入しないようにチェックしています。この後にはさらに多くのステップがありますが、ベースを見つけたら自分で残りを作ることができるはずです。
もし(acc> 1000)を入れようとしましたが、 が壊れました。私のforループの最後に、しかしその後this happened...
ありがとうございます!
はSO割り当て質問が好きではありません試してみてください。 –
あなたは質問しませんでした。また、問題がある唯一のヒントとしてリンクも好きではありません。 – user463035818
'acc> 1000'の場合、出力は' NUMELS'までループしません。 – drescherjm