2017-02-22 6 views
1

私はプログラミングに少し新しく、コード全体が一度に実行される理由を理解するのに問題があります。ユーザーに一度に1つのことを尋ねるようにするにはどうすればよいですか?私はそれがとてもシンプルなものだと確信していますが、私はそれを忘れていたに違いありません。ありがとう。プログラムはすべての行を一度に実行します

#include<iostream> 
using namespace std; 

int main() 
{ 

    int length; 
    int width; 
    int height; 
    int numberCoats; 
    int squareFeet; 
    int name; 
    int paintNeeded; 
    int brushesNeeded; 
    int coatsPaint; 
    int gallonsPaint; 

    cout << "Welcome to the program! What is your first name? \n"; 
    cin >> name; 


    cout << name << " What is the length of the house?"; 
    cin >> length; 


    cout << name << " What is the width of the house?"; 
    cin >> width; 


    cout << name << " What is the height of the house?"; 
    cin >> height; 

    cout << name << " How many coats of paint will it need?"; 
    cin >> coatsPaint; 


    squareFeet = length * width * height; 
    paintNeeded = squareFeet/325; 
    brushesNeeded = squareFeet/1100; 
    gallonsPaint = coatsPaint * paintNeeded; 

    cout << name << " , the amount of square feet is " << squareFeet << endl; 
    cout << name << " , the amount of coats of paint you will need is " << coatsPaint << endl; 
    cout << name << " , you will need " << gallonsPaint << " of paint" << endl; 
    cout << name << " , you will need " << brushesNeeded << " of brushes" << endl; 

        system("pause"); 
        return 0; 
} 
+0

'name'は' int'です。それは正しいとは言えません。 'name'の文字列を入力していますか? –

+0

ああ良いキャッチ、あなたは正しいです私はすぐにそれを修正するでしょう – Chad

+0

また、文字列を使用する場合、あなたのヘッダに '#include 'が必要です – Rime

答えて

0

あなたの名前として(例えば)Chadを入力するとnameは整数型ではなく、文字列型であるので、cin >> nameを失敗します。

これは、Chadが入力ストリームに残り、すべての他のすべての文も(整数型であるため)失敗することを意味します。 :-)

より良い解決策はstd::stringおよび使用にnameを変更することで、あなたの名前ではありません事実以外の

あなたが7として自分の名前を入力した場合、あなたはそれだけで正常に動作見つけることができますが、前者は全体のラインを取得するのに対し、後者は空白に停止しますので、むしろその後、cin >>getline()を使用する理由がある

#include <string> 

std::string name; 

getline(cin, name); 

:でそれを読むことgetline()。言い換えれば

Chad Morganを入力すると、まだそれはあなたの名前としてChadを受け入れ、youer家の長としてMorganを取得しようとしますので、あなたが現在見ている問題があります。

+0

詳細な説明をいただきありがとうございます。ほんとうにありがとう。このプログラムは今の魅力のように機能します。 getlineは理にかなっている、私はそれを使い始めるだろうし、多分それはこのような愚かな間違いをカバーするだろう:)。 – Chad

+0

@Chadは、 'cin :: somestring'より' getline(cin、somestring) 'を好む理由で拡張しました。 – paxdiablo

+0

それを拡張していただきありがとうございます。 – Chad

関連する問題