Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

Problem z funkcją case

Ostatnio zmodyfikowano 2015-07-05 18:13
Autor Wiadomość
NKKS
Temat założony przez niniejszego użytkownika
Problem z funkcją case
» 2015-07-05 17:30:35
Napisałem kalkulator w konsoli niestety wyskakują mi błędy i nie wiem w czym problem. Dopiero się uczę. Z góry dziękuje za pomoc.
Błędy:
01.cpp:78: error: case label `1' not within a switch statement
01.cpp:85: error: case label `2' not within a switch statement
01.cpp:91: error: case label `3' not within a switch statement
01.cpp:98: error: case label `4' not within a switch statement
01.cpp:106: error: case label `5' not within a switch statement
01.cpp:114: error: case label `6' not within a switch statement
01.cpp:122: error: case label `7' not within a switch statement
01.cpp:130: error: case label `8' not within a switch statement
01.cpp:145: error: case label `9' not within a switch statement
01.cpp:148: error: case label not within a switch statement
Kod:
C/C++
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <fstream>


float bokkw, przekatnakw, bokpr1, bokpr2, podstawatr1, podstawatr2, wysokosctr, d1, d2, o1, o2, m1, m2, dz1, dz2;

float obwod_kwadratu( float x )
{
    return x * 4;
}
float pole_kwadratu( float x )
{
    return x * x;
}
float pole_kwadratuprze( float x )
{
    return x * x / 2;
}
float obwod_prostokata( float x, float y )
{
    return x * 2 + y * 2;
}
float pole_prostokata( float x, float y )
{
    return x * y;
}
float pole_trapezu( float h, float a, float b )
{
    return a + b / 2 * h;
}
float dodawanie( float a, float b )
{
    return a + b;
}
float odejmowanie( float a, float b )
{
    return a - b;
}
float mnozenie( float a, float b )
{
    return a * b;
}
float dzielenie( float a, float b )
{
    return a / b;
}



int main()
{
    for(;; )
    {
        printf( "\nMENU GLOWNE \n" );
       
        printf( "\n-------------------------------------------\n" );
       
        printf( "1.Pole i obwod kwadrata z uzyciem bokuw\n" );
        printf( "2.Pole kwadrata z uzyciem przekatnej \n" );
        printf( "3.Pole i obwod prostokata z uzyciem bokuw\n" );
        printf( "4.Pole trapezu z uzyciem podstaw i wysokosci\n" );
        printf( "5.Dodawanie 2 liczb\n" );
        printf( "6.Odejmowanie 2 liczb\n" );
        printf( "7.Mnozenie 2 liczb\n" );
        printf( "8.Dzielenie 2 liczb\n" );
        printf( "9.Wyjscie\n" );
       
        ; printf( "\n-------------------------------------------\n" );
        char opcja;
        printf( "wybierz opcje " );
        scanf( "%c", opcja );
       
       
        switch( opcja );
        {
        case 1:
            printf( "\nPodaj dlugosc boku kwadratu " );
            scanf( "%s", bokkw );
            float isamu = obwod_kwadratu( bokkw );
            float polekw = pole_kwadratu( bokkw );
            printf( "kwadrat o boku %2.f ma obwod %2.f a pole %2.f\n", bokkw, isamu, polekw );
            break;
        case 2:
            printf( "\npodaj dlugosc przekatnej kwadratu " );
            scanf( "%2.f\n", przekatnakw );
            float pole = pole_kwadratuprze( przekatnakw );
            printf( "kwadrat o przekatnej %2.f ma pole %2.f\n", przekatnakw, pole );
            break;
        case 3:
            printf( "\npodaj boki prostokata  " );
            scanf( "%2.f %2.f\n", bokpr1, bokpr2 );
            float obwodpr = obwod_prostokata( bokpr1, bokpr2 );
            float polepr = pole_prostokata( bokpr1, bokpr2 );
            printf( "Prostokat o bokach %2.f %2.f ma pole %2.f o obwod %2.f\n", bokpr1, bokpr2, polepr, obwodpr );
            break;
        case 4:
            printf( "\nPodaj wysokość trapezu " );
            scanf( "%2.f", wysokosctr );
            printf( "podaj dlugosc podstaw trapezu  " );
            scanf( "%2.f %2.f\n", podstawatr1, podstawatr1 );
            float poletrapezu = pole_trapezu( wysokosctr, podstawatr1, podstawatr2 );
            printf( "trapez ma pole %2.f\n", poletrapezu );
            break;
        case 5:
            printf( "\nPodaj 1 liczbe " );
            scanf( "%2.f\n", d1 );
            printf( "podaj 2 liczbe " );
            scanf( "%2.f\n", d2 );
            float dodawaniee = dodawanie( d1, d2 );
            printf( "wynik %2.f\n", dodawaniee );
            break;
        case 6:
            printf( "\nPodaj 1 liczbe " );
            scanf( "%2.f\n", o1 );
            printf( "podaj 2 liczbe " );
            scanf( "%2.f\n", o2 );
            float odejmowaniie = odejmowanie( o1, o2 );
            printf( "wynik %2.f\n", odejmowaniie );
            break;
        case 7:
            printf( "Podaj 1 liczbe " );
            scanf( "%2.f", m1 );
            printf( "podaj 2 liczbe " );
            scanf( "%2.f", m2 );
            float mmnozenie = mnozenie( m1, m2 );
            printf( "wynik %2.f", mmnozenie );
            break;
        case 8:
           
            printf( "Podaj 1 liczbe " );
            scanf( "%2.f", dz1 );
            printf( "podaj 2 liczbe " );
            scanf( "%2.f", dz2 );
           
           
            if(( dz1 != 0 ) ||( dz2 != 0 ) )
           
                 printf( "wynik %2.f", dzielenie( dz1, dz2 ) );
           
            else printf( "\nNie dzielimy przez 0\n" );
           
            break;
        case 9:
            exit( 0 );
            break;
            default:
            printf( "\nNiema takiej opci\n" );
            break;
        }
        getchar();
        system( "cls" );
    }
    return 0;
}
P-134345
pekfos
» 2015-07-05 17:33:48
C/C++
switch( opcja );
Średnik.
P-134346
NKKS
Temat założony przez niniejszego użytkownika
pekfos
» 2015-07-05 17:56:11
gdy usuwam średnik wyskakuje mi:
01.cpp:85: error: jump to case label
01.cpp:82: error:   crosses initialization of `float polekw'
01.cpp:81: error:   crosses initialization of `float isamu'
01.cpp:91: error: jump to case label
01.cpp:88: error:   crosses initialization of `float pole'
01.cpp:82: error:   crosses initialization of `float polekw'
01.cpp:81: error:   crosses initialization of `float isamu'
01.cpp:98: error: jump to case label
01.cpp:95: error:   crosses initialization of `float polepr'
01.cpp:94: error:   crosses initialization of `float obwodpr'
01.cpp:88: error:   crosses initialization of `float pole'
01.cpp:82: error:   crosses initialization of `float polekw'
01.cpp:81: error:   crosses initialization of `float isamu'
01.cpp:106: error: jump to case label
01.cpp:103: error:   crosses initialization of `float poletrapezu'
01.cpp:95: error:   crosses initialization of `float polepr'
01.cpp:94: error:   crosses initialization of `float obwodpr'
01.cpp:88: error:   crosses initialization of `float pole'
01.cpp:82: error:   crosses initialization of `float polekw'
01.cpp:81: error:   crosses initialization of `float isamu'
01.cpp:114: error: jump to case label
01.cpp:111: error:   crosses initialization of `float dodawaniee'
01.cpp:103: error:   crosses initialization of `float poletrapezu'
01.cpp:95: error:   crosses initialization of `float polepr'
01.cpp:94: error:   crosses initialization of `float obwodpr'
01.cpp:88: error:   crosses initialization of `float pole'
01.cpp:82: error:   crosses initialization of `float polekw'
01.cpp:81: error:   crosses initialization of `float isamu'
01.cpp:122: error: jump to case label
01.cpp:119: error:   crosses initialization of `float odejmowaniie'
01.cpp:111: error:   crosses initialization of `float dodawaniee'
01.cpp:103: error:   crosses initialization of `float poletrapezu'
01.cpp:95: error:   crosses initialization of `float polepr'
01.cpp:94: error:   crosses initialization of `float obwodpr'
01.cpp:88: error:   crosses initialization of `float pole'
01.cpp:82: error:   crosses initialization of `float polekw'
01.cpp:81: error:   crosses initialization of `float isamu'
01.cpp:130: error: jump to case label
01.cpp:127: error:   crosses initialization of `float mmnozenie'
01.cpp:119: error:   crosses initialization of `float odejmowaniie'
01.cpp:111: error:   crosses initialization of `float dodawaniee'
01.cpp:103: error:   crosses initialization of `float poletrapezu'
01.cpp:95: error:   crosses initialization of `float polepr'
01.cpp:94: error:   crosses initialization of `float obwodpr'
01.cpp:88: error:   crosses initialization of `float pole'
01.cpp:82: error:   crosses initialization of `float polekw'
01.cpp:81: error:   crosses initialization of `float isamu'
01.cpp:145: error: jump to case label
01.cpp:127: error:   crosses initialization of `float mmnozenie'
01.cpp:119: error:   crosses initialization of `float odejmowaniie'
01.cpp:111: error:   crosses initialization of `float dodawaniee'
01.cpp:103: error:   crosses initialization of `float poletrapezu'
01.cpp:95: error:   crosses initialization of `float polepr'
01.cpp:94: error:   crosses initialization of `float obwodpr'
01.cpp:88: error:   crosses initialization of `float pole'
01.cpp:82: error:   crosses initialization of `float polekw'
01.cpp:81: error:   crosses initialization of `float isamu'
01.cpp:148: error: jump to case label
01.cpp:127: error:   crosses initialization of `float mmnozenie'
01.cpp:119: error:   crosses initialization of `float odejmowaniie'
01.cpp:111: error:   crosses initialization of `float dodawaniee'
01.cpp:103: error:   crosses initialization of `float poletrapezu'
01.cpp:95: error:   crosses initialization of `float polepr'
01.cpp:94: error:   crosses initialization of `float obwodpr'
01.cpp:88: error:   crosses initialization of `float pole'
01.cpp:82: error:   crosses initialization of `float polekw'
01.cpp:81: error:   crosses initialization of `float isamu'
P-134348
notabigthreat
» 2015-07-05 18:13:07
C/C++
case 1:
{
    //tu kod
    break;
}
case 2:
{
    //tu kod
    break;
}
//...
Klamry ograniczają widoczność zmiennej w ten sposób, że nie może być ona użyta w innym
case
 - tak bezpieczniej. Rozważ taki przykład:
C/C++
int a;
cin >> a;
switch( a )
{
case 0:
    int b = 3;
case 1:
    cout << b; //b jest tu dostępne
}
Jakby to się kompilowało (i działałoby zgodnie z intuicją), to gdyby ktoś wprowadził 0, wszystko dobrze, lecz co jak na wejściu znalazłaby się jedynka?


Przetestuj dzielenie dla takich wejść:
0 0
0 1
1 0
P-134349
« 1 »
  Strona 1 z 1