randomization in c++?

This is a discussion on randomization in c++? within the C++ Programming forums, part of the General Programming Boards category; hi i wrote a trivia program but i want to improve it by randomizing the questions in it. but how ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    2

    randomizing ?

    hi i wrote a trivia program but i want to improve it by randomizing the questions in it. but how do i do it? thanks
    Code:
    main()
    {
    	int l;
    	clrscr();
    	gotoxy(30,7);printf("[1] QUESTIONS\n");
    	gotoxy(30,9);printf("[2] QUIT\n\n");
    	gotoxy(30,13);printf("Choose:  ");
    	scanf("%d",&l);
    	if(l==1)
    	easy();
    	else if(l==2)
    	exit();
    	}
    easy()
    {
    	int x;
    	char d[5];
    	int i=0;
    	char a[]="a";
    	char b[]="b";
    	char c[]="c";
    	for(x=0;x<=3;x++)
    	{
    	clrscr();
    	gotoxy(10,5);printf("Direction: Choose the letter of the correct answer.");
    	gotoxy(10,7);printf("1. What two letters are both symbols for 1,000?");
    	gotoxy(10,9);printf("%s. K and M.",a);
    	gotoxy(10,11);printf("%s. A and B.",b);
    	gotoxy(10,13);printf("%s. L and D",c);
    	gotoxy(10,15);printf("Answer: ");gets(d);
    	if(strcmp(d,a)==0)
    	{
    	gotoxy(12,20);printf("Correct!!!\n");
    	i=i+1;
    	gotoxy(12,21);printf("Your score is %d",i);
    	x=3;
    	getch();
    	}
    	if(strcmp(d,a)!=0)
    	{
    	gotoxy(12,20);printf("Wrong!!!\n");
    	gotoxy(12,21);printf("Life[%d]",x);
    	getch();
    	if(x>=3)
    	{
    	main();
    	}
    	}
    	}
    	for(x=1;x<=3;x++)
    	{
    	clrscr();
    	gotoxy(10,7);printf("2. What's short for Binary Digit?");
    	gotoxy(10,9);printf("%s. BD.",a);
    	gotoxy(10,11);printf("%s. Bit.",b);
    	gotoxy(10,13);printf("%s. Digit.",c);
    	gotoxy(10,15);printf("Answer: ");gets(d);
    	if(strcmp(d,b)==0)
    
    	{
    	gotoxy(12,20);printf("Correct!!!\n");
    	i=i+1;
    	gotoxy(12,21);printf("Your score is %d",i);
    	x=3;
    	getch();
    	}
    	else
    	{
    	gotoxy(12,20);printf("Wrong!!!\n");
    	gotoxy(12,21);printf("Life[%d]",x);
    	getch();
    	if(x>=3)
    	{
    	main();
    	}
    	}
    	}
    	for(x=1;x<=3;x++)
    	{
    	clrscr();
    	gotoxy(10,7);printf("3. What digit did Arab mathematician al-Khwarizmi give to the West\n            around 800 B/B.?");
    	gotoxy(10,10);printf("%s. Zero",a);
    	gotoxy(10,12);printf("%s. One",b);
    	gotoxy(10,14);printf("%s. Two",c);
    	gotoxy(10,16);printf("Answer: ");gets(d);
    	if(strcmp(d,a)==0)
    
    	{
    	gotoxy(12,20);printf("Correct!!!\n");
    	i=i+1;
    	gotoxy(12,21);printf("Your score is %d",i);
    	x=3;
    	getch();
    	}
    	else
    	{
    	gotoxy(12,20);printf("Wrong!!!\n");
    	gotoxy(12,21);printf("Life[%d]",x);
    	getch();
    	if(x>=3)
    	{
    	main();
    	}
    	}
    	}
    	for(x=1;x<=3;x++)
    	{
    	clrscr();
    	gotoxy(10,7);printf("4. What word describes a number system with a base of two?");
    	gotoxy(10,9);printf("%s. Decimal",a);
    	gotoxy(10,11);printf("%s. Octal",b);
    	gotoxy(10,13);printf("%s. Binary",c);
    	gotoxy(10,15);printf("Answer: ");gets(d);
    	if(strcmp(d,c)==0)
    
    	{
    	gotoxy(12,20);printf("Correct!!!\n");
    	i=i+1;
    	gotoxy(12,21);printf("Your score is %d",i);
    	x=3;
    	getch();
    	}
    	else
    	{
    	gotoxy(12,20);printf("Wrong!!!\n");
    	gotoxy(12,21);printf("Life[%d]",x);
    	getch();
    	if(x>=3)
    	{
    	main();
    	}
    	}
    	}
    	for(x=1;x<=3;x++)
    	{
    	clrscr();
    	gotoxy(10,7);printf("5. How many equal sides does an icosahedron have?");
    	gotoxy(10,9);printf("%s. Ten",a);
    	gotoxy(10,11);printf("%s. Twenty",b);
    	gotoxy(10,13);printf("%s. Thirty",c);
    	gotoxy(10,15);printf("Answer: ");gets(d);
    	if(strcmp(d,b)==0)
    
    	{
    	gotoxy(12,20);printf("Correct!!!\n");
    	i=i+1;
    	gotoxy(12,21);printf("Your score is %d",i);
    	x=3;
    	getch();
    	}
    	else
    	{
    	gotoxy(12,20);printf("Wrong!!!\n");
    	gotoxy(12,21);printf("Life[%d]",x);
    	getch();
    	if(x>=3)
    	{
    	main();
    	}
    	}
    	}
    
    }
    Last edited by zanderela; 03-21-2008 at 12:54 AM.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    this is C, not C++

    and you may want to fix firts several issues like

    do not ever call main from your code
    specify the return type of your functions
    do not use gets

    and think about learning C instead of your own criptic language
    p,q,g are not good for variable names - and even more horrible as function names
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    And fix your indentation, too. If you don't use Visual Studio and there's no ban on the software and you aren't limited to a compiler, then I say get it and let it fix everything (Alt+F8).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Randomization
    By ldb88 in forum C++ Programming
    Replies: 12
    Last Post: 06-21-2006, 12:36 AM
  2. Arrrays, Loops, and Randomization
    By myrddin in forum C Programming
    Replies: 4
    Last Post: 05-21-2003, 04:01 AM
  3. Randomization
    By blackmagic in forum C++ Programming
    Replies: 7
    Last Post: 12-14-2002, 10:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21