-
Strange or stupidity
On a particluar menu option I have different option 1-6. I have put an if statement in my code to produce an error mesage if the input is greater than 6 but my program just seems to ignore it and exit.
[CODE]
{
int choice = 0; /* sets choice as an integer value starts at 0*/
while ((choice <=6)) /* Sets a while statement so case is only active within range less than 6*/
{
switch (choice) /* Choice number will represent menu */
{
case 1:
{
num_lines(); /* When 1 is selected takes it to sub program num_lines */
break;
}
case 2: /* Case 2 to case 6 same as above */
{
num_words();
break;
}
case 3:
{
num_chars();
break;
}
case 4:
{
chars_rev();
break;
}
case 5:
{
lines_rev();
break;
}
case 6:
{
exit(1);
}
if(choice >=7)
{
printf("invalid selection");
getch();
}
}
{/CODE]
Any help would be much appreciated cheers
-
-
Layout the code properly (indent it) and you'll see that the if statement is in the wrong place. For a start, it's buried in the switch, after the exit(1), therefore it will never run. Also, it's inside a while loop that is controlled with (choice <=6), so testing for 7 or higher inside this loop will also never work.
btw, code tags are like in my sig.
And delete the duplicate thread!
-
Thanks hammer sorry about that I type
{/code]
by accident at the end dont know how to delete other thread doh
thx again
-
delete: edit the other thread, and at the top you'll see a delete tick box and button.
-
Hmmmmm
I cant seem to do it because if I put the if statement before the while statement it still ignores it. But if I change the int choice=0 it automatically runs the if statement
your permanently in deep state of confusion
-
An example for you to study
Code:
int choice = -1;
while (choice != 0)
{
switch (choice)
{
case 0: /* Do nothing */
break;
case 1:
dostuff1();
break;
case 2:
dostuff2();
break;
case 3:
dostuff3();
break;
case 4:
dostuff4();
break;
default:
dootherstuff();
break;
}
}
Obviously, somewhere you'll need to change the value of choice. :)
-
OK im getting there I think but for some reason the error i.e Hello just for the sake of a message comes up when the program begins as well
I know its probably something simple but i cant figure it out, think im thinking to hard
Code:
int choice=-1; /* sets choice as an integer value starts at 0*/
while (choice!=0&&choice<7) /* Sets a while statement so case is only active within range less than 6*/
{
switch (choice) /* Choice number will represent menu */
{
case 1:
{
num_lines(); /* When 1 is selected takes it to sub program num_lines */
break;
}
case 2: /* Case 2 to case 6 same as above */
{
num_words();
break;
}
case 3:
{
num_chars();
break;
}
case 4:
{
chars_rev();
break;
}
case 5:
{
lines_rev();
break;
}
case 6:
{
exit();
}
default:
{
printf("hello");
getch();
}
}
thx again
-
You may also want to check for negative values since int is a signed type.
-
Quote:
Originally posted by Bazza
OK im getting there I think but for some reason the error i.e Hello just for the sake of a message comes up when the program begins as well
I know its probably something simple but i cant figure it out, think im thinking to hard
Code:
int choice=-1; /* sets choice as an integer value starts at 0*/
while (choice!=0&&choice<7)
<--snip--->
default:
{
printf("hello");
getch();
}
}
Look at the logic of the loop. The first time through, choice will be -1, and will therefore drop straight into the "default" section. And, I presume that you are actually changing the value of choice somewhere else in the code? In you example given you are asking the user for a choice, I take it this is done in code you haven't posted? It doesn't matter if so, I don't want/need to see it, just as long as you understand :)