array in case structure

Can i have an array value in a case structure ? The program repeat storing 10 calculating value's to be displayed how will the array be intialized in the case structure? here is the basic layout

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
// float value [];//


do {    

              // program to be repeated //

          case :1 {
                   //calculation//          
                   }



} while (condition) ;
Yes and no. You most certainly can use the value of a certain object in an array in a switch statement.

1
2
3
4
5
6
7
8
9
10
int intArray[2] = {5, 1}; 

switch(intArray[0])
{
    case 5:
        cout << "intArray[0] equals 5" << endl;
        break;
    default:
        break;
}


However, you should never use a floating point variable in an equality comparison. Switch statements can only be used to compare constants, and floats vary depending on how they're calculated and where they're truncated, so it's not really viable to use switch statements in the case of floating point variables. Unless you're doing something like:
1
2
3
4
5
6
7
8
9
10
11
float x = 7.2539;

switch(x > 5)
{
    case true:
        cout << "x > 5" << endl;
        break;
    case false:
        cout << "x < 5" << endl;
        break;
}


What exactly are you trying to do anyway?



1.Basically i have a program that does a specific calculation(totalwin) for a bet amount based on 3 choices(case)

2.The variable totalwin is displayed based on the choice made(case).. the user is asked to repeat or not
total win calculation is in a case structure


3.Totalwin originally is not an array but needs to be so 10 values can be stored displayed at different locations when the user stops the program.

Last edited on
any advice ?
Topic archived. No new replies allowed.