1

I am trying to create a program that prints a random number whenever the user enters "roll", and allows the user to enter "1" if the random number is greater than or equal to 3.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
    int randomnumber;
    char diceinput;
    int slotnumber;
    char lettertable[7];
    char *character;

    lettertable[1] = 'l';
    lettertable[2] = 'r';
    lettertable[3] = 't';
    lettertable[4] = 'e';
    lettertable[5] = 't';
    lettertable[6] = 'e';

    character = &lettertable[1];

    printf("Enter 'roll' to roll the dice. Enter 'exit' to quit.\n");

    scanf("%s", &diceinput);

    if (strcmp(&diceinput, "exit")==0) {        
        printf("Now quitting. Thank you, and please play again!\n");        
    }

    if (strcmp(&diceinput, "roll")==0) {        
        srand((unsigned)time(NULL));
        randomnumber = rand() % 6 + 1;
        printf("%d\n", randomnumber);

        if (randomnumber >= 3) {            
            printf("Enter 1 to get the corresponding letter from table.\n");
            scanf("%d", &slotnumber);

            if (slotnumber == 1) {                
                printf("%s", character);                
            }
        }
    }
 }

After the user enters "1" the program is supposed to get the letter stored in the letter table[1], an element in the array lettertable. However, when I run the program and enter "1", instead of getting the letter "l", the output is a weird phrase: "lrtete" with an upside down question mark. Can somebody please help me? Thank you.

Please note that the code shown above is only a revelant section of the unfinished program.

Nisarg
  • 397
  • 1
  • 5
  • 14
  • diceInput is a char but needs to be an array char diceInput[80]; Don't pass a pointer to diceinput for the scanf you don't need to alter where the array is -- just the data inside the array. – dcaswell Sep 06 '13 at 19:55
  • 2
    Please learn how to indent consistently. It makes it easier to read and understand the code. Using a one space indent is too small in my view. SO encourages 4 space indents by mapping tabs to 4 spaces in display; it is a reasonable balance between indenting enough to be clear and not overrunning the line length. – Jonathan Leffler Sep 06 '13 at 19:58
  • 1
    Also, you print 'Now quitting' but your code does not then quit. And there doesn't seem to be a loop in there. – Jonathan Leffler Sep 06 '13 at 19:59

4 Answers4

4

Try this

if (slotnumber == 1) 
{
        printf("%c", *character);
}

Also as pointed by Jonathan Leffler your code does not quit for if (strcmp(&diceinput, "exit")==0). You should use exit(0) to quit;

 if (strcmp(&diceinput, "exit")==0) 
 {
      printf("Now quitting. Thank you, and please play again!\n");
      exit(0);
 }
Community
  • 1
  • 1
haccks
  • 104,019
  • 25
  • 176
  • 264
3

if you're trying to output one letter try replacing printf("%s" , character) with printf("%c" , *character)

Farouq Jouti
  • 1,657
  • 9
  • 15
2

The printf function will print what it finds at the location provided and will stop printing when it finds a binary zero. Your problem is here:

printf("%s", character);

Since the second element of the array is not zero it will keep printing until it happens to find a zero. In your case you were lucky and it found one before printing lots of garbage.

Jay
  • 13,803
  • 4
  • 42
  • 69
  • printf("%c", character); There's an excellent reference here: http://www.cplusplus.com/reference/cstdio/printf/ – Jay Sep 06 '13 at 20:01
  • I know that...but does the person reading your answer know that? Just a suggestion on how to improve your answer. – Jonathan Leffler Sep 06 '13 at 20:25
  • 1
    I got the answer, but you provided a nice explanation for the printf function in the program. Thanks, Jay. – Nisarg Sep 06 '13 at 21:21
  • 1
    @Nisarg you should accept an answer that answers your question (no offense jay) because otherwise people having the same problem as you can get confused – Farouq Jouti Sep 06 '13 at 21:24
  • @Lorenzo Donati Thanks, response should have been printf("%c", *character); – Jay Sep 12 '13 at 14:55
2

Use %c as in printf("%c", *character); instead of %s in printf("%s", character);

You have a few logical mistakes in your code:

1) char diceinput; and then scanf("%s", &diceinput);, this is a big mistake, your program may crash as you don't have sufficient memory allocated (neither statically nor dynamically) for the input string. Use char diceinput[5]; and scanf("%s", diceinput);

2) You have started indexing from 1. (Pardon me if its intentional)

3) You are using printf("%s", character);, I will say its your luck that you are getting "lrtete" as output, i.e. it may not stop at ..te and print some garbage characters after, because there is no explicit \0 character at the end of character array.

0xF1
  • 6,046
  • 2
  • 27
  • 50
  • "Use `%c` instead of `%s` in `printf("%s", character);`", not completely correct: `character` is a `char *`, it must be dereferenced as well. – LorenzoDonati4Ukraine-OnStrike Sep 06 '13 at 20:14
  • @LorenzoDonati : Thanks for pointing that out, I pointed out his mistakes, but forgot mine. :-) – 0xF1 Sep 06 '13 at 20:18
  • Thank you for providing a great explanation. Just to let you know, I started indexing from 1 on purpose for the sake of the user. Again, thanks. – Nisarg Sep 06 '13 at 21:10