2016-05-10 7 views
-8

Attached ImageSomeone Please help me fix the this problem.I was using the if statement to determine the material is copper, and then I would like to use arrays to print out different type of length and material. Thank You.このchar配列が

enter code here 
#include <stdlib.h> 
#include <iostream> 
using namespace std; 
float steelbar(); 
float steelrod(); 
char printMenu(); 
void testing(float length, float area, float stress, float price); 
int main() 
{ 
char Choice; 
float result; 
cout<<"\t\tWelcome to Smart Calculating System\n"; 
cout<<"\t\t_______________________________________\n"; 
Choice = printMenu(); 
if (Choice == 'A' || Choice == 'a') 
{ 
result = steelbar(); 
} 
else if (Choice == 'B' || Choice == 'b') 
{ 
result = steelrod(); 
} 
else 
{ 
printf("Invalid input!\n"); 
fflush(stdin); 
getchar(); 
} 
} 
char printMenu() 
{ 
char choice; 
cout<<" Please choose a type:\n"; 
cout<<" A/a = Steel bar B/b = Steel rod\n"; 
cout<<"Answer = "<<endl; 
cin>>choice; 
cout<<"\n"; 
return choice; 
} 
///////////////////////////////////////////////////////////////// 
float steelbar() 
{ 
float length, width, height, pressure, area, stress, price; 
int program=1; 
while(program == 1) 
{ 
cout<<"Please enter length: "<<endl; 
cin>>length; 
cout<<"Please enter width: "<<endl; 
cin>>width; 
cout<<"Please enter height: "<<endl; 
cin>>height; 
cout<<"Please enter pressure: "<<endl; 
cin>>pressure; 
area = height * width; 
stress = pressure/area; 

cout<<"\nThe stress is : "<<stress<<endl; 

if (stress <= 690) 
{ 
testing(length, area, stress, price); 
fflush(stdin); 
getchar(); 

cout<<"\n"; 
cout<<" Do you wish to continue? (Yes=1 /No=0)\n"; 
cout<<"Answer = "; 
cin>>program; 
if (program == 1) 
{ 
    printMenu(); 
} 
else (program == 0); 
{ 
    return 0; 
} 
} 
else if (stress > 690) 
{ 
cout<<"\n"; 
cout<<" Please enter stress less than 690 MPA\n"; 
cout<<" Do you wish to continue? (Yes=1 /No=0)\n"; 
cout<<"Answer = "<<endl; 
system("cls"); 
}} 
return price; 
} 
///////////////////////////////////////////////////////////////// 
float steelrod () 
{ 
float length, diameter, pressure, area, stress, price ,density, rate ; 
int program; 
while(program == 1) 
{ cout<<"Please enter length: "<<endl; 
cin>>length; 
cout<<"Please enter diameter: "<<endl; 
cin>>diameter; 
cout<<"Please enter pressure: "<<endl; 
cin>>pressure; 
area = (3.14159 * diameter * diameter)/4; 
stress = pressure/area; 
cout<<" The stress is : "<<stress;if (stress <= 690) 
{ 
testing(length, area, stress,price); 
fflush(stdin); 
getchar(); 
system("cls"); 
cout<<"\n"; 
cout<<" Do you wish to continue? (Yes=1 /No=0)\n"; 
cout<<"Answer = "; 
cin>>program; 
if (program == 1) 
{ system("cls"); 
    main(); 
} 
else (program == 0); 
{return 0; 
} 
} 
else if (stress > 690) 
{ 
cout<<"\n"; 
cout<<" Please enter stress less than 690 MPA\n"; 
cout<<" Do you wish to continue? (Yes=1 /No=0)\n"; 
cout<<"Answer = "<<endl; 
cin>>program; 
system("cls"); 
} 
return price; 
} 
} 
///////////////////////////////////////////////////////////////// 
void testing(float length, float area, float stress, float price) 
{ 
const char *material[30]; 
int i, j; 

if(stress <= 70) 
{ 
material[i] = "Copper"; 
price = length * area * 10 * 1.5; 
} 
else if(stress >70 && stress <= 130) 
{ 
material[i] = "Cast iron"; 
price = length * area * 15 * 1.8; 
} 
else if(stress >130 && stress <= 200) 
{ 
material[i] = "Brass"; 
price = length * area * 17 * 2.0; 
} 

else if(stress >200 && stress <= 690) 
{ 
material[i] = "ASTM A51"; 
price = length * area * 22.5 * 2.20; 
} 
for(i=0;i<3;i++) 
{ 
cout<<"Material : "<<material[i]; 
cout<<"\nThe total price is : RM"<<price<<endl; 
} 
} 
+2

でreleavantコード、予想される出力と観測出力、(デバッグから)suspectionを投稿してください詳細 – Smeeheey

+1

を投稿してください**テキスト**形式。 –

答えて

0

の印刷を停止望めない修正するためにどのように私はあなたの意図についてはよく分からないけど...私は非常に奇妙ですサイム物事を参照してください。

(1)testing()の場合、変数iを定義しますが、値は割り当てられません。したがって、ときにiの値が未定義である

material[i] = "Cooper"; 

を割り当てる

(2)printarray()に、materialのあなたの定義は(とiが関数のパラメータである

float material[i], price; 

あります未定義値、testing()から受信した場合)。これは、標準のCではありません++あなたはCスタイルの配列を定義することはできませんので(ただし、あなたの意図だった?)コンパイル時間ディメンションで知られていないと

(3)あなたがfloat配列としてmaterialを定義printarray()にサイズがiで、iの値は初期化されません。したがってmaterialキャリーi未定義値;あなたは以下のfor

for(i=0;i<3;i++) 
{ 
cout<<"Material : "<<material[i]; 
cout<<"\nThe total price is : RM"<<price<<endl; 
} 

でそれをアセスするとき、あなたは3つの未定義の値にアクセス

(4)あなたが私たちを報告した画像に従って、

cout<<"Material : "<<material[i]; 

であなたは(Cスタイルの文字列を印刷)に値「Cooper」、いくつかのガベージ(初期化されていない値)、空の文字列materialのコードによれば、floatの配列にする必要があります。あなたが私たちに示すイメージは、別のコードによって生成されると推論します。 、私たちにcorrispondent(完全な)コードを表示してください

(5)私の悪い英語のため申し訳ありません

+0

は大丈夫です。 アドバイスありがとうございます。 私はすでに完全なコードを表示していました。 – Lun