Thread: Assignment Help: Taking input to convert card notations

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    4

    Assignment Help: Taking input to convert card notations

    I normally do assignments on my own but here's the thing. I'm in a JAVA class and wrote this program in JAVA. Then all of the sudden today my professor goes "oh by the way I actually want this in C". I guess a prereq here for Java students was to take C??? I transferred from another school and haven't taken C so I have no idea where to begin.

    Here is the full assignment:
    Write a program that takes user input describing a playing card in shorthand notation.

    The program can only accept capital letters and can only accept 3 characters max, anything else should print unknown. (For example you can't put in like "00002"
    Example:
    Enter the card notation: QS
    Queen of Spades
    Enter the card notation: 10H
    Ten of Hearts

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    If your professor suddenly decided to change the language required by the course with no warning, then that is most unfortunate. I would advise speaking to them about your situation, and asking if you could do the program in the language for which the class was named.

    Barring that, the only other choice would be to get a C book, or find some tutorials (we have some here, though I would recommend a book over tutorials), and get cracking.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Considering you can write it at home... what's the problem? If you know Java, it shouldn't be any problem for you to write it in Java first, but without OOP (only static methods, static fields, and POJOs). Then just rewrite it to C using a bit of googling...

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I suspect the Java version referred to by the OP was found and not written, as per this similar request posted around the same time.

  5. #5
    Registered User
    Join Date
    Feb 2016
    Posts
    4
    Wowie, you guys are very suspicious people. Anyways, the class is actually an intro to programming class, we have just been learning mostly Java in it. (We have touched on C a few times and this other thing called Processing which is like a graphical program compiler thing, but both have been talked about for like less than 3 classes). ANYWAYS after waiting for an email response from my teacher and much learning I wrote the program.
    (Also it turns out that writing it in C was just for bonus points, but since I've come this far I thought I'd might as well finish it...so yeah I really pay attention well sometimes)

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(){
    #ifdef _WIN32
        setvbuf(stdout, NULL, _IONBF, 0);
        setvbuf(stderr, NULL, _IONBF, 0);
    #endif
    
        // init input string
        char input[3] = "";
    
        char rank[5] = "";
        char suit[8] = "";
    
        int suit_index = 1;
    
        // get input from users and store in input[]
        printf("Enter the card notation: ");
        scanf("%3c", input);
    
    
        // A23456789JQK and 10 for rank
        if(input[0] == 'A'){
            // cpy Ace into rank
          strncpy(rank, "Ace", 5);
        } else if(input[0] == '2'){
             strncpy(rank, "Two", 5);
        } else if(input[0] == '3'){
                strncpy(rank, "Three", 5);
        } else if(input[0] == '4'){
                strncpy(rank, "Four", 5);
        } else if(input[0] == '5'){
                strncpy(rank, "Five", 5);
        } else if(input[0] == '6'){
                strncpy(rank, "Six", 5);
        } else if(input[0] == '7'){
                strncpy(rank, "Seven", 5);
        } else if(input[0] == '8'){
          strncpy(rank, "Eight", 5);
        } else if(input[0] == '9'){
                strncpy(rank, "Nine", 5);
        } else if(input[0] == '1' && input[1] == '0'){
            strncpy(rank, "Ten", 5);
            suit_index = 2;
        } else if(input[0] == 'J'){
            strncpy(rank, "Jack", 5);
        } else if(input[0] == 'Q'){
            strncpy(rank, "Queen", 5);
        } else if(input[0] == 'K'){
           strncpy(rank, "King", 5);
       }
    
        //for debug comment out when turning in assignment
    //    puts("Your card is");
    
    //
         //CDHS for suit
        if(input[suit_index] == 'C'){
         strncpy(suit, "Clubs", 8);
        }
        else if(input[suit_index] == 'H'){
         strncpy(suit, "Hearts", 8);
        }
        else if(input[suit_index] == 'S'){
         strncpy(suit, "Spades", 8);
        }
        else if(input[suit_index] == 'D'){
             strncpy(suit, "Diamonds", 8);
            }
    
        if(strcmp(rank, "")  != 0 && strcmp(suit, "") != 0){
                printf("Your card is %s of %s\n", rank, suit);
            } else {
                printf("Unknown\n");
            }
    
    
    
    
        return 0;
    }
    Go see if you can Google that Matty.


    For anyone willing to actually help, the program works fine but for some reason anything with Diamonds in it adds in some weird special characters at the end. Not sure what it is, all the other suits work fine.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by flapjack1114 View Post
    Wowie, you guys are very suspicious people.
    To be fair, your first post sounded very much like an excuse - coupled with no attempt and no specific questions, it certainly appeared as though you were looking for a handout. This happens quite often here.

    Quote Originally Posted by flapjack1114 View Post
    Go see if you can Google that Matty.
    Tsk tsk.

    Quote Originally Posted by flapjack1114 View Post
    For anyone willing to actually help, the program works fine but for some reason anything with Diamonds in it adds in some weird special characters at the end. Not sure what it is, all the other suits work fine.
    This sounds like a classic case of buffer overrun.

    After a quick look-through, I noticed this:

    Code:
    char suit[8] = "";
    
    // ...
    
    strncpy(suit, "Diamonds", 8);
    Your array can hold 8 characters, but "Diamonds" requires 9 - eight for the word, and one for the string-terminating null character ('\0').

    I suggest you ensure the rest of your buffers are not being overrun in a similar fashion.

  7. #7
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Why are you helping this lying little bonehead?
    Re-read the first post.
    He's obviously asking someone to write the program for him.
    If you google around, you can easily find it written in java.
    Obviously that's what he did.
    He found it in java and was too lazy (or stupid, judging by his code) to translate it to C himself.
    He's cobbled together this program over the last 4 hours after realizing that no one was going to do it for him
    Then he comes back and lies his face off about it.
    What a pathetic little loser.

  8. #8
    Registered User
    Join Date
    Feb 2016
    Posts
    4
    Okay you have redeemed yourself, sorry for being mean.
    By what you are saying, doesn't that mean that the anything with "7" should encounter the same issue?

  9. #9
    Registered User
    Join Date
    Feb 2016
    Posts
    4
    Quote Originally Posted by algorism View Post
    Why are you helping this lying little bonehead?
    Re-read the first post.
    He's obviously asking someone to write the program for him.
    If you google around, you can easily find it written in java.
    Obviously that's what he did.
    He found it in java and was too lazy (or stupid, judging by his code) to translate it to C himself.
    He's cobbled together this program over the last 4 hours after realizing that no one was going to do it for him
    Then he comes back and lies his face off about it.
    What a pathetic little loser.
    It's intro to program logic, thx 4 input tho

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by flapjack1114 View Post
    By what you are saying, doesn't that mean that the anything with "7" should encounter the same issue?
    I'm not sure what you mean by "anything with 7".

    Just make sure each of your arrays is at least large enough to hold the longest possible string for that array, plus one. Also ensure you're copying enough bytes with "strncpy()".

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    @algorism: While I don't feel it's necessary to explain to you my reasons for helping the OP, I chose to do so because I did detect some effort on their part, even if was "cobbling together" a program. I obviously suspect the same things as you (if you re-read posts #2 and #4, you'll see this), but I generally try to avoid condemning someone without hard evidence of outright cheating.

    Besides, if their approach to programming assignments is to hack up found code, then their academic career is doomed to be short-lived anyway.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Taking input from text file..
    By Passa in forum C Programming
    Replies: 4
    Last Post: 09-14-2010, 06:12 PM
  2. Help with taking input from a file
    By babe20042004 in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2009, 08:25 AM
  3. Taking input in C
    By GUIPenguin in forum C Programming
    Replies: 1
    Last Post: 04-12-2006, 01:53 PM
  4. Taking input while calculating
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 07-12-2002, 04:47 PM
  5. Taking Voice input
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 01-30-2002, 01:34 PM