just a doubt guys but pls answer.

i am actually unable to understand if it's possible or not, to have conditional statements where there are three or more possible answers . And if so can it be done by using "if"and"else"?


Last edited on
answer guys i am a bit confused here
closed account (SECMoG1T)
Yes, that what they do.

example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int choice;

//get choice from user
std::cout<<"enter your choice ";
std::cin>>choice;

if(choice == 1)
//do something

else if(choice  == 2)
//do something else

else if(choice == 3)
//do something else

else
//no choice was correct do something else 


you could also use a switch statement in the example above instead of typing multiple if\else
Last edited on
SOS pls help
conditional statements where there are three or more possible answers

Try switch. See last chapter in http://www.cplusplus.com/doc/tutorial/control/
Alas, it is not so versatile as select in REXX.
which compiler did you use for that example ?DEV C++?
That example of if else is just a couple statements. Not a complete program that one could compile. Therefore, no compiler has been used.

The syntax of the statements is valid on all modern compilers. The namespaces were described already 1990, but early compiler implementations did not have C++ Standard Library properly in namespace std. If your compiler does not accept std::cout, then update.
closed account (SECMoG1T)
As Keskiverto mentioned the statements weren't compiled and were just meant to help you picture a scenario on usage, below I have added two more examples although compilable they achieve nothing interesting, i hope they will help you get a clearer picture.

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
///Example 1: if\else

#include <iostream>

int main()
{
    std::cout<<" 1. play\n 2. save\n 3. restore\n 4. exit\n\n";
    int choice;

    //get choice from user
    std::cout<<"enter your choice ";
    std::cin>>choice;
    
    if(choice == 1)
      std::cout<<" started playing...\n";
     //your code
    
    else if(choice  == 2)
      std::cout<<"saving progress\n";
    //state saved
    
    else if(choice == 3)
      std::cout<<"restoring previous state\n";
    //your code here
    
    else if(choice == 4)
       std::cout<<"exiting\n";

    else
      std::cout<<"unknown choice, retry";
  
   return 0;
}



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
///Example 2: switch

#include <iostream>

int main()
{
    std::cout<<" 1. play\n 2. save\n 3. restore\n 4. exit\n\n";
    int choice;
   

    //get choice from user
    std::cout<<"enter your choice ";
    std::cin>>choice;

    switch(choice)
    {
        case 1:  std::cout<<" started playing...\n";  /*your code here*/  break;
        case 2:  std::cout<<"saving progress\n";  /*your code here*/ break;
        case 3:  std::cout<<"restoring previous state\n";  /*your code here*/ break;
        case 4:  std::cout<<"exiting\n";  /*your code here*/ break;
        default: std::cout<<"unknown choice, retry";  break;
    }
  
   return 0;
}


As for the compiler, use any up to date c++ compiler on all your projects, some options might include GCC, LLVM Clang, Microsoft vs compiler, .....

As for the Dev c++ their last known update was in 2015 so i wouldn't recommend if you are into c++ 17
Last edited on
thnx misters i have another problem check in the recent topics.
since you are new, know that switch only works on integer family types in c++.


It also works with (scoped) enumeration types and class types which are contextually convertible to an integral or enumeration type.

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
#include <iostream>

struct A
{
    constexpr A( int x, int y = 0 ) : x(x), y(y) {}

    constexpr operator long long() const { return (long long)x + y ; }

    int x ;
    int y ;
};

int main()
{
    int a, b ;
    std::cin >> a >> b ;
    A cond(a,b) ;

    constexpr A case_label(2) ;

    switch(cond)
    {
        case case_label : std::cout << "A(2)\n" ; break ;
        case A(5,6) : std::cout << "A(5,6)\n" ; break ;
        default: std::cout << "default\n" ;
    }
}

http://coliru.stacked-crooked.com/a/3274d731992db74e
“misters”?


JSYK, in English that is a pretty gender-specific word, which would normally be passable in an online context, if only a little weird.

Except that your biggest help came from a feminine username... At which point it seems like you are being deliberately clueless or rude toward her after having already ignored her first very good answer...

For future reference, “sirs” works, but an informal “everyone” and some careful spelling does wonders:

Thanks everyone.


Hope this helps.
Oh my God I am sorry Miss,pls don't take this for rude talk ma'am forgive me .
Topic archived. No new replies allowed.