Help Needed please!!

This is a discussion on Help Needed please!! within the C++ Programming forums, part of the General Programming Boards category; Hey everyone, I have got this question in an assignment about a simplified SNAP card game. I have spent far ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    3

    Help Needed please!!

    Hey everyone, I have got this question in an assignment about a simplified SNAP card game. I have spent far too much time on this assignment and I still have not found a solution. Apparently the program should print SNAP and then finish if two identical values of two cards are put one after other.

    The question is as follows:

    Write a program to play a simplified form of the card game 'Snap'. The user inputs a card using a string of two characters e.g. 3D is the 3 of diamonds, JS is the jack of spades. (the suits are H C D and S, the values are 23456789TJQKA). Each card will be input on a line. When two cards of the same value are input one after the other, the program should respond with SNAP, and then finish.
    e.g. Type in your cards:
    3S
    KH
    AH
    7C
    7S
    SNAP

    This is what I have done so far, but I am still stuck and the program is not working as I want it to. I am not sure if I am taking the right steps or not. Please can any one help me, any help would be greatly appreciated.

    Code:
    #include <stdio.h>
    #include <strng.h>
    
    char card1[1], card2[1];
    
    
    int main() {
    printf("Type in card1: ");
    strcpy(card1, "23456789TJQKA");
    gets(card1);
    printf("Type in card2: ");
    strcpy(card2, "HCDS");
    gets(card2);
    while(card1!=card2) {
    printf("Type in card1: ");
    gets(card1);
    printf("Type in card2: ");
    gets(card2);
    while(card1==card2) {
    printf("SNAP\n");
    }
    }
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    > #include <strng.h>
    Is this your real code or just something you typed in for the fun of it?
    Because this isn't a proper header file, so this code won't compile.

    > char card1[1], card2[1];
    Count how many characters you type in, and pick a suitably sized array to match that.
    Don't forget that strings have an extra \0 at the end.

    > strcpy(card1, "23456789TJQKA");
    What does this have to do with what the user types in?

    > gets(card1);
    See SourceForge.net: Gets - cpwiki

    > while(card1!=card2)
    You can't compare strings using == or != in C, you need to use strcmp
    But note that your assignment only cares about the rank of the card (2,3 etc) not the suit.

    You should also read this -> SourceForge.net: Indentation - cpwiki
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    3
    Hey mate thanx for your reply. I have no idea how this got posted in here, I definitely posted somewhere else because I wasn't able to find the Post new thread button hah.
    How do I compare only the first character of the string with another character inside a string?

    Appreciated

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    The post is here, because you posted it on the end of someone else's thread, and I moved it here.

    You compare individual chars in an array with a subscript
    if ( array1[pos1] == array2[pos2] )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help needed again
    By Vikramnb in forum C++ Programming
    Replies: 7
    Last Post: 01-09-2009, 01:03 PM
  2. Help needed!!
    By jawahar in forum C++ Programming
    Replies: 4
    Last Post: 01-22-2008, 01:57 AM
  3. Help needed here please!!!!!!
    By ben2000 in forum C Programming
    Replies: 3
    Last Post: 08-18-2007, 11:50 AM
  4. Help Needed !!
    By vodlum in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2005, 02:33 PM
  5. Help needed.
    By hyrule in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2005, 09:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21