Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. switch quantity not an integer!

switch quantity not an integer!

Scheduled Pinned Locked Moved C / C++ / MFC
help
10 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    moloza
    wrote on last edited by
    #1

    Hello all.. for the following program I got headache about the switch statment: if I use switch(*hello) I got a compile error : switch quantity not an integer! if I remove the stat I got a runtime error: segmentation fault! I need help to fix this problem.. #include main() { int *hello; printf("Hi, Enter any value > "); scanf("%d",&hello); switch (hello) { case 1: printf("your 1 luck number is > %d\n",hello); case 2: printf("your 2 luck number is > %d\n",hello); case 3: printf("your 3 luck number is > %d\n",hello); default: printf("your 4 luck number is > %d\n",hello); } } thanks Ajmi

    P B N T 4 Replies Last reply
    0
    • M moloza

      Hello all.. for the following program I got headache about the switch statment: if I use switch(*hello) I got a compile error : switch quantity not an integer! if I remove the stat I got a runtime error: segmentation fault! I need help to fix this problem.. #include main() { int *hello; printf("Hi, Enter any value > "); scanf("%d",&hello); switch (hello) { case 1: printf("your 1 luck number is > %d\n",hello); case 2: printf("your 2 luck number is > %d\n",hello); case 3: printf("your 3 luck number is > %d\n",hello); default: printf("your 4 luck number is > %d\n",hello); } } thanks Ajmi

      P Offline
      P Offline
      Peter Weyzen
      wrote on last edited by
      #2

      did you try changing the declaration of hello to be: int hello; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc.](http://www.soonr.com)

      1 Reply Last reply
      0
      • M moloza

        Hello all.. for the following program I got headache about the switch statment: if I use switch(*hello) I got a compile error : switch quantity not an integer! if I remove the stat I got a runtime error: segmentation fault! I need help to fix this problem.. #include main() { int *hello; printf("Hi, Enter any value > "); scanf("%d",&hello); switch (hello) { case 1: printf("your 1 luck number is > %d\n",hello); case 2: printf("your 2 luck number is > %d\n",hello); case 3: printf("your 3 luck number is > %d\n",hello); default: printf("your 4 luck number is > %d\n",hello); } } thanks Ajmi

        B Offline
        B Offline
        Bob Ciora
        wrote on last edited by
        #3

        The segmentation fault is because the scanf statement is writing a new value to the POINTER hello, setting it to the value entered by the user. So if the user enters "4", hello is now a pointer to "address" 4. This will crash. Do this instead: int hello; printf("Hi, Enter any value > "); scanf("%d", &hello); switch(hello) { ... ... } Bob Ciora

        1 Reply Last reply
        0
        • M moloza

          Hello all.. for the following program I got headache about the switch statment: if I use switch(*hello) I got a compile error : switch quantity not an integer! if I remove the stat I got a runtime error: segmentation fault! I need help to fix this problem.. #include main() { int *hello; printf("Hi, Enter any value > "); scanf("%d",&hello); switch (hello) { case 1: printf("your 1 luck number is > %d\n",hello); case 2: printf("your 2 luck number is > %d\n",hello); case 3: printf("your 3 luck number is > %d\n",hello); default: printf("your 4 luck number is > %d\n",hello); } } thanks Ajmi

          N Offline
          N Offline
          Naveen
          wrote on last edited by
          #4

          moloza wrote:

          int *hello;

          int hello is enough nave

          M 1 Reply Last reply
          0
          • N Naveen

            moloza wrote:

            int *hello;

            int hello is enough nave

            M Offline
            M Offline
            moloza
            wrote on last edited by
            #5

            Thanks all but I need hello to be a pointer because it might be passed as an argument to some function (like in case of use 'recv' in socket programing) in the program. Actually I submet a very simple program but what if the program read hello from the keyboard and send it to another side in a socket connection, ofcourse, I will need hello to be a pointer. so the change must be somewhere in the program rather than the decleration of hello. but where?!! thanks again Moloza Ajmi

            N A 2 Replies Last reply
            0
            • M moloza

              Thanks all but I need hello to be a pointer because it might be passed as an argument to some function (like in case of use 'recv' in socket programing) in the program. Actually I submet a very simple program but what if the program read hello from the keyboard and send it to another side in a socket connection, ofcourse, I will need hello to be a pointer. so the change must be somewhere in the program rather than the decleration of hello. but where?!! thanks again Moloza Ajmi

              N Offline
              N Offline
              Naveen
              wrote on last edited by
              #6

              modify like this.. main() { int *hello = new int; printf("Hi, Enter any value > "); scanf("%d",hello); switch (*hello) { case 1: printf("your 1 luck number is > %d\n",hello); case 2: printf("your 2 luck number is > %d\n",hello); case 3: printf("your 3 luck number is > %d\n",hello); default: printf("your 4 luck number is > %d\n",hello); } } nave

              1 Reply Last reply
              0
              • M moloza

                Hello all.. for the following program I got headache about the switch statment: if I use switch(*hello) I got a compile error : switch quantity not an integer! if I remove the stat I got a runtime error: segmentation fault! I need help to fix this problem.. #include main() { int *hello; printf("Hi, Enter any value > "); scanf("%d",&hello); switch (hello) { case 1: printf("your 1 luck number is > %d\n",hello); case 2: printf("your 2 luck number is > %d\n",hello); case 3: printf("your 3 luck number is > %d\n",hello); default: printf("your 4 luck number is > %d\n",hello); } } thanks Ajmi

                T Offline
                T Offline
                ThatsAlok
                wrote on last edited by
                #7

                moloza wrote:

                int *hello;printf("Hi, Enter any value > ");scanf("%d",&hello);switch (hello)

                if you still want to use pointers in your code, you see this example

                #include
                #include
                main()
                {
                int *hello;
                printf("Hi, Enter any value > ");
                hello=(int*)malloc(sizeof(int));
                scanf("%d",hello);
                switch (*hello)
                {
                case 1: printf("your 1 luck number is > %d\n",*hello); break;
                case 2: printf("your 2 luck number is > %d\n",*hello);break;
                case 3: printf("your 3 luck number is > %d\n",*hello);break;
                default: printf("your 4 luck number is > %d\n",*hello);
                }
                }

                "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                cheers, Alok Gupta VC Forum Q&A :- I/ IV

                M 1 Reply Last reply
                0
                • M moloza

                  Thanks all but I need hello to be a pointer because it might be passed as an argument to some function (like in case of use 'recv' in socket programing) in the program. Actually I submet a very simple program but what if the program read hello from the keyboard and send it to another side in a socket connection, ofcourse, I will need hello to be a pointer. so the change must be somewhere in the program rather than the decleration of hello. but where?!! thanks again Moloza Ajmi

                  A Offline
                  A Offline
                  Andrew Hain
                  wrote on last edited by
                  #8

                  If you need a pointer to hello, you can get one by writing &hello.

                  1 Reply Last reply
                  0
                  • T ThatsAlok

                    moloza wrote:

                    int *hello;printf("Hi, Enter any value > ");scanf("%d",&hello);switch (hello)

                    if you still want to use pointers in your code, you see this example

                    #include
                    #include
                    main()
                    {
                    int *hello;
                    printf("Hi, Enter any value > ");
                    hello=(int*)malloc(sizeof(int));
                    scanf("%d",hello);
                    switch (*hello)
                    {
                    case 1: printf("your 1 luck number is > %d\n",*hello); break;
                    case 2: printf("your 2 luck number is > %d\n",*hello);break;
                    case 3: printf("your 3 luck number is > %d\n",*hello);break;
                    default: printf("your 4 luck number is > %d\n",*hello);
                    }
                    }

                    "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                    cheers, Alok Gupta VC Forum Q&A :- I/ IV

                    M Offline
                    M Offline
                    moloza
                    wrote on last edited by
                    #9

                    Thanks for ALL ITS working now>> CHEERS Ajmi

                    T 1 Reply Last reply
                    0
                    • M moloza

                      Thanks for ALL ITS working now>> CHEERS Ajmi

                      T Offline
                      T Offline
                      ThatsAlok
                      wrote on last edited by
                      #10

                      moloza wrote:

                      ITS working now>>

                      you are always welcome!

                      "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                      cheers, Alok Gupta VC Forum Q&A :- I/ IV

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups