如何修复这个char数组不能停止打印

时间:2016-05-10 08:09:08

标签: c++

Attached Image Someone 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; 
}
}

1 个答案:

答案 0 :(得分:0)

我不确定你的意图但是......我看到了很奇怪的事情。

(1)在testing()中,您定义了变量i,但您没有为其指定任何值;所以,当你指定

material[i] = "Cooper";

i的值未定义

(2)在printarray()中,您对material的定义是

float material[i], price;

其中i是函数的参数(如果从testing()收到,则具有未定义的值);这不是标准的C ++,因为你无法在编译时维度定义一个C风格的数组(但你的意图是什么?)

(3)在printarray()中,您将material定义为float大小为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];

使用值&#34; Cooper&#34;,一些垃圾(未初始化的值)和空字符串打印C风格的字符串(char *);根据您向我们展示的代码,material应该是float的数组。我推断您向我们展示的图像是由不同的代码生成的。请向我们展示相应的(和完整的)代码

(5)抱歉我的英语不好