weird cpp error

Discussion in 'Software' started by alarm, Oct 29, 2004.

  1. alarm

    alarm Private First Class

    when I run this, it doesn't do what it's supposed to. It's an ascii space invaders game(my first game programming project). It's supposed to go left when you press a, and right when you press d.


    Code:
    #include <stdio.h>			// for printf
    #include <conio.h>			// for getch
    #include <string.h>
    #include <iostream.h>
    
    
    
    char espace_vaisseau[76];
    char left_right;
    int espace = 39;
    int count;
    int loop;
    
    main ()
    {
    	strcpy(espace_vaisseau, "                                       ");		// graphics pour le vaisseau
    
    	
    
    	cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
    
    	cout << "^W^" << espace_vaisseau ; // afficher vaisseau
    
    for(loop = 0; loop < 4; loop + 0)       // infinite loop; while didn't work
    	{
    		if((left_right = getch()) == 'a')
    		{
    			espace--;
    		}
    		else if ((left_right = fetch()) == 'd')
    		{
    			espace++;
    		}
    		if (espace < 0)
    		{
    			espace++;
    		}
    		else if (espace > 76)
    		{
    			espace--;
    		}
    
    		for (count = 0; count <= espace; count++)
    		{
    			espace_vaisseau[count]=' ';
    		}
    
    		printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
    
    		printf("%s^W^", espace_vaisseau); // afficher vaisseau
    	}
    
    return (0);
    }  
     
  2. Maxwell

    Maxwell Folgers

    Your first else if statement has a call to fetch rather than getch to read the next character from the keyboard.

    Also, you are mixing C calls (printf) with C++ syntax (cout <<).

    What do you do with the while statement?

    Does for (; ; ) not work as well?
     
  3. alarm

    alarm Private First Class

    sorry, fixed version:
    Code:
    #include <stdio.h>			// for printf
    #include <conio.h>			// for getch
    #include <string.h>
    #include <iostream.h>
    
    
    
    char espace_vaisseau[76];
    char left_right;
    int espace = 39;
    int count;
    int loop;
    
    main ()
    {
    	strcpy(espace_vaisseau, "                                       ");		// graphics pour le vaisseau
    
    	
    
    	printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
    
    	printf("%s^W^", espace_vaisseau); // afficher vaisseau
    
    for(;;)		// infinite loop
    	{
    		if((left_right = getch()) == 'a')
    		{
    			espace--;
    		}
    		else if ((left_right = getch()) == 'd')
    		{
    			espace++;
    		}
    		if (espace < 0)
    		{
    			espace++;
    		}
    		else if (espace > 76)
    		{
    			espace--;
    		}
    
    		for (count = 0; count <= espace; count++)
    		{
    			espace_vaisseau[count]=' ';
    		}
    
    		printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
    
    		printf("%s^W^", espace_vaisseau); // afficher vaisseau
    	}
    
    return (0);
    } 
    isn't there a 'edit post'?
     
  4. alarm

    alarm Private First Class

    got it! this is the corrected verision. 81 array elements instead of 77, and end of string character at end of string.

    Code:
    #include <stdio.h>			// for printf
    #include <conio.h>			// for getch
    #include <string.h>
    #include <iostream.h>
    
    
    
    char espace_vaisseau[81];
    char left_right;
    int espace = 39;
    int count;
    int loop;
    
    main ()
    {
    	strcpy(espace_vaisseau, "                                       ");		// graphics pour le vaisseau
    
    	
    
    	printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
    
    	printf("%s^W^\0", espace_vaisseau); // afficher vaisseau
    
    	for(;;)		// infinite loop
    	{
    		if((left_right = getch()) == 'a')
    		{
    			espace--;
    		}
    		else if ((left_right = getch()) == 'd')
    		{
    			espace++;
    		}
    		if (espace < 0)
    		{
    			espace++;
    		}
    		else if (espace > 76)
    		{
    			espace--;
    		}
    
    		for (count = 0; count <= espace; count++)
    		{
    			espace_vaisseau[count]=' ';
    		}
    
    		espace_vaisseau[count] = '\0';
    
    		printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
    
    		printf("%s^W^", espace_vaisseau); // afficher vaisseau
    	}
    
    return (0);
    } 
     
  5. QuickSilver

    QuickSilver Corporal

    Yea, an infinite for loop can be constructed with

    for(;;)
    {
    }

    I believe any for loop which isn't completed properly defaults to an infinite one
    but I may be wrong on that...
    So
    for(i=0 ; i<10 ; )
    is also an infinite loop...
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds