-1

I am having trouble with my syntax for calling srand to insert random numbers for an addition problem?? tried looking up a similar program to view syntax with no luck.

#include<stdio.h>
#include"simpleio.h"

 int main() 
{    
   //  int seed;scanf("%d",&seed);
   //  srand((unsigned)seed);

       int decision,n,o ;
     // scanf("%d", &n);
     // srand((unsigned)n);   


    do{
       printf("1.Give me an addition problem.\n"
              "2.Give me a subtraction problem.\n"
              "3.Give me a multiplication problem.\n"
              "4.Quit\n");
       scanf("%d", & decision);
      } while (decision>5 && decision<0);

    if (decision==1) 
    {
      printf("1+1= ");
    }
    else if (decision==2) 
    {
      printf("1-2\n");   
    }
    else if (decision==3) 
    {
      printf("1*2\n");
    }
    else if(decision==4) 
    { 
       printf("\n");
    } 

return 0;
}
chouaib
  • 2,763
  • 5
  • 20
  • 35
cyerocks
  • 1
  • 2
  • 1
    `decision>5 && decision<0` always false. change to `decision>4 || decision<1` – BLUEPIXY Sep 18 '14 at 23:00
  • 1
    You haven't told us what problem you're having. Your `srand` calls are commented out. Your code compiles with or without them (once I delete the unused `#include"simpleio.h"`). You should only call `srand()` once, before making any calls to `rand()`. And if you're reading the seed from the user, you might as well declare the seed as `unsigned` so you don't have to convert it. What exactly are you asking? – Keith Thompson Sep 18 '14 at 23:01
  • I dont know how to use srand to insert two random integers in the addition problem keith. my problem is setting up the srand call, I've never used this call before.pls dont laugh, yes i am fairly new to programming and I would like some information on how to use and setup srand to perform the action of placing two random integers to be added.can u help me out on the set upand briefly explain keith ? – cyerocks Sep 18 '14 at 23:18
  • I commented that portion of syntax because i was not particularly sure that the placement of the syntax belonged there. – cyerocks Sep 18 '14 at 23:19
  • 1
    `srand()` only seeds the random number generator. You call `rand()` to retrieve random number from it. But beware, there are lots of pitfalls available if you are moving beyond simple arithmetic problem generation. The generator implemented by `srand()` and `rand()` is *not* suited for must serious uses of random numbers. In any case, `rand()` is suited to this use case, as long as you only call `srand()` *once* per execution of the program. – RBerteig Sep 18 '14 at 23:56
  • @RBerteig: While your assumption is probably correct, `srand()` is called to seed the random number generator. Some programs might want to have the same random sequence produced, so they might well call `srand()` several times per program execution. – wallyk Sep 19 '14 at 00:23
  • 1
    @wallyk I'm just heading off the standard new user mistake of calling `srand` once per call to `rand` to "make sure it got seeded". A user who knows about using a seed a second time to get the same sequence would not be asking *this* question.... An example of (correctly) calling it more than once would be in a level generator for a maze game. You might seed it once per level so that the game can be restarted on that level, and reduce the state storage for the basic maze to essentially a single seed value. But for the OP's flash card simulation, once per run and seeded from time() is good. – RBerteig Sep 19 '14 at 00:28
  • @cyerocks: If you want me to see your comment, you need to include my name preceded by an `@` symbol (there are several examples in other comments here). Read the [comp.lang.c FAQ](http://www.c-faq.com/), starting at question 13.15. – Keith Thompson Sep 19 '14 at 00:38

1 Answers1

0

In similar tasks, it is preferable to do it with switch case :

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

main()
{
    int decision;

    do{
       printf("1.Give me an addition problem.\n"
              "2.Give me a subtraction problem.\n"
              "3.Give me a multiplication problem.\n"
              "4.Give me a division problem.\n"
              "5.Quit\n");
       scanf("%d", & decision);
      } while (decision >5 || decision <= 0); // correct your condition

    // generate two random numbers for the operation
    srand(time(NULL));
    int a = rand()%20 + 1; // random between 1 and 20
    int b = rand()%20 + 1; // random between 1 and 20

    switch(decision)
    {
        case 1: //addition
        printf("%d + %d = %d\n", a, b, a+b);
        break;

        case 2: //subtraction
        printf("%d - %d = %d\n", a, b, a-b);
        break;

        case 3: //multiplication
        printf("%d * %d = %d\n", a, b, a*b);
        break;

        case 4: //division
        printf("%d / %d = %d, and remainder = %d\n", a, b, a/b, a%b);


        default :
        break;
    }

    return;
}

I included the division as a bonus

chouaib
  • 2,763
  • 5
  • 20
  • 35