combining

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;

return bmi;
}
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/222263/
Hello amyblackpearl,

Nice name, I like it.

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.

BTW I like your default case.

Hope that helps,

Andy
hello andy thanks for your comment,

one thing im suspicious about my program is ,i hear if using case as a selection we can use char and sring is it true or i just misheard it
Hello amyblackpearl,

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.

Hope that helps,

Andy
You can only switch on an integral value - which includes char, but not string or char*.
@MikeyBoy,

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.

Andy
Yes, I was answering the OP's question, not commenting on your suggestion. The question was:

amyblackpearl wrote:
i hear if using case as a selection we can use char and sring is it true or i just misheard it
Last edited on
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>

struct A
{
    int x ;
    int y ;
    constexpr A( int x, int y ) : x(x), y(y) {}
    constexpr operator char() const { return x+y ; }
};

struct B
{
    int x ;
    int y ;
    B( int x, int y ) : x(x), y(y) {}
    operator int() 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 : ;
    }
}

http://coliru.stacked-crooked.com/a/962699ebb48894f5
Topic archived. No new replies allowed.