switch quantity not an integer!
-
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
-
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
did you try changing the declaration of hello to be: int hello;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc.](http://www.soonr.com)
-
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
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 -
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
-
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
-
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
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
-
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
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
-
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
If you need a pointer to hello, you can get one by writing
&hello
. -
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