2014-11-01 36 views
3

試圖編寫一個程序來計算盒子的體積,表面積或「周長」。我希望能夠爲每個主題輸入一個字母,然後在計算該主題後,爲下一個主題輸入一個字母。該程序還希望在鍵入Q時結束。我相信我的數學是正確的,但這個計劃對我來說並不合適。它編譯得很好,但卷不會計算,但其他兩個主題將會。我還得到了三行答案,當我輸入第二個字母作爲第二個值時,程序瘋狂。任何和所有的幫助,非常感謝。謝謝你的時間。while循環內部的開關語句C++

include <iostream> 
include <iomanip> 

using namespace std; 

const char SENTINEL = 'Q';//found this suggestion online to quit a program 

int main() 
{ 

char V, A, G, inputChar; 
float length, width, depth, totalV, totalA, totalG; 

cout<<"Enter character V for Volume, character A for Surface Area,\n"; 
cout<<"character G or Girth plus Depth or Q to quit\n"<<endl; 
cin>>inputChar;// pick letter 
while (inputChar!=SENTINEL)// if not Q then check the if-ele statments 
{ 
     switch (inputChar) 
     case 'V': 
     { 
       cout<<"Enter Length, Width, and Depth for Volume\n"; 
       cin>>length, width, depth; 
       totalV=length*width*depth;//math for volume 
       cout<<"The Volume = "<<totalV<<endl; 
       break; 
     } 
     case 'A': 
     { 
       cout<<"Enter Length, Width, and Depth for Surface Area\n"; 
       cin>>length, width, depth; 
       totalA=2*length*width+2*width*depth+2*length*depth;//math for area 
       cout<<"The Surface Area = "<<totalA<<endl; 
       break; 
     } 
     case 'G': 
     { 
       cout<<"Enter Length, Width, and Depth for Girth\n"; 
       cin>>length, width, depth; 
       totalG=2*(length+width)+depth;//math for girth 
       cout<<"The Girth = "<<totalG<<endl; 
       break; 
     }   
} 
     system ("pause"); 
     return 0; 
} 

回答

7

加上艾略特的回答,我發現你的程序有一些改進,沒有它我不認爲它會編譯。像包含語句中的'#'和錯誤的開關塊一樣。即使輸入多個值,我們也需要級聯而不是逗號。

這裏是將編譯代碼:

#include <iostream> 
#include <iomanip> 

using namespace std; 

const char SENTINEL = 'Q';//found this suggestion online to quit a program 

int main() 
{ 

char V, A, G, inputChar; 
float length, width, depth, totalV, totalA, totalG; 

cout<<"Enter character V for Volume, character A for Surface Area,\n"; 
cout<<"character G or Girth plus Depth or Q to quit\n"<<endl; 
cin>>inputChar;// pick letter 

while (inputChar!=SENTINEL)// if not Q then check the if-ele statments 
{ 
     switch (inputChar) 
     { 
     case 'V': 

       cout<<"Enter Length, Width, and Depth for Volume\n"; 
       cin>>length>>width>>depth; 
       totalV=length*width*depth;//math for volume 
       cout<<"The Volume = "<<totalV<<endl; 
       break; 

     case 'A': 

       cout<<"Enter Length, Width, and Depth for Surface Area\n"; 
       cin>>length>>width>>depth; 
       totalA=2*length*width+2*width*depth+2*length*depth;//math for area 
       cout<<"The Surface Area = "<<totalA<<endl; 
       break; 

     case 'G': 

       cout<<"Enter Length, Width, and Depth for Girth\n"; 
       cin>>length>>width>>depth; 
       totalG=2*(length+width)+depth;//math for girth 
       cout<<"The Girth = "<<totalG<<endl; 
       break; 
     } 

    cout<<"Enter character V for Volume, character A for Surface Area,\n"; 
    cout<<"character G or Girth plus Depth or Q to quit\n"<<endl; 
    cin>>inputChar;// pick letter   
} 
     return 0; 
} 
4

您從不更新您的inputChar。在你while循環結束讀取另一個char

cin>>inputChar; 
} 
system ("pause"); 

否則,它會進入一個死循環(可能與高的CPU使用率)時,字符不交換機的情況下或定點相匹配。

6

while (cin >> inputChar && inputChar != SENTINEL) 
{ 
//... 

,而不是

cin>>inputChar;// pick letter 
while (inputChar!=SENTINEL)// 

你也可以寫

auto prompt = [] 
{ 
    cout << "\nEnter character V for Volume, character A for Surface Area,\n"; 
    cout << "character G or Girth plus Depth or Q to quit\n"<<endl; 
}; 

while (prompt(), cin >> inputChar && inputChar != SENTINEL) 
{ 
//... 

或者

auto prompt = [] 
{ 
    cout << "\nEnter character V for Volume, character A for Surface Area,\n"; 
    cout << "character G or Girth plus Depth or Q to quit\n"<<endl; 
}; 

prompt(); 
while (cin >> inputChar && inputChar != SENTINEL) 
{ 
    switch (inputChar) 
    { 
    case 'V': 
    { 
    //... 
    } 

    //... 
    } 

    prompt(); 
} 
2

您需要再次提示用戶輸入數據,while循環中:

do 
{ 
    cout << "Enter character 'V' for Volume, character 'A' for Surface Area,\n"; 
    cout << "character 'G' for Girth plus Depth or 'Q' to quit\n"<<endl; 
    //Right here before the switch statement 
    cin >> inputChar; 
    switch (inputChar) 
    { 
     //I also recommend stacking your cases, for lower case and upper case support: 
     case 'V': 
     case 'v': 
     { 
      // 
     } 
     case 'A': 
     case 'a': 
     { 
      // 
     } 
     case 'G': 
     case 'g': 
     { 
      // 
     } 
     //Add a default case if they're not any of the above letters 
     default: 
     { 
      cout << inputChar << " is not valid input!"; 
     } 
    } 
} while (inputChar != SENTINEL); 
1

只需添加黴素>> inputChar; 在while循環結束時。而已。 剛剛編譯後,&運行。 你會得到預期的輸出。 :-)

-Jayesh