Thread: doesnt work while ..loop

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    80

    doesnt work while ..loop

    hi

    in my programme there has to be at least 2 actions
    i used do while loop in one of the fuctions and this function is called when pressed "p" and problem occurs here:this function uses do while loop .while this function works changes between(1-5 seconds) other fuctions dont work.

    so i tried changing it to for loop.but at this time the function doesnt work.i mean there is nothing printed to screen.
    how can i solve this problem?
    my code is:#include <conio.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <dos.h>
    void player();
    void fire();
    void animation();
    void stupid_ship();

    int x=3,y=3;
    int oldx,oldy;
    int firex,firey;
    int shipx,shipy,tempy
    int existship=0;


    void player() {

    char key;

    while(1) {
    gotoxy(x,y);printf(":->");
    oldx=x;
    oldy=y;
    key = getch();
    switch(key) {

    case 'w' :
    y--;
    gotoxy(oldx,oldy);
    printf(" ");
    if(y==1) {
    y=2;
    }
    break;
    case 's' :
    y++;
    gotoxy(oldx,oldy);
    printf(" ");
    if(y==23) {
    y=22;
    }
    break;
    case 'a' :
    x--;
    gotoxy(oldx+2,oldy);
    printf(" ");
    if(x==1) {
    x=2;
    }
    break;
    case 'd' :
    x++;
    gotoxy(oldx,oldy);
    printf(" ");
    if(x==81) {
    x=8*;
    }
    break;

    case 'p' :
    fire();
    break;

    case 'q' :
    exit(0);
    break;

    default :
    getch();
    }
    } //end of while
    } //end of player
    //---------------------------------------------------------
    void fire() {
    firex=x+4;
    firey=y;
    // for(firex=x+4;firex>85;firex++)
    do {
    delay(50);
    gotoxy(firex,firey);printf(".");
    gotoxy(firex-1,firey);printf(" ");
    firex++;
    if(firex==shipx && firey==shipy) {
    animation();
    firex=60;
    }
    }while(firex!=82) ;
    }
    //-------------------------------------------------------------
    void animation() {
    gotoxy(firex,firey);printf("isi bitti");
    delay(100);
    gotoxy(firex,firey);printf(" ");
    existship=0;
    }

    void stupid_ship() {
    randomize();
    shipx=60;
    shipy=random(25);
    textcolor(random(12));
    while(shipx!=1) {
    delay(400);
    old_shipx=shipx;
    old_shipy=shipy;
    gotoxy(shipx,shipy);printf("-<<=");
    gotoxy(old_shipx,old_shipy);printf(" ");
    shipx--;
    } //end of while
    } //end of function

    void main() {
    clrscr();
    player();
    }
    Last edited by condorx; 02-24-2002 at 12:50 PM.

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    Well for one:
    It may be my eyes but I can't see stupid_ship called in the program, which means nothing is put in shipx, shipy.
    So the test:
    if(firex==shipx && firey==shipy)
    is unlikely to be succesful.
    Last edited by C_Coder; 02-24-2002 at 01:12 PM.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >so i tried changing it to for loop
    Very rarely is your choice of loop the culprit, it's probably something elsewhere in the code causing problems. Perhaps an infinite loop. Post some code so that we can debug a bit better, my crystal ball has been decommissioned.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    80

    i add my code

    sorry i attach my code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM
  2. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  3. Replies: 26
    Last Post: 06-15-2005, 02:38 PM
  4. The Bludstayne Open Works License
    By frenchfry164 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-26-2003, 11:05 AM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM