2010-12-21 13 views
2

私は最後にcoutにエラーが表示される理由を教えていただけますか?期待された `; ' 「ペニー」の前に? C++のデバッグ(コード完了)

#include <iostream> 
#include <cmath> 
#include <stdio.h> 
#include <cstring> 
#include <conio.h> 

using namespace std; 
inline void keep_window_open() { char ch; cin>>ch; } 

int main() 
{ 
cout << "How many pennies do you have?\n"; 
int pennies; 
cin >> pennies; 
double total_pen; 
total_pen = (0.01 * pennies); 
      if (pennies >= 1) 
      { 
         string penn = " pennies."; 
      }else 
      { 
         string penn = " penny."; 
} cout << "How many nickles do you have?\n"; 
int nickles; 
cin >> nickles; 
double total_nic; 
total_nic = (0.05 * nickles); 
      if (nickles >= 1) 
      { 
         string five = " nickels."; 
      }else 
      { 
         string five = " nickel."; 
} cout << "How many dimes do you have?\n"; 
int dimes; 
cin >> dimes; 
double total_dim; 
total_dim = (0.10 * dimes); 
      if (dimes >= 1) 
      { 
        string ten = " dimes."; 
      }else 
      { 
        string ten = " dime."; 
} cout << "How many quarters do you have?\n"; 
int quarters; 
cin >> quarters; 
double total_qua; 
total_qua = (0.25 * quarters); 
      if (quarters >= 1) 
      { 
         string twentyfive = " quarters."; 
      }else 
      { 
         string twentyfive = " quarter."; 
} cout << "How many half-dollars do you have?\n";    
int half_dollars; 
cin >> half_dollars; 
double total_dol; 
total_dol = (0.50 * half_dollars); 
      if (half_dollars >= 1) 
      { 
         string fifty = " half dollars."; 
      }else 
      { 
         string fifty = " half dollar."; 
      } 
string saying = "You have "; 
cout << saying pennies penn << "\n" << saying nickles five << "\n" << saying dimes ten << "\n" << saying quarters twentyfive << "\n" << saying half_dollars fifty << "\n"; 
keep_window_open() 
return 0; 
} 
+0

インデントスタイルがenragingされていること。 – sbi

答えて

5

<<以上を追加します。

cout << saying << pennies << penn << "\n" 
    << saying << nickles << five << "\n" 
    << saying << dimes << ten << "\n" 
    << saying << quarters << twentyfive << "\n" 
    << saying << half_dollars << fifty << "\n"; 

はEDIT:また、あなたが内部ブロックで変数を宣言されていない - 彼らの名前が外もはや有効です。以前にあなたの文字列を宣言してください。

+0

+1をうまく分割するために:) – daramarak

+0

私のpenn、five、ten、twenty、およびfiftyの変数すべてにエラーがまだ残っています –

+0

答えが更新されました。 – x13n

3

文字列と数字の間に<<がありません。それは読む必要があります:

cout << saying << pennies << penn << ... 
+0

これは私がやった最初のことでしたが、実際には前にもっとエラーがありました –

+0

@Josh、あなたが何かを修正したことを必要とするわけではありません... – st0le

5

変数間に<<がありません。

試してみてください。

< <ペニー< <ペン< < < <を言って "\ n" は< < < < nickles < < を言って "\ n" は< <を言ってcoutの< <ダイム< < "\ n" は < < < 4分の< < < twentyfive < < "\ n" は< <は "N \" < < half_dollars < < <を言って言って。

更新:、彼らはCOUTの声明で見ることができないことを意味

あなたの変数のいくつかの範囲は、そのようなpennとして。

if/else文の外で変数を宣言する必要があります。

また、@Color Bendに記載されているように、keep_window_open()関数の後にセミコロンがありません。

+0

これは私がやった最初のことでした。 –

2

keep_window_open()にはセミコロンがありません。

2

としてだけでなく、不足している<<、あなたのpennfivetentwentyfivefifty文字列変数を使用すると、そのCOUTに着くまでに、すべての範囲外です。

あなたはこれを行うことはできません:それはそれらの中括弧の範囲を離れるとき、文字列がもう存在しないとして

if (quarters >= 1) 
{ 
    string twentyfive = " quarters."; 
} 
else 
{ 
    string twentyfive = " quarter."; 
} 

を。 そのようにするには、変数を最初に中括弧の外側に宣言する必要があります。

string twentyfive = " quarter."; 
if (quarters >= 1) 
{ 
    twentyfive = " quarters."; 
} 

それともそれをneatenする三項演算子を使用します。

string twentyfive = (quarters > 1 ? " quarters." : " quarter."); 
関連する問題