Problem with "switch"

I'm making a program with dev-c++ that needs "float" to declare and not "int" because it needs precision on numbers.(sorry for bad english i hope you'll understand). Here is the first part of the code:

#include <cstdlib>
#include <iostream>
#include <math.h>


using namespace std;

int main(int argc, char *argv[])
{
int p,cat1,cat2,i,i2,cat,catot;

system("color 1d");

cout <<"Choose an option:"<<endl;
cout <<"1) Moltiplication"<<endl;
cout <<"2) division"<<endl;
cin >> p;

switch (p)
{
case 1:


and then it continues.. but I need help on this part. Instead of "int p,cat1,cat2,i,i2,cat,catot;" i need "float p,cat1,cat2,i,i2,cat,catot;" but if i put "float" it tells me error: switch quantity not an integer.
what should i do? please hep me.
What do your cases do? We may be able to work around the switch case statement completley with a recursive function.
The cases are all right because if i use "int" the program works perfectly exept for one thing:

case 1:
it moltiplies an input number by another input number. If the number1 is 2 then the number2 is 3 it gives me result: 6.(and that's all right)
but if number1 is 2,5 (for example) it wont work because it needs "float" for numbers with ","
then when u use a double value which error u encountered? or it will just round up to the nearest value giving incorrect result?
if i use double the error is the same as float
I'm assuming comma "," really means decimal ".". Well you could just use a bunch of if then else statements but that's sloppy. I wanted a few case statements to see if we can find a common element among them and eliminate the switch case all together. Could you post like three of them?
will be helpful if u post the whole code here for me to see...Thanks :)
The cases of a switch statement must be integers or convertible to integers (chars). Using floats or doubles probably wouldn't work anyway, because of floating-point imprecision.
@Toshitaka: When you cast a double or a float to an int it should round down, in reality it should truncate (cut the number off) at the decimal point.

EDIT: I'm with filipe, which is why I think we need to work around the need for a switch case statement.
Last edited on
ok i'll post it in a sec but i advert you that it's written initalian. (not the code though)
here it is:

#include <cstdlib>
#include <iostream>
#include <math.h>


using namespace std;

int main(int argc, char *argv[])
{
int pitagora,cat1,cat2,i,i2,cat,catot;

system("color 1d");

cout <<"Scegliere una opzione:"<<endl;
cout <<"1) Teorema per calcolare ipotenusa"<<endl;
cout <<"2) Teorema per calcolare cateto"<<endl;
cin >> pitagora;

switch (pitagora)
{
case 1:
cout <<"Inserire il cateto1:"<<endl;
cin >> cat1;
cout <<"Inserire il cateto2:"<<endl;
cin >> cat2;

i=sqrt(cat1*cat1+cat2*cat2);

cout <<"L'ipotenusa misura: "<<i<<endl;
break;

case 2:
cout <<"Inserire l'ipotenusa:"<<endl;
cin >> i2;
cout <<"Inserire il cateto:"<<endl;
cin >> cat;

catot=sqrt(i2*i2-cat*cat);

cout <<"Il cateto misura: "<<catot<<endl;
break;

};


system("PAUSE");
return EXIT_SUCCESS;
}

I think you're trying to do too much at once. What happens if I want to subtract the product of my elements? But my first number is 2? You should prompt the user for a number first then use that in the switch case statement, THEN you prompt him for the data to be used in the mathmatical operations.
wow man :))) you have few errors... for ex. two cout's one cin ...
This code works...

to solve the prob

Declaration of variable:
int pitagora,cat1,cat2,i,i2,cat,catot;

Change it to:
int Pythagoras;
double cat1, cat2, i, i2, cat, catot;
@Janlan: I can't see that error, do you have a line number? Also any others you spot would help, I didn't copy and compile this yet.


By the way, Light Purple text on a Blue background? Holy hangover do you really hate your teacher that much?
Yay! Thanks alot Toshitaka you solved my prob! :)
Topic archived. No new replies allowed.