PDA

View Full Version : [C++]Esercizio semplice con switch


Krat0s
31-08-2009, 14:28
'giorno a tutti....
ho trovato dei problemi per implementare un mini-programma che, dati due numeri e il segno, da la soluzione. So che č semplice ma ho un problema con l'uso di switch. Ecco il codice:

#include <iostream>
using namespace std;

int main ()
{
float a,b,temp;
char segno;
cout << "Inserire i due numeri: ";
cin >> a >> b;
cout << "Inserire il segno: ";
cin >> segno;
switch (segno) {
case +:
temp = a+b;
cout << "Num. 1 + Num.2 = " << temp;
break;
case -:
temp = a-b;
cout << "Num. 1 - Num.2 = " << temp;
break;
case *:
temp = a*b;
cout << "Num. 1 * Num.2 = " << temp;
break;
case /:
temp = a/b;
cout << "Num. 1 / Num.2 = " << temp;
break;
}
system("Pause");
return 0;
}

Mi vengono dati i seguenti errori:
Compiling: C:\Documents and Settings\Kratos\Desktop\Untitled1.c
C:\Documents and Settings\Kratos\Desktop\Untitled1.c: In function `int main()':
C:\Documents and Settings\Kratos\Desktop\Untitled1.c:13: error: expected primary-expression before ':' token
C:\Documents and Settings\Kratos\Desktop\Untitled1.c:17: error: expected primary-expression before ':' token
C:\Documents and Settings\Kratos\Desktop\Untitled1.c:21: error: expected primary-expression before ':' token
C:\Documents and Settings\Kratos\Desktop\Untitled1.c:21: error: `*' cannot appear in a constant-expression
C:\Documents and Settings\Kratos\Desktop\Untitled1.c:25: error: expected primary-expression before '/' token
C:\Documents and Settings\Kratos\Desktop\Untitled1.c:25: error: expected primary-expression before ':' token
Process terminated with status 1 (0 minutes, 0 seconds)
6 errors, 0 warnings

Qualcuno puņ dirmi dove sbaglio? grazie

xblitz
31-08-2009, 14:35
i segni delle 4 operazioni devono essere messi tra apici (carattere) altrimenti per il compilatore sono operatori matematici e quindi ti da errore.

!@ndre@!
31-08-2009, 14:38
Se non ricordo male metti tra parentesi il valore dopo l'istruzione case in questo modo:

case ('+'):
temp = a+b;
cout << "Num. 1 + Num.2 = " << temp;
break;