Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [C-tut] WIP - Whirlwind Tour of C
Index -> Programming, C -> C Tutorials
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Tony




PostPosted: Wed Jul 09, 2014 8:26 pm   Post subject: RE:[C-tut] WIP - Whirlwind Tour of C

By "efficient" I assume you mean "less characters to type". In such a case, you can check if an element is included in a set.
Quote:

irb(main):001:0> ['a','b','c'].include?('a')
=> true
irb(main):002:0> ['a','b','c'].include?('d')
=> false
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Wed Jul 09, 2014 8:35 pm   Post subject: RE:[C-tut] WIP - Whirlwind Tour of C

Is this viable in C though? Sure, you could draw up a function that operates on an array to do the same thing, but then things might start to get messy.
Srlancelot39




PostPosted: Wed Jul 09, 2014 8:49 pm   Post subject: Re: [C-tut] WIP - Whirlwind Tour of C

Okay, thanks!

Also though, the following code does not seem to be working for me:
c:
char menuchoice = NULL;
        do
        {
                printf("(A)dd a Student\n");
                printf("(C)hange a Student's Information\n");
                printf("(D)elete a Student\n");
                printf("(L)ist All Students\n");
                printf("(Q)uit\n");
                printf("\nEnter letter corresponding to option...");
                fflush(stdin);
                getch(menuchoice);
                if (menuchoice != 'A' && menuchoice != 'C' && menuchoice != 'D' && menuchoice != 'L' && menuchoice != 'Q' && menuchoice != 'a' && menuchoice != 'c' && menuchoice != 'd' && menuchoice != 'l' && menuchoice != 'q')
                {
                        printf("\n\nError: Invalid entry!");
                        getch();
                        system("cls");
                }
                else break;
        } while (1);


Even when a "valid" character is entered, it still executes the error message. What am I doing wrong?
Tony




PostPosted: Wed Jul 09, 2014 8:51 pm   Post subject: RE:[C-tut] WIP - Whirlwind Tour of C

Depending on specifics, using bitfields might work very well in C. Given that every option is represented by a unique bit, then you can OR a bunch of options to create a "set" and AND against that "set" to check for inclusion.

Ruby example, but idea is the same for C:
Quote:

irb(main):001:0> A,B,C,D = [1,2,4,8]
=> [1, 2, 4, 8]
irb(main):002:0> D & (A|B|C)
=> 0
irb(main):003:0> A & (A|B|C)
=> 1
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Srlancelot39




PostPosted: Wed Jul 09, 2014 9:25 pm   Post subject: Re: [C-tut] WIP - Whirlwind Tour of C

Srlancelot39 @ Wed Jul 09, 2014 8:49 pm wrote:
Also though, the following code does not seem to be working for me:


Nevermind!

I solved it by using
c:
menuchoice = getch();
instead of
c:
getch(menuchoice);


Smile
Display posts from previous:   
   Index -> Programming, C -> C Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 2  [ 20 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: