question with if-else statement

i am a C++ beginner and stuck with the following problem.What's wrong with my code and how can i write the same thing by switch statement?


#include<iostream>
#include <math.h>
main ()
{
using std::cout;
using std::cin;
float number;
cout<<"Type a number \n";
cin>>number;

if (number<-1,number=-1)
number=number^2+4;
else if (number>-1,number<=1)
number=number*2=1;
else
number=number*3;
}
Please please use the code tags.

What exactly do you want this program to do? I am a little confused. Tell me want you want it to do and I will help you make it do it. :)
Last edited on
#include <iostream>
#include <conio>
int main()
{
float number;
cout<<"Please Enter a number\n";
cin>>number;

if(number<=-1)
number=(number*number+4);

else if (number>=1)
number=(number*2+1); // I am assuming your = sign was +

else
number=(number*3);

cout<<"\nYour number after calculation is: "<<number;
getche();
}

Last edited on
Think of switch as a multiple choice decision, and an if statement as a true/false.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
switch(myVar){ // Test myVar as the decision making variable
    case 1: // If myVar is 1, then do this
        ...
        ...
        break; // Always break after a case, this will kick you out of the switch
    case 2: // If myVar is 2, then do this
        ...
        ...
        break;
    default: // Default is what happens if myVar isn't the same as anything selected by case
        ...
        ...
        break;
}
Thank you very much!!
Topic archived. No new replies allowed.