Thread: What could have possibly caused this garbage?

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    28

    What could have possibly caused this garbage?

    Hi!im workin on a millionaires game ryt now and the problem is that when i call the questions i made in the game part,it prints out nothing or sometyms prints out garbage...whats wrong with it?do i ahve to add a null initialization in my funciton that initialozes the questions? or does it have to do with my passing paramter thing?

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Post the code and we can see... else it's just guessing on our part.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    28
    Here is the game part of the program...i hope you could look into whats wrong with my paramter passing here thanks....:>
    Code:
    /* Name: Program name: 
       Description: This program is a simplified version of Who Wants To Be a Millionaire!
    		*/
    
    #include<stdio.h>
    #include<string.h>    
    #include<stdlib.h>
    
    #define MAX 48
    #define ERROR "Invalid Input!"
    #define NOT_FOUND -1
    /*variable declarations*/
    
    typedef char str20[21];
    typedef char str70[71];
    
    typedef struct
    {
    	str70 strQuestions;
    	str20 strChoices[4];
    	char  cAns;
    	int  nLevel;
    }struct_Game;
    struct structNode
    {
    	struct_Game sGame;
    	struct structNode *pGPrev,*pGNext;
    };
    typedef struct structNode structGNodeType;
    typedef structGNodeType *ptrGNode;
    
    
    
    /* Function: Game_mod(ptrGNode*pGame,struct_Game sGame)					*
     * Purpose: This function enters the user to the game proper		*/
    			
    void Game_mod(ptrGNode *pGame,struct_Game sGame)
    {	ptrGNode pTemp;
    	int nNum,nCtr=3,nTemp,nAmt_won;
    	char cCont,cChoice ,cEnter;
    	pTemp=*pGame;
    	int arrlifeline[3]={1,2,3}; 
    	int arrPeso_level[16] =
    	{0,1000,2000,3000,5000,10000,20000,30000,40000,50000,100000,200000,300000,500000,1000000,2000000};
    	initialize_question(&*pGame,sGame);
    	/*nTemp=rand()%15;*/
    	cCont = display_instructions(cCont);
    		if(cCont=='Y' || cCont=='y')
    		{	scanf("%c",&cEnter);
    		  do
    	 	  {
    printf("You are going to the %dlevel\n",arrPeso_level[nTemp+1]);
    
       		  printf("You have %d lifelines left\n",nCtr);
    
    		  printf("\nYour question is :\n");
    		  
    		/*Calls subfunction*/
    		  cChoice =  get_answer(pTemp,sGame);
    			/*evaluates user's answer*/
    		do
    		{
    		if(cChoice!='a'&&cChoice!='b'&&cChoice!='c'&&cChoice!='d'&&
    		cChoice!='1'&&cChoice!='2'&&cChoice!='3'&&cChoice!='x')
    		{	  
    			  printf("Invalid answer!\n");
    			  printf("Enter your answer again");
    			  
    			  scanf("%c",&cChoice);
    		}
            	}while(cChoice!='a' && cChoice!='b' &&
    				cChoice!='c' && cChoice!='d'&&
    		cChoice!='1' && cChoice!='2' && cChoice!='3' &&
    		cChoice!='x');
    			scanf("%c",&cEnter);				
    			if(cChoice == pTemp->sGame.cAns)
    			{
    			  printf("That's correct!\n");
    			  nTemp++;
    			  pTemp=pTemp->pGNext;
    			}
    			else if(cChoice == 'x' || cChoice == 'X')
    			{
    			  printf("You chose to end this game\n");
    			  printf("You receive %d pesos\n",arrPeso_level[nTemp]);
    			  printf("Congratulations!");
    			}
    			else if(cChoice=='a'||cChoice=='b'||cChoice=='c'|| cChoice=='d')
    			{
                               printf("Sorry,Wrong answer!\n");
    			  printf("You receive %d pesos\n",arrPeso_level[nTemp]);
    				Menu(&*pGame,sGame);       
    			}	
                          else if(cChoice=='1' || cChoice=='2' ||
    				cChoice=='3')
    			 nCtr = display_lifeline(nCtr,arrlifeline,cChoice,sGame);
    		/* Condition for bringing incorrect answer to safelevel*/
    				if(arrPeso_level[nTemp]/5==0)
    				  nAmt_won=0;
    				else if(arrPeso_level[nTemp]/5==1)
    				  nAmt_won=10000;
    				else if(arrPeso_level[nTemp]/5==2)
    				  nAmt_won==100000;
    				else if(arrPeso_level[nTemp]/5==3)
    				 { nAmt_won==2000000;
    				  printf("You're our lucky Millionaire,Congratulations!!");		
    				}
    		  	 
    	}while(cChoice==pTemp->sGame.cAns||cChoice=='1'||cChoice=='2'||
    	 cChoice==3||cCont=='Y'||cCont=='y'&& nTemp <16);
    	}
    
    
    	/*else part to exit program*/
    	else
    		printf("Program aborted by user!\n\n");
    		
    	}	
    /*Function:display_instructions()				*
      *Purpose : Prints Welcome message and reads value for choice	*
      *Called by: game_mod()					*
      *Receives: cCont(character)					*
      *Returns: cCont(Character)					*/
     
    char display_instructions(char cCont)
    { 	clear_screen();
    	printf("\n");
    	printf("+-----------------------------------------------+\n");
    	printf("|		WELCOME TO		        |\n");
    	printf("|	WHO WANTS TO BE A MILLIONAIRE!!!	|\n");
    	printf("| To START------------------------------ TYPE Y	|\n");
    	printf("| To TERMINATE/END---------------------- TYPE N |\n");
    	printf("+-----------------------------------------------+\n");
    	do
    	{
    	  printf("Enter your choice:");
    	  scanf("%c",&cCont);
    	 
    		if(cCont!='Y'&& cCont!='y'&& cCont!='N'&& cCont!='n')
                    printf("Wrong choice!\n");
    	}while(cCont!='Y'&& cCont!='y'&& cCont!='N'&& cCont!='n');
    
    	return cCont;		
    }/*end of function*/
    
    /*Function: int display_lifeline()				*
     *Purpose : This function displays and updates lifeline		*
     *Returns: integer						*
     *Note:	 1- call-a-friend,2-50/50,3-ask-the-audience		*/
    int display_lifeline(int nCtr, int arrlifeline[],char cChoice,struct_Game sGame)
    {	
    	int  nCount;
    	if(cChoice =='1')
    		nCount = 1;
    	else if(cChoice =='2')
    		nCount = 2;
    	else if(cChoice=='3')
    		nCount = 3;
    		if(nCount<=3 && nCount>=1)
    		{
    		
    		if(nCount==arrlifeline[0])
    		{
    		  printf("You chose call-a-friend\n");
    		  printf("\nDialing...\n");
    		  delay();
    		  printf("Chris: I think the answer is %c
    			\n",&sGame.cAns);
    		printf("I'm 100% sure!");
    		  arrlifeline[0]=5; /*assigns a different value to array*/
    		  nCtr--;
    		}
    		else if(nCount==arrlifeline[1])
    		{
    		  printf("You chose 50/50\n");
    			display_50(sGame);
    		  arrlifeline[1]=6;
    		  nCtr--;
    		}
    		else if(nCount==arrlifeline[2])
    		{
    		  printf("You chose ask-the-audience\n");
    		  puts("a.40%");
    		 puts("b.15%");
    		 puts("c. 20%");
    		  puts("d. 25%");
    		  arrlifeline[2]=7;
    		  nCtr--;
    		}	
    	}	
    	return nCtr;
            }	/*end of function*/
    
    /* Function: char get_answer()							*
     * Purpose: This Function displays the question and throws answer to main()	*
     * Returns: character								*/
    
    char get_answer(ptrGNode pGame,struct_Game sGame)
    {	
    	int j;
    	char cChoice;
    	
    	if(strcmp(pGame->sGame.strQuestions,""))
    	{	/* nTemp = rand()%15;*/
    		puts(pGame->sGame.strQuestions);
    		for(j=0;j<4;j++)
    		{
    			if(j==0)
    			{
    			printf("a.");
    			puts(pGame->sGame.strChoices[j]);
    			
    			}
    			else if(j==1)
    			{	
    			printf("b.");
    			puts(pGame->sGame.strChoices[j]);
    			
    			}
    			else if(j==2)
    			{	
    			printf("c.");
    			puts(pGame->sGame.strChoices[j]);
    			
    			}	
    			else if(j==3)
    			{	
    			printf("d.");
    			puts(pGame->sGame.strChoices[j]);
    			
    			}
    		
    		}
    		
    		
    	}
    	printf("What is your answer?");
    	scanf("%c", &cChoice);
    	return cChoice;		
    }	/*end of function*/
    
    /*Function: void initialize_question()					*
     * Purpose: This function initializes values for aGame			*
     * Returns: nothing							*/
    void initialize_question(ptrGNode *pGame,struct_Game sGame)
    { int i,j,k;
    	ptrGNode pRun,pNew;
    	i=0,j=0;
     /*copies value of string to array*/
    	
     	if(*pGame==NULL)
    	{
    	strcpy((*pGame)->sGame.strQuestions," Ano ang pambansang hayop ng Pilipinas?");
    	strcpy((*pGame)->sGame.strChoices[0],"aso");
    	strcpy((*pGame)->sGame.strChoices[1],"pusa");
    	strcpy((*pGame)->sGame.strChoices[2],"kalabaw");
    	strcpy((*pGame)->sGame.strChoices[3],"kabayo");
    	(*pGame)->sGame.cAns = 'c';(*pGame)->sGame.nLevel = 1;
    	(*pGame)->pGPrev=NULL;
    	(*pGame)->pGNext=NULL;
    	pRun=*pGame;
    	}
    	pNew =(ptrGNode)malloc(sizeof(structGNodeType));
    	
    	strcpy(pNew->sGame.strQuestions," Sino ang pumatay kay Magellan?");
    	strcpy(pNew->sGame.strChoices[0],"Raja Soliman");
    	strcpy(pNew->sGame.strChoices[1],"Si Malakas");
    	strcpy(pNew->sGame.strChoices[2],"Raja Ulayman");
    	strcpy(pNew->sGame.strChoices[3],"Lapu-Lapu");
    	pNew->sGame.cAns = 'd'; pNew->sGame.nLevel = 1;
    	pRun->pGNext=pNew;
    	pNew->pGPrev=pRun;
    	pNew->pGNext=NULL;
    	pRun=pRun->pGNext;
    
    	pNew = (ptrGNode)malloc(sizeof(structGNodeType));
    	strcpy(pNew->sGame.strQuestions," Ano ang pambansang awit ng Pilipinas?");
    	strcpy(pNew->sGame.strChoices[0],"Lupang Hinirang");
    	strcpy(pNew->sGame.strChoices[1]," Bayang Magiliw");
    	strcpy(pNew->sGame.strChoices[2]," Perlas ng Silanganan");
    	strcpy(pNew->sGame.strChoices[3]," Lupa ng Araw");
    	pNew->sGame.cAns = 'a'; pNew->sGame.nLevel = 1;
    	pRun->pGNext=pNew;
    	pNew->pGPrev=pRun;
    	pNew->pGNext=NULL;
    	pRun=pRun->pGNext;
    
    	pNew = (ptrGNode)malloc(sizeof(structGNodeType));
    	strcpy(pNew->sGame.strQuestions," Ano ang tagalog ng rectangle?");
    	strcpy(pNew->sGame.strChoices[0],"kwadrado");
    	strcpy(pNew->sGame.strChoices[1],"tatsulok");
    	strcpy(pNew->sGame.strChoices[2],"parihaba");
    	strcpy(pNew->sGame.strChoices[3],"parisukat");
    	pNew->sGame.cAns = 'c'; pNew->sGame.nLevel = 1;
    	pRun->pGNext=pNew;
    	pNew->pGPrev=pRun;
    	pNew->pGNext=NULL;
    	pRun=pRun->pGNext;
    
    	pNew =(ptrGNode)malloc(sizeof(structGNodeType));
    	strcpy(pNew->sGame.strQuestions," Ano ang pangalan ng seawitch sa Disney movie na LittleMermaid?");
    	strcpy(pNew->sGame.strChoices[0],"Ariel");
    	strcpy(pNew->sGame.strChoices[1],"Flounder");
    	strcpy(pNew->sGame.strChoices[2],"Cruella");
    	strcpy(pNew->sGame.strChoices[3],"Ursula");
    	pNew->sGame.cAns = 'd'; pNew->sGame.nLevel = 1;
    	pRun->pGNext=pNew;
    	pNew->pGPrev=pRun;
    	pNew->pGNext=NULL;
    	pRun=pRun->pGNext;
    
    	pNew =(ptrGNode)malloc(sizeof(structGNodeType));
    	strcpy(pNew->sGame.strQuestions,"Saan nobela ni Rizal makikita si SIMOUN?");
    	strcpy(pNew->sGame.strChoices[0],"Noli Me Tangere");
    	strcpy(pNew->sGame.strChoices[1],"El Filibusterismo");
    	strcpy(pNew->sGame.strChoices[2],"Florante at Laura");
    	strcpy(pNew->sGame.strChoices[3],"Mi Ultimo Adios");
    	pNew->sGame.cAns = 'b'; pNew->sGame.nLevel = 2;
    	pRun->pGNext=pNew;
    	pNew->pGPrev=pRun;
    	pNew->pGNext=NULL;
    	pRun=pRun->pGNext;
    
    	pNew =(ptrGNode)malloc(sizeof(structGNodeType));	
    	strcpy(pNew->sGame.strQuestions," Saan kilala si Gabriel Flash Elorde?");
    	strcpy(pNew->sGame.strChoices[0],"bowling");
    	strcpy(pNew->sGame.strChoices[1],"boxing");
    	strcpy(pNew->sGame.strChoices[2],"chess");
    	strcpy(pNew->sGame.strChoices[3],"karate");
    	pNew->sGame.cAns = 'b'; pNew->sGame.nLevel = 2;
    pRun->pGNext=pNew;
    	pNew->pGPrev=pRun;
    	pNew->pGNext=NULL;
    	pRun=pRun->pGNext;
    
    	pNew =(ptrGNode)malloc(sizeof(structGNodeType));
    	strcpy(pNew->sGame.strQuestions," Sino sa mga ss.ang hindi myembro ng 80s band naDuran2?");
    	strcpy(pNew->sGame.strChoices[0],"Andy Taylor");
    	strcpy(pNew->sGame.strChoices[1],"Tony Hadley");
    	strcpy(pNew->sGame.strChoices[2],"Simon Le Bon");
    	strcpy(pNew->sGame.strChoices[3],"Nick Rhodes");
    	pNew->sGame.cAns = 'b'; pNew->sGame.nLevel = 2;
    pRun->pGNext=pNew;
    	pNew->pGPrev=pRun;
    	pNew->pGNext=NULL;
    	pRun=pRun->pGNext;
    
    	pNew =(ptrGNode)malloc(sizeof(structGNodeType));
    	strcpy(pNew->sGame.strQuestions," Tungkol kanino ang pelikulang MommieDearest?");
    	strcpy(pNew->sGame.strChoices[0],"Ingrid Bergman");
    	strcpy(pNew->sGame.strChoices[1],"Grace Kelly");
    	strcpy(pNew->sGame.strChoices[2]," Greta Garbo");
    	strcpy(pNew->sGame.strChoices[3]," Joan Crawford");
    	pNew->sGame.cAns = 'd'; pNew->sGame.nLevel = 2;
    	pRun->pGNext=pNew;
    	pNew->pGPrev=pRun;
    	pNew->pGNext=NULL;
    	pRun=pRun->pGNext;
    
    	pNew =(ptrGNode)malloc(sizeof(structGNodeType));
    	strcpy(pNew->sGame.strQuestions," Ano ang nickname kay IanThorpe,Australian Olympic Swimming?");
    	strcpy(pNew->sGame.strChoices[0]," Thornado");
    	strcpy(pNew->sGame.strChoices[1],"Mighty Thor");
    	strcpy(pNew->sGame.strChoices[2]," Thorgue");
    	strcpy(pNew->sGame.strChoices[3]," Thorpedo");
    	pNew->sGame.cAns = 'd'; pNew->sGame.nLevel = 2;
    	pRun->pGNext=pNew;
    	pNew->pGPrev=pRun;
    	pNew->pGNext=NULL;
    	pRun=pRun->pGNext;
    
    	pNew=(ptrGNode)malloc(sizeof(structGNodeType));
    	strcpy(pNew->sGame.strQuestions," Kapag hinanap ka ng taga-BIR,may kaso ka na?");
    	strcpy(pNew->sGame.strChoices[0],"tax Evasion");
    	strcpy(pNew->sGame.strChoices[1],"jaywalking");
    	strcpy(pNew->sGame.strChoices[2],"smokebelching");
    	strcpy(pNew->sGame.strChoices[3],"illegal recruiting");		
    	pNew->sGame.cAns = 'a';pNew->sGame.nLevel = 3;
    	pRun->pGNext=pNew;
    	pNew->pGPrev=pRun;
    	pNew->pGNext=NULL;
    	pRun=pRun->pGNext;
    
    	pNew = (ptrGNode)malloc(sizeof(structGNodeType));
    	strcpy(pNew->sGame.strQuestions," Saan ang pinakamalaking isla ng Pilipinas?");
    	strcpy(pNew->sGame.strChoices[0],"Panay");
    	strcpy(pNew->sGame.strChoices[1],"Palawan");
    	strcpy(pNew->sGame.strChoices[2],"Luzon");
    	strcpy(pNew->sGame.strChoices[3],"Cebu");
    	pNew->sGame.cAns = 'c'; pNew->sGame.nLevel = 3;
    	pRun->pGNext=pNew;
    	pNew->pGPrev=pRun;
    	pNew->pGNext=NULL;
    	pRun=pRun->pGNext;
    
    	pNew=(ptrGNode)malloc(sizeof(structGNodeType));
    	strcpy(pNew->sGame.strQuestions," Ano ang tawag sa headdress ng mga madre?");
    	strcpy(pNew->sGame.strChoices[0],"wimple");
    	strcpy(pNew->sGame.strChoices[1],"hob");
    	strcpy(pNew->sGame.strChoices[2],"bittern");
    	strcpy(pNew->sGame.strChoices[3],"camela");
    	pNew->sGame.cAns = 'a';pNew->sGame.nLevel = 3;
    	pRun->pGNext=pNew;
    	pNew->pGPrev=pRun;
    	pNew->pGNext=NULL;
    	pRun=pRun->pGNext;
    
    	pNew =(ptrGNode)malloc(sizeof(structGNodeType));
    	strcpy(pNew->sGame.strQuestions," Sino ang doktor na nakadiskubre ng bakuna sa smallpox?");
    	strcpy(pNew->sGame.strChoices[0],"Edward Jenner");
    	strcpy(pNew->sGame.strChoices[1],"Alexander Fleming");
    	strcpy(pNew->sGame.strChoices[2],"Irene Curie");
    	strcpy(pNew->sGame.strChoices[3],"Ernest Cain");
    	pNew->sGame.cAns = 'a';pNew->sGame.nLevel = 3;
    	pRun->pGNext=pNew;
    	pNew->pGPrev=pRun;
    	pNew->pGNext=NULL;
    	pRun=pRun->pGNext;
    
    	pNew=(ptrGNode)malloc(sizeof(structGNodeType));
    	strcpy(pNew->sGame.strQuestions," Pisika,ano ang ibig sabihin ng EMF?");
    	strcpy(pNew->sGame.strChoices[0],"electromotive force");
    	strcpy(pNew->sGame.strChoices[1],"electromagnetic field");
    	strcpy(pNew->sGame.strChoices[2],"electromagnetic force");
    	strcpy(pNew->sGame.strChoices[3],"electromotive field");
    	pNew->sGame.cAns = 'a';pNew->sGame.nLevel = 3;
    	pRun->pGNext=pNew;
    	pNew->pGPrev=pRun;
    	pNew->pGNext=NULL;
    	pRun=pRun->pGNext;
    
    
    	
    	
    }	
    
    
    void display_50(struct_Game sGame)
    {	int i;
    	puts(sGame.strQuestions);
    		for(i=0;i<4;i++)
    		{	if(sGame.cAns=='a'||sGame.cAns=='c')
    			{	if(i==1)
    				{	printf("b.");
    					puts(sGame.strChoices[i]);
    				}
    				if(i==3)
    				{	printf("d.");
    					puts(sGame.strChoices[i]);
    				}
    			}
    			else if(sGame.cAns=='b'||sGame.cAns=='d')
    			{	if(i==0)
    				{
    				printf("a.");
    				puts(sGame.strChoices[i]);
    				}
    				if(i==2)
    				{
    				printf("c.");
    				puts(sGame.strChoices[i]);
    				}
    			}
    }
    }
    Last edited by kreyes; 03-07-2002 at 08:14 PM.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Use the damn [code] tags! How hard is it? It's easy, you can even press the PHP button if you want pretty color. Using the code/php tags MAKES YOUR CODE FORMATTED and so your code is MUCH EASIER TO READ.

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    28
    ive already fixed the code thing---- sorry,hope you can help
    only the fittest survive!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  2. Replies: 1
    Last Post: 12-01-2007, 02:06 AM
  3. C++ Garbage Collection
    By vaibhav in forum C++ Programming
    Replies: 1
    Last Post: 11-27-2005, 10:26 AM
  4. Garbage Collection
    By Orborde in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2005, 11:18 PM
  5. Array[] garbage output
    By guda in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2002, 12:52 PM