Thread: Programming Question

  1. #1
    Registered User Fool's Avatar
    Join Date
    Aug 2001
    Posts
    335

    Programming Question

    I'm trying to code a simple adding program. How can I do this? Just asking questions (IE: "What's 1+1?" and gives the user a chance to input the number. Then bring up another question after the answer is correct.)

    -Fool

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    Here's a sample program. Hope it helps.

    Code:
    #include <cstdio>
    #include <cstdlib>
    #include <ctime>
    
    int main()
    {
        int n1, n2, answer;
    
        // Let's seed the random number generator with the current time
        // This is done to insure that every time the program is run,
        // it will have different questions.
        srand((unsigned)time(NULL));
    
        // An infinite loop
        for(;;)
        {
            // First let's generate two random numbers between 1 and 10
            n1 = rand() % 10 + 1;
            n2 = rand() % 10 + 1;
    
            // Ask the user for the answer and recieve it in the answer variable
            printf("What is %d + %d? (To quit type 0)\n", n1, n2);
            scanf("%d", &answer);
    
            // If the user typed 0, exit the loop and the program
            if(answer == 0)
                break;
            // Check for correct answer
            else if(answer == n1 + n2)
                printf("This is correct, %d + %d = %d!\n", n1, n2, answer);
            else
                printf("No, %d + %d = %d, not %d. Try again.\n", n1, n2, n1 + n2, answer);
        }
    
        return 0;
    }
    - lmov

  3. #3
    Registered User Fool's Avatar
    Join Date
    Aug 2001
    Posts
    335
    Wow, thanks!

    -Fool

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    How did you get the syntax highlighting like that lmov? Did you write some code to add HTML to code to make it look so nice?
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  5. #5
    Registered User Fool's Avatar
    Join Date
    Aug 2001
    Posts
    335
    lmov,

    Now that I've had a second to really look at it I understand it better. The only thing that throws me off is I wouldn't have known the commands. But the logic makes sence to me. Thanks again for the help.

    SilentStrike,

    I think he used a program like MSVC or DevC++ that will do that for you.

    -Fool

  6. #6
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    I think he used a program like MSVC or DevC++ that will do that for you.
    It wont preserve the colours when transfering to code to the board. He went to Sunlight's site and downloaded the code formater.

    You have to modify the source though, as the original was written for the Programers Heaven board.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM