Hi,
Can I use an if else statement withing a Case??
e.g Case 'C': if(*roundHr < 3)
.
.
.
.
.
etc
how could I implement that??
Thanks
A :)
Printable View
Hi,
Can I use an if else statement withing a Case??
e.g Case 'C': if(*roundHr < 3)
.
.
.
.
.
etc
how could I implement that??
Thanks
A :)
Don't put too much inside one case though, your program will get confusing.Code:switch (c)
{
case 'A':
if (something == TRUE)
{
printf("blurb\n");
}
else
{
printf("No blurb\n");
}
break;
case 'B':
.....
default:
.....
}
Code:#include <stdio.h>
#include <conio.h>
int choices ( void );
int main ( void )
{
int Number = 100;
char Choice;
while( choices() == 0 )
{
Choice=getch();
switch(Choice)
{
case 'n':
if(Number == 100)
{
printf("%d\n\n", Number);
}
else
{
printf("You changed Number's value!\n\n");
}
break;
case 27:
exit(0);
break;
default:
break;
}
}
return 0;
}
int choices ( void )
{
printf("N = show number\nEscape = exit\n\n");
return 0;
}
> Don't put too much inside one case though, your program will get confusing.
I agree.
Functions! functions! functions!
Use more function and pointer!!I think it is very important for the speed o fprogram....
People I don't quite get what you mean by function pointers.
Well, I do use a switch statement like this:
/*Start sample*/
void first(void);
void second(void);
void third(void);
...
switch(choice) {
case '1': first(); break;
case '2': second(); break;
case '3': third(); break;
default: break;
}
/*End Sample*/
I don't expand processes inside switch statements.
So what do you think about this style?
Dont know if he actually meant function pointers........but you could do something like...Quote:
Originally posted by billholm
People I don't quite get what you mean by function pointers.
Code:#include <stdio.h>
void first(void){printf("First\n");}
void second(void){printf("Second\n");}
void third(void){printf("Third\n");}
typedef void (*MYFUNC)(void);
int main(int argc, char *argv[])
{
MYFUNC MyFunc = NULL;
int x = 0;
while(x!=4){
printf("Enter 1,2 or 3 (4 to end)\n");
scanf("%d",&x);
if(x == 4)break;
switch(x){
case 1:
MyFunc = (MYFUNC)first;
break;
case 2:
MyFunc = (MYFUNC)second;
break;
case 3:
MyFunc = (MYFUNC)third;
break;
}
MyFunc();
}
return 0;
}
Just for fun......
Code:#include <stdio.h>
void first(void){printf("First\n");}
void second(void){printf("Second\n");}
void third(void){printf("Third\n");}
void (*func[])(void) = { first, second, third };
int main(int argc, char *argv[])
{
int x = 0;
while(x!=4)
{
printf("Enter 1,2 or 3 (4 to end)\n");
scanf("%d",&x);
if(x == 4) break;
if(x > 0 && x < 4) func[x-1]();
}
return 0;
}
Well I didn't know that there are other techniques like those... till today that is. :D
I think I'll practice using those. It could actually shorten some of the codes i have already written. :cool:
In my contest entry, I did a menu based on function pointers, similar to Monsters version, but slightly more dynamic (It worked out the number of items in the menu by itself).
Here's a section of the code that made it work:
The io_GetInt() function is in another library, but all it does is ask the user for an int between 1 and the value in the parameter you pass. If they entered the letter x, the function would return -1, and we'd break out of the infinite loop in menu.Code:struct menu_item
{
char *Text;
void (*fptr) (void *);
void *args;
};
static struct menu_item Page2Menu[] =
{
{ "Search By Chosen Field", ui_DisplayMenu, SearchByFieldMenu },
{ "Display All Entries", ui_DisplayAll, NULL },
{ "Set Sort Order", ui_DisplayMenu, SortByFieldMenu },
{ "Undelete An Entry", ui_UndeleteEntry, NULL },
{ "Save Changes", ui_Save, NULL },
{ "Purge Entries Marked For Deletion", ui_PurgeDeleted, NULL },
{ "Application Options", ui_DisplayMenu, OptionsMenu },
{ NULL, NULL, NULL }
};
struct menu_item MainMenu[] =
{
{ "Add New Entry", ui_AddNewEntry, NULL },
{ "Delete Entry", ui_DeleteEntry, NULL },
{ "Update Entry", ui_UpdateEntry, NULL },
{ "Display Entry", ui_SearchAll, (void *)RECF_ID},
{ "Search By Name", ui_SearchAll, (void *)RECF_NAME },
{ "Save Changes", ui_Save, NULL },
{ "More Options", ui_DisplayMenu, Page2Menu },
{ "Help/About", ui_ShowInfo, NULL },
{ NULL, NULL, NULL }
};
void ui_DisplayMenu(void *pmenu)
{
int i, choice;
struct menu_item *menu = pmenu;
struct menu_item *item;
for ( ; ; )
{
printf ("---> PhoneBook Menu <---\n");
for (i = 1, item = menu; item->Text != NULL; item++, i++)
{
printf("%2d %s\n", i, item->Text);
}
printf(" x Exit Menu\nEnter choice >");
if ((choice = io_GetInt(i-1)) == -1)
break;
item = menu + choice - 1;
if (item->Text == NULL) break;
(void) (*item->fptr) (item->args);
}
}
The full source is in a zip file on the contest thread if you want to look at this in more detail. :)