2010-12-21 30 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

這縮進風格激怒。 – sbi 2010-12-21 11:05:36

回答

5

添加更多<<

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

編輯。早些時候聲明你的字符串。

+0

+ 1用於分割線條很好:) – daramarak 2010-12-21 10:45:26

+0

我仍然得到所有我的penn,五,十,二十和五十變量的錯誤 – 2010-12-21 10:49:53

+0

已更新的答案。 – x13n 2010-12-21 10:53:25

3

您錯過了字符串和數字之間的<<。它應顯示爲:

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

這是我做的第一件事,而且我在 – 2010-12-21 10:47:27

+0

@Josh之前得到了更多的錯誤,錯誤更少並不意味着你修好了一些東西...... – st0le 2010-12-21 10:53:11

5

您錯過了變量之間的<<

嘗試:

COUT < <說< <便士< <佩恩< < 「\ n」 < <說< <尼克斯< < 「\ n」 < <說< <硬幣< < 「\ n」 < <說<4分之 <二萬< < 「\ n」 < <說< < half_dollars < < < 「\ n」;

更新:

你的一些變量的範圍,因爲這樣penn,意味着他們不能在COUT聲明中可以看出。

您需要在if/else語句之外聲明變量。

此外,正如@Color Bend所述,您在keep_window_open()函數後缺少分號。

+0

這是我做的第一件事,而我之前實際上得到了更多的錯誤。 – 2010-12-21 10:47:09

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."); 
相關問題