Thread: Really simple first C program

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    2

    Really simple first C program

    Hi there was hoping someone might be able to help me out. It's very easy my first C assignment however I'm stuck. It's supposed to train a kid how to do their multiplication tables. I can't figure out how to recognise an incorrect answer. This is what I have below but it won't compile as their is one error "identifier user_answer"

    [code]
    #include <stdio.h>
    main()
    {
    int times_tables, correct_answer,user_answer;
    int counter = 1;
    int correct_answer_counter = 0;
    int incorrect_counter = 0;

    printf("Please enter the times tables you wish to be tested on between 1-12\n");
    scanf("%d",&times_tables);
    printf("You are now going to be tested on your %d times tables\n",times_tables);
    correct_answer = times_tables * counter;
    for (user_answer=times_tables*counter;counter < 13; counter++)/*This is where our loop begins.*/
    {
    printf("%d * %d=\n" ,times_tables,counter);
    scanf("%d",&user_answer);
    if user_answer = times_tables*counter;
    printf("This is not the right answer please try again");
    incorrect_counter++;

    }



    }
    [code/]

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Fix your code tags
    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.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    I made a few corrections.

    Code:
    #include <stdio.h>
    
    int main()
    {
    int times_tables, correct_answer,user_answer;
    int counter;
    int correct_answer_counter = 0;
    int incorrect_counter = 0;
    
    printf("Please enter the times tables you wish to be tested on between 1-12\n");
    scanf("%d", & times_tables);
    printf("You are now going to be tested on your %d times tables\n",times_tables);
    
    for (counter=1;counter < 13; counter++)
    {
    printf("%d * %d=" ,times_tables,counter);
    scanf("%d",&user_answer);
    correct_answer = times_tables * counter;
    if (user_answer != correct_answer)
    {
        printf("This is not the right answer please try again\n");
        incorrect_counter++;
        counter--;
    }
    }
    
    printf("Incorrect Answers: %d", incorrect_counter);
    }

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    2
    Salem,

    Sorry about the code tags.

    Dr Zoidberg,

    Thank you very much. I know it seems very easy to you guys but I was getting a little fustrated that it wasn't working out. Much appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a very simple program
    By htdefiant in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2007, 01:27 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  4. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM