Like Tree1Likes
  • 1 Post By tabstop

Simple Question

This is a discussion on Simple Question within the C Programming forums, part of the General Programming Boards category; Code: //Ant, Bear, Cat, Dog, Emu, Goat, Lizard, Monkey, Parrot, Rabbit, Snake, and Tiger// #include <stdio.h> #include <stdlib.h> #include <time.h> ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    12

    Simple Question

    Code:
    //Ant, Bear, Cat, Dog, Emu, Goat, Lizard, Monkey, Parrot, Rabbit, Snake, and Tiger//
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    char LetterSwap(char a)//swaps invalid randomed char to other valid which wasn't randomed out//
    {    if (a=='F') a='M';
         else if (a=='H') a='P';
         else if (a=='I') a='R';
         else if (a=='J') a='S';
         else if (a=='K') a='T';
         return a;
         }
    
         
    int main(){
        char rand1='0',rand2='0',rand3='0',rand4='0';
        char a,b,c,d;
        char diff;
        int times,guessnumber=1;
        
        
        while (rand1==rand2 ||rand1==rand3 || rand1==rand4 ||rand2==rand3||rand2==rand4||rand3==rand4)//Equal randoms to be changed//
        { 
              srand(time(NULL));
              rand1=(rand()%12)+'A';
              rand2=(rand()%12)+'A';
              rand3=(rand()%12)+'A';
              rand4=(rand()%12)+'A';
              }
        rand1=LetterSwap(rand1);//uses functions that swaps invalid to unrandomed characters//
        rand2=LetterSwap(rand2);
        rand3=LetterSwap(rand3);
        rand4=LetterSwap(rand4);
        printf("%c-%c-%c-%c\n",rand1,rand2,rand3,rand4);
        printf("MASTERMIND GAME\n\nI got a secret code\n");
        printf("Please select level of difficulty(L-low, H-high):");
        scanf("%c",&diff);
        if(diff=='L') printf("You want a low level of difficulty\n");
        else if (diff=='H') printf("Excellent, you want a high level of difficulty\n");
        else{
             printf("INVALID!");
             goto Done;}
        for(times=0;times<=10;times++)
        {
             printf("Enter guess%d:",guessnumber);
             scanf("%c %c %c %c",&a,&b,&c,&d);
            if (a==b||a==c||a==d||b==c||b==d||c==d)
            printf("Repeated");
            
      
                }
             Done: system("pause");
             return 0;}



    This is unfinished. I can't move on till i finish the looping part..It gives me a problem . When Testing it..Some outputs correct then suddenly it will not print "Repeated" even though there is a repeated letter. THANKS!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You forgot the space before the first %c in your scanf string.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    12
    I guess that's it..it's been working good now. Thanks..but..why was the space a big thing @_@

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Because enter-key is a perfectly wonderful character, and so you were reading it in as one of your characters (the space prevents it).
    Matticus likes this.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,798
    You probably want to move the srand call to just before your while loop.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. really simple question
    By JezW in forum C Programming
    Replies: 8
    Last Post: 04-25-2009, 07:25 PM
  2. simple question
    By tat in forum C Programming
    Replies: 9
    Last Post: 10-18-2006, 08:47 PM
  3. simple simple design question
    By Chaplin27 in forum C++ Programming
    Replies: 6
    Last Post: 05-31-2005, 11:33 PM
  4. Simple Question(maybe)
    By CC4CocoaPuffs in forum C++ Programming
    Replies: 10
    Last Post: 10-07-2004, 09:13 PM
  5. A simple question
    By Kelvin in forum C Programming
    Replies: 6
    Last Post: 07-08-2002, 02:35 PM

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