-4

Please help me with this obviously silly mistake

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<ctime>

int main()
{

    srand((unsigned)time(NULL));
    int No_of_Q = 0;            //number of questions 
    int m1, m2, ans;            //two operands and answer
    int ra = 0;                 //number of right answers
    int wa = 0;                 //number of wrong answers
    char c = 'y';               //whether to continue

    printf("Let's play multiply!\n");
    do 
    {
        printf("How many questions would you like to attempt? ");
        scanf_s("%d", &No_of_Q);
        printf("\n");

        for(int i=1; i<=No_of_Q; i++)
        {
            m1=(rand()%10 + 1);
            m2=(rand()%10 + 1);

            printf("\nWhat is %d x %d = ", m1, m2);
            scanf_s("%d",&ans);

            if(ans== m1*m2)
            {
               printf("Your answer is correct!\n");
               ra++ ;
            }
            else{
            printf("Wrong, the correct answer is %d.\n", m1*m2);
            wa++ ;
            } 
        }
        printf("\n\nOut of %d answers, %d were correct and %d wrong.\n",No_of_Q, ra, wa);
        if(wa==0)
            printf("Congratulations!\n");
        else
            printf("Better luck next time!\n");
        printf("Continue game? ");
        scanf_s("%c", &c );      /*-------CANNOT GET THIS TO PERFORM--------------*/
        //printf("%c",c);
    }
      while(c=='y');

      return 0;
}

I am using vs 2012 express edition. I cannot get my program to scan answer to continue at the end of the program. The while statement at the end doesn't compute. Please help.

  • First of all your question is not clear.You need to post the error .This is the reason no one trying to give you the solution. – Madan Ram Aug 11 '13 at 11:48
  • See, for example, [`scanf()` won't ask for input the second time](http://stackoverflow.com/questions/13372923/scanf-wont-ask-for-input-the-second-time) as one of many similar questions. – Jonathan Leffler Aug 11 '13 at 11:54
  • 1
    see http://msdn.microsoft.com/en-us/library/w40768et%28v=vs.80%29.aspx `scanf_s` need for `%s`, `%c` Eg `scanf_s("%c", &c, 1);` – BLUEPIXY Aug 11 '13 at 11:58
  • The title is very unclear, try to modify it to reflect the problem. – Yu Hao Aug 11 '13 at 12:32

2 Answers2

0

The problem is that your line scanf_s("%d", &ans); reads up to but not including the newline after the number. The scanf_s("%c", &c); then reads the newline, which is not 'y', so the loop fails.

This is why you will find many people (myself included) recommended reading the response with fgets() and then converting the input with sscanf_s().

You should also check that scanf_s() returns the correct value (1 for the two calls mentioned) — or that fgets() and sscanf_s() return the correct values (fgets(line, sizeof(line), stdin) != 0 and sscanf_s(...) == 1)).

You should also note that #include <ctime> is C++ header; the C header is #include <time.h>. I don't see any use of #include <conio.h>, so you could omit that.


Additionally, you are mis-calling scanf_s() when using the %c conversion. As BluePixy points out in a comment, you should be passing a length of 1 after the &c:

if (scanf("%c", &c, 1U) != 1)
    ...oops...

The C11 (ISO/IEC 9899:2011) Annex K covers the Bounds-checking Interfaces and says much the same.

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0

Use scanf_s("%c", &c ); function two times instead of one. The frist one will get the '\n' character and then it will be replaced by your input.

printf("Continue game? ");
scanf_s("%c", &c );      /*-------WILL GET '\n'--------------*/
scanf_s("%c", &c );      /*-------WILL ASK FOR INPUT--------------*/
Vinod Kumar
  • 479
  • 5
  • 12