Thread: Help me...

  1. #1
    Registered User
    Join Date
    May 2018
    Posts
    1

    Help me...

    this is a manhattan tourist problem program with c language... help me to fix the user input part, i can enter the user name, and the program can be build..but it only can run up until the attactions visited:0 then the program will self stop..HELP ME!
    Attached Files Attached Files

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    Here's the code that was in the file:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <time.h>
    
    #define ROWS 8
    #define COLS 8
    
    void initializer(int arr[ROWS][COLS])
    {
        int x,y;
    	
        
        for(x=0;x<ROWS;x++)
        {
            for(y=0;y<COLS;y++)
            {
                
                arr[x][y]=0;
            }
            
        }
    }
    void number(int num[8][8])
    {
    	char name[100];
    	
    
    	/* for user to input their name*/	
      printf("\n\n*****Welcome to Manhattan Tourist Problem!*******\n\n");
    	printf("Please Key in your name:");
    		scanf("%s",name);
    		
    		printf("****Good day %s , let start the game...ALL THE BEST!******\n");
    			printf("Press any key to start the game \n");
         			 getch();
    }
    
    void display(int num[8][8])
    {
    	
        int x, y=0;
    
    
    				
    	/*to generate the number */
       srand(time(0));
      
        for(x=0;x<8;x++)
        {
    	  for (y=0;y<8;y++)
            {
                num[x][y]= rand() %10;
               
        	if(y==0)
    		{
                     printf("  |    |    |    |    |    |    |    |  \n");
               
             }   
                if(num[0][0]=0)
                {
                    printf("|%d|",num[x][y]);
                }
                else
                {
                    printf(" %d ",num[x][y]);
                }
                
                printf("-");
            }
            printf("\n");
        }
        printf("\n");
    	}
    	
    void action(int num[8][8])
    	{
    	int attrac=0;
    	int x,y=0;
    	char choice;
    	char input;
    	int total;
    	int row=8,col=8;
    	
    	printf("Number of attraction visited so far: %d \n",attrac);
    	
    	 attrac+=num[x][y];
    
    	printf("\n\nEach number in the map represent the number of attractions along each street,You may only move to the right or move down!\n\n");
    
    	printf("Press 'D' to move down\n");
    	printf("Press 'R' to move to the right\n");
    	printf("Press 'Q' to Exit the game\n");
    	printf("Please enter your next action:%c",input);
    	 scanf("%c",&input);
    	 printf("\n");
    	 
    	 if(input == 'R' || input == 'r'){
    		x++;
    		num[x][y]=1;
    
    		
    	}
    	 if(input == 'D' || input == 'd'){
    		y++;
    		num[x][y]=1;
    	
    		
    	
    	}
    	
    	printf("\n\nCongratulation, You have complted the tour!\n\n");
    
    	printf("Total attractions you have visited: %d \n",total);
    	
    	printf("Thanks for playing!! See You Again!");
    		
    }
    
    
    
    
    
    
    int main()
    {
    	
     	int arr[ROWS][COLS],attrac;
    	
    	
    	initializer(arr);
    	number(arr);
    	
    	display(arr);
    	action(arr);
    	
    	
    	
    	
     }
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    My editor put a squiggly line under a lot of places including line 79 int x,y=0; with the warning "Variable 'x' is not assigned a value." But if you aren't using an editor that does static analysis on the fly, you can get the same information by just turning on compiler warnings.

    If your compiler doesn't have good warnings, it will still have errors. You can help it along by putting the rvalues on the left in comparisons. num[0][0]=0 compiles because this is a legal assignment, 0=num[0][0] will not, so you will notice that you should have used == there.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,490
    Choose better subject titles.
    How To Ask Questions The Smart Way

    Operand swapping is ugly.
    Question 17.4
    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.

Popular pages Recent additions subscribe to a feed

Tags for this Thread