2016-05-10 27 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.如何解決這個字符數組水溼停止打印

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

請發表更詳細 – Smeeheey

+1

請張貼在releavant代碼,預期產出和觀察到的輸出,suspection(從調試)**文本**格式。 –

回答

0

我不知道你的意圖,但...我看到森那東西,是很奇怪的。

(1)在testing()中,您定義了變量i,但您不會爲其指定任何值;這樣,當分配

material[i] = "Cooper"; 

i的值是未定義的

(2)在printarray(),你的material定義是

float material[i], price; 

其中i是所述函數的參數(與未定義的值,如果從testing()收到);這不是標準的C++,因爲你不能定義一個C數組(但你的意圖是什麼?)有沒有知名的在編譯時間維度

(3)printarray()定義materialfloat陣列大小爲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風格的字符串( char *),值爲「Cooper」,一些垃圾(未初始化的值)和空字符串;根據您向我們顯示的代碼,material應該是float的數組。我推斷你向我們展示的圖像是由不同的代碼生成的。請向我們展示corrispondent(和完整)代碼

(5)我的英語不好對不起

+0

是好的.. 謝謝你的建議。 我已經顯示我的完整代碼。 – Lun