can someone give me an example of c++ that combining array,pointer,looping,selection ,structure,function and program menu.
i just wanna know is it possible
yes it is possible.
You could ask the user to input a choice say 1 for something which uses arrays 2 for something that uses pointer and so on.
Then use 'if else' to run according to user input.
i try combining array,pointer,selection,structure,function and program menu .but the problem is in didnt run as i want,it doesnt loop and i want 5 participant as an array butt its run non stop,the '{' always error
#include<iostream>
using namespace std;
int sumBMI(int weight , double height);//function
int main()
{
float height;
int weight;
float bmi;
int gender;
char Quit;
int participants[5];/// array
cout<<"Choose your gender :"<<endl;
cout<<"1.male :"<<endl;
cout<<"2.female :"<<endl;
cout<<"choose your operation[1..2]="<<endl;
cin>>gender;
switch(gender)
{
case 1:
cout<<"Enter your weight(kg)\n";
cin>>weight;
cout<<"Enter your height(m) \n";
cin>>height;
bmi = sumBMI( weight , height);
if (bmi<20.7)
cout << "Underweight && High-Less nutrition ";
else if (bmi>=20.7 && bmi<=26.4)
cout << "Normal && Low";
else if (bmi>=26.4 && bmi<=27.8)
cout << "Marginally overweight && Moderate";
else if (bmi>=27.8 && bmi<=31.1)
cout << "Overweight && high";
else
cout << "Obese && Very High";
break;
case 2:
cout<<"Please enter your weight(in kg):\n";
cin>>weight;
cout<<"Please enter your height(in meters):\n";
cin>>height;
bmi = sumBMI( weight , height);
cout<<"Your BMI is "<<bmi<<"\n";
if (bmi<19.1)
cout<<"You are UNDERWEIGHT and Your co-morbidities risk is HIGH-LESS NUTRITION";
else if ((bmi>=19.1)&&(bmi<=25.8))
cout<<"You are NORMAL and Your co-morbidities risk is LOW";
else if ((bmi>=25.8)&&(bmi<=27.3))
cout<<"You are MARGINALLY OVERWEIGHT and Your co-morbidities risk is MODERATE";
else if ((bmi>=27.3)&&(bmi<=32.3))
cout<<"You are OVERHEIGHT and Your co-morbidities risk is HIGH";
if (bmi>32.3)
cout<<"You are OBESE and Your co-morbidities risk is VERY HIGH";
break;
default:
cout<<"Your are alien";
break;
}
return 0;
}
int sumBMI (int weight , double height)
{
int bmi;
bmi = weight/(height*height) ;
cout<<"Your BMI is : "<<bmi<<endl;
I have not tested you program yet. These are just things I noticed.
doubles are preferred over floats. Better precision.
In main you define floats and ints. "gender" is OK as an int for the switch everything else I would type as a double. It makes everything more understandable and easier when doing calculations.
You ask for weight in kilos and height in meters. What happens when the user is not familiar with the metric system and has to go outside the program to convert from English to Metric. Maybe you could account for that in the program and add a function to convert.
The switch/case has a section for male and female, but each uses the same "bmi" function. If I remember correctly there is a different formula for males and females.
Overall the program looks like it should work. I will know more when I load it up and can test it.
The switch uses an int as a means to pick the case statements. Since a char is a type of int all you need is '1' or the ASCII equivalent 49.
A string might work, something I have not tried, but you would have to write the switch condition as switch (str[0]) to just use one character of the string.
Been playing around with your program and so far it has worked.
One last thing.
PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/ http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.
Very true. But as I tested it switch (str[0]) is a single character and it does work as I thought. It does make extra work deciding which charcter of a string to use.
We can switch on integral values, enumeration values and on values of any class type where a contextual conversion to an integral or enumeration type is available.
For a case label, any expression which yields a converted constant expression of the appropriate integer or enumeration type can be used.
#include <iostream>
struct A
{
int x ;
int y ;
constexpr A( int x, int y ) : x(x), y(y) {}
constexproperatorchar() const { return x+y ; }
};
struct B
{
int x ;
int y ;
B( int x, int y ) : x(x), y(y) {}
operatorint() volatile { return x += y ; }
};
enum C { D, E, F };
int main()
{
volatile B b( 10, 20 ) ;
switch(b) // contextually converted to int
{
// the label expression yields a converted constant expression of type int
case A(15,15) : std::cout << "ok!\n" ; break ;
case 34 : std::cout << "also ok\n" ; break ;
// the label expression yields a converted constant expression of type int
case E : std::cout << "also ok\n" ; break ;
default : ;
}
}