-3

I'm attempting to make a simple game-like-program that checks if the character entered through the keyboard (under a time limit of three seconds) is same as randomly generated output or not . I'm using TC++(latest edition).

PLEASE TAKE A LOOK AT THE CODE BEFORE READING THE FOLLOWING.

The second time, no matter what character I enter(Right or Wrong) the program says that the character entered is wrong. It doesn't goto scanf (maybe) because it prints "Q" and then the "OOPS" line
------------------------------------CODE-------------------------------------

#include <stdio.h>
#include<conio.h>
#include<time.h>


void main()
{
    int count , sec;
    char output, input;
    clock_t start, end;

    clrscr();

    restart:
    count = 0;
    
    next:
    //Generating random letters and symbols: ASCII characters from A-z 
    //Set output to A
    output = 'A';
    //Adding a value from 1-57 to get a random ASCII character from A-z
    output += rand() % 57;
    printf("%c\n",output);

    start = clock();
    scanf("%c",&input);
    end = clock() - start;
    sec = end / CLK_TCK;
    //Time limit check (Time in seconds)
    if(sec >= 3)
    {
        printf("\nYou ran out of time.");
        goto endgame;
    }

    if(input == output)
    {
        count++;
        goto next;
    }

    else
    {
        printf("\nOOPS! You are wrong\n\nYour Score is %d",count*10);
        goto endgame;
    }

    endgame:

    printf("\nDo you want to restart?(Y/N)");
    scanf("%c",&input);

    if(input == 'y' || input == 'Y')
    {
        goto restart;
    }

    getch();
}


    

OUTPUT:
    E
    *I enter E*
    Q
    *CONSOLE DOESN'T WAIT FOR ME TO ENTER ANYTHING*
    OOPS! You are wrong.
    
    Your score is 10.
    Do you want to restart(Y/N)?

Community
  • 1
  • 1
Xtreme
  • 31
  • 5
  • 3
    The second iteration probably is scanning the `\n` you entered in your first iteration. – jxh Jul 17 '17 at 18:29
  • 2
    Why is that question tagged with c++? Why are you yelling in bold to us? Why didn't you inspect your code with the debugger before asking here? – user0042 Jul 17 '17 at 18:30
  • 1
    AAAARGH so many goto! https://stackoverflow.com/questions/3517726/what-is-wrong-with-using-goto – litelite Jul 17 '17 at 18:31
  • Instead of goto, use loops http://www.cprogramming.com/tutorial/c/lesson3.html – litelite Jul 17 '17 at 18:32
  • @jxh it..doesnt scan \n, not with this code(tried removing it) – Xtreme Jul 17 '17 at 18:34
  • @xing There is no " %c"(no spaces there buddy) :) – Xtreme Jul 17 '17 at 18:35
  • note too that you say `c compiler doesn't.....`. The c compiler is long in the past when you run your code. I think you need a beginning `c` book before you go any further. – KevinDTimm Jul 17 '17 at 18:48
  • @Xtreme: Since it is apparent you are still a student, I did not provide you with a complete answer, and instead provided a comment with a hint. Instead of spending time to figure out how to use the hint, you made an assumption, tested it, and accused me of not providing a helpful hint. Your question has already demonstrated you are struggling with troubleshooting your own program. So instead of wasting time on bad assumptions, perhaps you should spend more time trying to use the helpful advice being provided. Good luck with your assignment! – jxh Jul 17 '17 at 19:06
  • By "TC++(latest edition)" do you mean Turbo C++? If so, are you using the 2006 edition (looks like a modern windows program) or the 1990's version (blue and yellow boxy interface) – user4581301 Jul 17 '17 at 20:01
  • @xing thanks, didnt see that one coming really, – Xtreme Jul 18 '17 at 02:11
  • @jxh thanks to you too, n im naive so..nvm..don't go easy – Xtreme Jul 18 '17 at 02:11
  • @xing can you please suggest a compiler equivalent to turbo C++ , or atleast similar...im tired of the GUI...tho it duznt matter – Xtreme Jul 18 '17 at 02:13

1 Answers1

1

Thanks to @xing and @jxh i got my silly mistake.. The console was reading "\n" too aparently, scanf(" %c"); space before %c did the trick, THANKS IN CAPS!

Xtreme
  • 31
  • 5