problem with nested switches

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MZaza

    problem with nested switches

    Hello,

    When I compile it I get an error in line 16, which got case 'c'.

    #include <cstdlib>
    #include <iostream>
    #include <math.h>
    using namespace std;

    int main()
    {
    float x, y;
    double r;
    char o;

    cout <<"To use the calculator press C" <<endl <<"To quite press Q"
    <<endl;

    switch (o);
    {
    case 'c': cout <<"Enter the mathmatical operation" <<endl;
    cin >>x >>o >>y;

    switch (o)
    {
    case '+': r=x+y;
    cout <<"The result is: " <<r <<endl;
    break;

    case '-': r=x-y;
    cout <<"The result is: " <<r <<endl;
    break;

    case '*': r=x*y;
    cout <<"The result is: " <<r <<endl;
    break;

    case '/': r=x/y;
    cout <<"The result is: " <<r <<endl;
    break;

    case '^': r=pow(x, y);
    cout <<"The result is: " <<r <<endl;
    break;

    default: cout <<"Error: wrong input"
    <<endl;
    }
    break;

    case 'q': cout <<"cya" <<endl;
    break;
    }

    system("PAUSE") ;
    return EXIT_SUCCESS;
    }


    Guys, I just started programming 2 days ago so take it easy on me :).

    --
    Mustafa Zaza
  • Victor Bazarov

    #2
    Re: problem with nested switches

    MZaza wrote:
    Hello,
    >
    When I compile it I get an error in line 16, which got case 'c'.
    >
    #include <cstdlib>
    #include <iostream>
    #include <math.h>
    using namespace std;
    >
    int main()
    {
    float x, y;
    double r;
    char o;
    >
    cout <<"To use the calculator press C" <<endl <<"To quite press Q"
    <<endl;
    >
    switch (o);
    A semicolon? And what is the value of 'o' here?
    {
    case 'c': cout <<"Enter the mathmatical operation" <<endl;
    cin >>x >>o >>y;
    >
    switch (o)
    {
    case '+': r=x+y;
    cout <<"The result is: " <<r <<endl;
    break;
    >
    case '-': r=x-y;
    cout <<"The result is: " <<r <<endl;
    break;
    >
    case '*': r=x*y;
    cout <<"The result is: " <<r <<endl;
    break;
    >
    case '/': r=x/y;
    cout <<"The result is: " <<r <<endl;
    break;
    >
    case '^': r=pow(x, y);
    cout <<"The result is: " <<r <<endl;
    break;
    >
    default: cout <<"Error: wrong input"
    <<endl;
    }
    break;
    >
    case 'q': cout <<"cya" <<endl;
    break;
    }
    >
    system("PAUSE") ;
    return EXIT_SUCCESS;
    }
    >
    >
    Guys, I just started programming 2 days ago so take it easy on me :).
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • Thomas J. Gritzan

      #3
      Re: problem with nested switches

      MZaza schrieb:
      Hello,
      >
      When I compile it I get an error in line 16, which got case 'c'.
      It would help if you provided us the error message you got.
      #include <cstdlib>
      #include <iostream>
      #include <math.h>
      using namespace std;
      >
      int main()
      {
      float x, y;
      double r;
      char o;
      >
      cout <<"To use the calculator press C" <<endl <<"To quite press Q"
      <<endl;
      >
      switch (o);
      ^
      Try to remove the semicolon here.
      {
      case 'c': cout <<"Enter the mathmatical operation" <<endl;
      cin >>x >>o >>y;
      [...]

      --
      Thomas

      sigfault

      Comment

      • MZaza

        #4
        Re: problem with nested switches

        On Mar 5, 9:13 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
        MZaza wrote:
        Hello,
        >
        When I compile it I get an error in line 16, which got case 'c'.
        >
        #include <cstdlib>
        #include <iostream>
        #include <math.h>
        using namespace std;
        >
        int main()
        {
        float x, y;
        double r;
        char o;
        >
        cout <<"To use the calculator press C" <<endl <<"To quite press Q"
        <<endl;
        >
        switch (o);
        >
        A semicolon? And what is the value of 'o' here?
        >
        >
        >
        {
        case 'c': cout <<"Enter the mathmatical operation" <<endl;
        cin >>x >>o >>y;
        >
        switch (o)
        {
        case '+': r=x+y;
        cout <<"The result is: " <<r <<endl;
        break;
        >
        case '-': r=x-y;
        cout <<"The result is: " <<r <<endl;
        break;
        >
        case '*': r=x*y;
        cout <<"The result is: " <<r <<endl;
        break;
        >
        case '/': r=x/y;
        cout <<"The result is: " <<r <<endl;
        break;
        >
        case '^': r=pow(x, y);
        cout <<"The result is: " <<r <<endl;
        break;
        >
        default: cout <<"Error: wrong input"
        <<endl;
        }
        break;
        >
        case 'q': cout <<"cya" <<endl;
        break;
        }
        >
        system("PAUSE") ;
        return EXIT_SUCCESS;
        }
        >
        Guys, I just started programming 2 days ago so take it easy on me :).
        >
        --
        Please remove capital 'A's when replying by e-mail
        I do not respond to top-posted replies, please don't ask
        I should get the value of the 'o' from the user, just as the x and the
        y.
        Is that what you mean?

        Comment

        • michael.goossens@gmail.com

          #5
          Re: problem with nested switches

          o doesnt seem to have a value and the ";" behind "switch(o); " doesn't
          seem right.

          But tbh switches aren't used that much, but guess you are just
          learning :). Keep up the good job.

          Comment

          • MZaza

            #6
            Re: problem with nested switches

            On Mar 5, 9:24 pm, "michael.gooss. ..@gmail.com"
            <michael.gooss. ..@gmail.comwro te:
            o doesnt seem to have a value and the ";" behind "switch(o); " doesn't
            seem right.
            >
            But tbh switches aren't used that much, but guess you are just
            learning :). Keep up the good job.
            Thanks :)
            But I got a problem... the program worked faultlessly when I had only
            one switch which is the one that got the cases for the mathematical
            operations. After I added it in the other switch, which is the one
            that got the menu shortcuts it doesn't work probably it just cout this
            "To use the calculator press C" <<endl <<"To quite press Q" then it
            quits..

            Here is a screen shot,

            Comment

            • michael.goossens@gmail.com

              #7
              Re: problem with nested switches

              On 5 mrt, 21:32, MZaza <mustafa.z...@g mail.comwrote:
              On Mar 5, 9:24 pm, "michael.gooss. ..@gmail.com"
              >
              <michael.gooss. ..@gmail.comwro te:
              o doesnt seem to have a value and the ";" behind "switch(o); " doesn't
              seem right.
              >
              But tbh switches aren't used that much, but guess you are just
              learning :). Keep up the good job.
              >
              Thanks :)
              But I got a problem... the program worked faultlessly when I had only
              one switch which is the one that got the cases for the mathematical
              operations. After I added it in the other switch, which is the one
              that got the menu shortcuts it doesn't work probably it just cout this
              "To use the calculator press C" <<endl <<"To quite press Q" then it
              quits..
              >
              Here is a screen shot,http://img258.imageshack.us/img258/893/98124847qf2.jpg
              do it with if {} else if {} else {} =). Those are nestable 100% and
              easier.

              Comment

              • MZaza

                #8
                Re: problem with nested switches

                On Mar 5, 10:46 pm, "michael.gooss. ..@gmail.com"
                <michael.gooss. ..@gmail.comwro te:
                On 5 mrt, 21:32, MZaza <mustafa.z...@g mail.comwrote:
                >
                >
                >
                On Mar 5, 9:24 pm, "michael.gooss. ..@gmail.com"
                >
                <michael.gooss. ..@gmail.comwro te:
                o doesnt seem to have a value and the ";" behind "switch(o); " doesn't
                seem right.
                >
                But tbh switches aren't used that much, but guess you are just
                learning :). Keep up the good job.
                >
                Thanks :)
                But I got a problem... the program worked faultlessly when I had only
                one switch which is the one that got the cases for the mathematical
                operations. After I added it in the other switch, which is the one
                that got the menu shortcuts it doesn't work probably it just cout this
                "To use the calculator press C" <<endl <<"To quite press Q" then it
                quits..
                >>
                do it with if {} else if {} else {} =). Those are nestable 100% and
                easier.
                I've tried the if {} else {} and it's not working probably, it does
                the if {} even if the condition doesn't match with it and matches with
                the else {}.

                #include <cstdlib>
                #include <iostream>
                #include <math.h>
                using namespace std;

                int main()
                {
                float x, y;
                double r;
                char o, c;

                cout <<"To use the calculator press C" <<endl <<"To quite press Q"
                <<endl;

                if (cin >>c)
                {
                cout <<"Enter the mathmatical operation" <<endl;
                cin >>x >>o >>y;

                switch (o)
                {
                case '+': r=x+y;
                cout <<"The result is: " <<r <<endl;
                break;

                case '-': r=x-y;
                cout <<"The result is: " <<r <<endl;
                break;

                case '*': r=x*y;
                cout <<"The result is: " <<r <<endl;
                break;

                case '/': r=x/y;
                cout <<"The result is: " <<r <<endl;
                break;

                case '^': r=pow(x, y);
                cout <<"The result is: " <<r <<endl;
                break;

                default: cout <<"Error: wrong input" <<endl;
                }
                }

                else
                {
                cout <<"Eorror";
                }

                system("PAUSE") ;
                return EXIT_SUCCESS;
                }


                Comment

                • Micah Cowan

                  #9
                  Re: problem with nested switches

                  "michael.goosse ns@gmail.com" <michael.goosse ns@gmail.comwri tes:
                  On 5 mrt, 21:32, MZaza <mustafa.z...@g mail.comwrote:
                  >On Mar 5, 9:24 pm, "michael.gooss. ..@gmail.com"
                  >>
                  ><michael.gooss ...@gmail.comwr ote:
                  o doesnt seem to have a value and the ";" behind "switch(o); " doesn't
                  seem right.
                  >>
                  But tbh switches aren't used that much, but guess you are just
                  learning :). Keep up the good job.
                  >>
                  >Thanks :)
                  >But I got a problem... the program worked faultlessly when I had only
                  >one switch which is the one that got the cases for the mathematical
                  >operations. After I added it in the other switch, which is the one
                  >that got the menu shortcuts it doesn't work probably it just cout this
                  >"To use the calculator press C" <<endl <<"To quite press Q" then it
                  >quits..
                  >>
                  >Here is a screen shot,http://img258.imageshack.us/img258/893/98124847qf2.jpg
                  >
                  do it with if {} else if {} else {} =). Those are nestable 100% and
                  easier.
                  switches are also nestable 100%, and I disagree on the "easier".

                  AFAICT, the reason switch statements are rarer in C++ than in C is
                  that most in most of the use cases for it in C, polymorphic typing is
                  the better idiom in C++. However, for cases such as selecting on a
                  character value read on input (like this one), it seems like the most
                  straightforward approach, and I at least would find it more readable
                  than the equivalent in if/else-if.

                  --
                  Micah J. Cowan
                  Programmer, musician, typesetting enthusiast, gamer...

                  Comment

                  • MZaza

                    #10
                    Re: problem with nested switches

                    On Mar 6, 12:00 am, MZaza <mustafa.z...@g mail.comwrote:
                    On Mar 5, 10:46 pm, "michael.gooss. ..@gmail.com"
                    >
                    >
                    >
                    <michael.gooss. ..@gmail.comwro te:
                    On 5 mrt, 21:32, MZaza <mustafa.z...@g mail.comwrote:
                    >
                    On Mar 5, 9:24 pm, "michael.gooss. ..@gmail.com"
                    >
                    <michael.gooss. ..@gmail.comwro te:
                    o doesnt seem to have a value and the ";" behind "switch(o); " doesn't
                    seem right.
                    >
                    But tbh switches aren't used that much, but guess you are just
                    learning :). Keep up the good job.
                    >
                    Thanks :)
                    But I got a problem... the program worked faultlessly when I had only
                    one switch which is the one that got the cases for the mathematical
                    operations. After I added it in the other switch, which is the one
                    that got the menu shortcuts it doesn't work probably it just cout this
                    "To use the calculator press C" <<endl <<"To quite press Q" then it
                    quits..
                    >>
                    do it with if {} else if {} else {} =). Those are nestable 100% and
                    easier.
                    >
                    I've tried the if {} else {} and it's not working probably, it does
                    the if {} even if the condition doesn't match with it and matches with
                    the else {}.
                    >
                    #include <cstdlib>
                    #include <iostream>
                    #include <math.h>
                    using namespace std;
                    >
                    int main()
                    {
                    float x, y;
                    double r;
                    char o, c;
                    >
                    cout <<"To use the calculator press C" <<endl <<"To quite press Q"
                    <<endl;
                    >
                    if (cin >>c)
                    {
                    cout <<"Enter the mathmatical operation" <<endl;
                    cin >>x >>o >>y;
                    >
                    switch (o)
                    {
                    case '+': r=x+y;
                    cout <<"The result is: " <<r <<endl;
                    break;
                    >
                    case '-': r=x-y;
                    cout <<"The result is: " <<r <<endl;
                    break;
                    >
                    case '*': r=x*y;
                    cout <<"The result is: " <<r <<endl;
                    break;
                    >
                    case '/': r=x/y;
                    cout <<"The result is: " <<r <<endl;
                    break;
                    >
                    case '^': r=pow(x, y);
                    cout <<"The result is: " <<r <<endl;
                    break;
                    >
                    default: cout <<"Error: wrong input" <<endl;
                    }
                    }
                    >
                    else
                    {
                    cout <<"Eorror";
                    }
                    >
                    system("PAUSE") ;
                    return EXIT_SUCCESS;
                    >
                    }
                    Any Ideas???

                    Comment

                    • MZaza

                      #11
                      Re: problem with nested switches

                      On Mar 6, 12:00 am, MZaza <mustafa.z...@g mail.comwrote:
                      On Mar 5, 10:46 pm, "michael.gooss. ..@gmail.com"
                      >
                      >
                      >
                      <michael.gooss. ..@gmail.comwro te:
                      On 5 mrt, 21:32, MZaza <mustafa.z...@g mail.comwrote:
                      >
                      On Mar 5, 9:24 pm, "michael.gooss. ..@gmail.com"
                      >
                      <michael.gooss. ..@gmail.comwro te:
                      o doesnt seem to have a value and the ";" behind "switch(o); " doesn't
                      seem right.
                      >
                      But tbh switches aren't used that much, but guess you are just
                      learning :). Keep up the good job.
                      >
                      Thanks :)
                      But I got a problem... the program worked faultlessly when I had only
                      one switch which is the one that got the cases for the mathematical
                      operations. After I added it in the other switch, which is the one
                      that got the menu shortcuts it doesn't work probably it just cout this
                      "To use the calculator press C" <<endl <<"To quite press Q" then it
                      quits..
                      >>
                      do it with if {} else if {} else {} =). Those are nestable 100% and
                      easier.
                      >
                      I've tried the if {} else {} and it's not working probably, it does
                      the if {} even if the condition doesn't match with it and matches with
                      the else {}.
                      >
                      #include <cstdlib>
                      #include <iostream>
                      #include <math.h>
                      using namespace std;
                      >
                      int main()
                      {
                      float x, y;
                      double r;
                      char o, c;
                      >
                      cout <<"To use the calculator press C" <<endl <<"To quite press Q"
                      <<endl;
                      >
                      if (cin >>c)
                      {
                      cout <<"Enter the mathmatical operation" <<endl;
                      cin >>x >>o >>y;
                      >
                      switch (o)
                      {
                      case '+': r=x+y;
                      cout <<"The result is: " <<r <<endl;
                      break;
                      >
                      case '-': r=x-y;
                      cout <<"The result is: " <<r <<endl;
                      break;
                      >
                      case '*': r=x*y;
                      cout <<"The result is: " <<r <<endl;
                      break;
                      >
                      case '/': r=x/y;
                      cout <<"The result is: " <<r <<endl;
                      break;
                      >
                      case '^': r=pow(x, y);
                      cout <<"The result is: " <<r <<endl;
                      break;
                      >
                      default: cout <<"Error: wrong input" <<endl;
                      }
                      }
                      >
                      else
                      {
                      cout <<"Eorror";
                      }
                      >
                      system("PAUSE") ;
                      return EXIT_SUCCESS;
                      >
                      }
                      Any Ideas???

                      Comment

                      • MZaza

                        #12
                        Re: problem with nested switches

                        On Mar 6, 12:00 am, MZaza <mustafa.z...@g mail.comwrote:
                        On Mar 5, 10:46 pm, "michael.gooss. ..@gmail.com"
                        >
                        >
                        >
                        <michael.gooss. ..@gmail.comwro te:
                        On 5 mrt, 21:32, MZaza <mustafa.z...@g mail.comwrote:
                        >
                        On Mar 5, 9:24 pm, "michael.gooss. ..@gmail.com"
                        >
                        <michael.gooss. ..@gmail.comwro te:
                        o doesnt seem to have a value and the ";" behind "switch(o); " doesn't
                        seem right.
                        >
                        But tbh switches aren't used that much, but guess you are just
                        learning :). Keep up the good job.
                        >
                        Thanks :)
                        But I got a problem... the program worked faultlessly when I had only
                        one switch which is the one that got the cases for the mathematical
                        operations. After I added it in the other switch, which is the one
                        that got the menu shortcuts it doesn't work probably it just cout this
                        "To use the calculator press C" <<endl <<"To quite press Q" then it
                        quits..
                        >>
                        do it with if {} else if {} else {} =). Those are nestable 100% and
                        easier.
                        >
                        I've tried the if {} else {} and it's not working probably, it does
                        the if {} even if the condition doesn't match with it and matches with
                        the else {}.
                        >
                        #include <cstdlib>
                        #include <iostream>
                        #include <math.h>
                        using namespace std;
                        >
                        int main()
                        {
                        float x, y;
                        double r;
                        char o, c;
                        >
                        cout <<"To use the calculator press C" <<endl <<"To quite press Q"
                        <<endl;
                        >
                        if (cin >>c)
                        {
                        cout <<"Enter the mathmatical operation" <<endl;
                        cin >>x >>o >>y;
                        >
                        switch (o)
                        {
                        case '+': r=x+y;
                        cout <<"The result is: " <<r <<endl;
                        break;
                        >
                        case '-': r=x-y;
                        cout <<"The result is: " <<r <<endl;
                        break;
                        >
                        case '*': r=x*y;
                        cout <<"The result is: " <<r <<endl;
                        break;
                        >
                        case '/': r=x/y;
                        cout <<"The result is: " <<r <<endl;
                        break;
                        >
                        case '^': r=pow(x, y);
                        cout <<"The result is: " <<r <<endl;
                        break;
                        >
                        default: cout <<"Error: wrong input" <<endl;
                        }
                        }
                        >
                        else
                        {
                        cout <<"Eorror";
                        }
                        >
                        system("PAUSE") ;
                        return EXIT_SUCCESS;
                        >
                        }
                        Any Ideas???

                        Comment

                        • lollinus@gmail.com

                          #13
                          Re: problem with nested switches

                          On 5 Mar, 20:12, MZaza <mustafa.z...@g mail.comwrote:
                          Hello,
                          >
                          When I compile it I get an error in line 16, which got case 'c'.
                          >
                          #include <cstdlib>
                          #include <iostream>
                          #include <math.h>
                          using namespace std;
                          >
                          int main()
                          {
                          float x, y;
                          double r;
                          char o;
                          >
                          cout <<"To use the calculator press C" <<endl <<"To quite press Q"
                          <<endl;
                          cin >o;

                          You need to acquire first o;
                          >
                          switch (o);
                          ^
                          There you go. Remove this semicolon and it should work fine.
                          {
                          case 'c': cout <<"Enter the mathmatical operation" <<endl;
                          cin >>x >>o >>y;
                          >
                          switch (o)
                          {
                          case '+': r=x+y;
                          cout <<"The result is: " <<r <<endl;
                          break;
                          >
                          case '-': r=x-y;
                          cout <<"The result is: " <<r <<endl;
                          break;
                          >
                          case '*': r=x*y;
                          cout <<"The result is: " <<r <<endl;
                          break;
                          >
                          case '/': r=x/y;
                          cout <<"The result is: " <<r <<endl;
                          break;
                          >
                          case '^': r=pow(x, y);
                          cout <<"The result is: " <<r <<endl;
                          break;
                          >
                          default: cout <<"Error: wrong input"
                          <<endl;
                          }
                          break;
                          >
                          case 'q': cout <<"cya" <<endl;
                          break;
                          }
                          >
                          system("PAUSE") ;
                          return EXIT_SUCCESS;
                          >
                          }
                          >
                          Guys, I just started programming 2 days ago so take it easy on me :).
                          >
                          --
                          Mustafa Zaza

                          Comment

                          • Victor Bazarov

                            #14
                            Re: problem with nested switches

                            MZaza wrote:
                            [..]
                            >
                            Any Ideas???
                            Posted the same message three times within 5 minutes. It seem
                            you're desperate. But even if it's so there is no need to pollute
                            Usenet. Patience is a virtue. Newsgroups are not chat rooms.

                            V
                            --
                            Please remove capital 'A's when replying by e-mail
                            I do not respond to top-posted replies, please don't ask


                            Comment

                            • MZaza

                              #15
                              Re: problem with nested switches

                              On Mar 6, 12:22 am, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
                              MZaza wrote:
                              [..]
                              >
                              Any Ideas???
                              >
                              Posted the same message three times within 5 minutes. It seem
                              you're desperate. But even if it's so there is no need to pollute
                              Usenet. Patience is a virtue. Newsgroups are not chat rooms.
                              >
                              V
                              --
                              Please remove capital 'A's when replying by e-mail
                              I do not respond to top-posted replies, please don't ask
                              @victor
                              There was a problem with my browser, when I refreshed to check if
                              there are new answers it reposted the message again.

                              Comment

                              Working...