Like Tree1Likes
  • 1 Post By Salem

Compiling fine but crashes

This is a discussion on Compiling fine but crashes within the C++ Programming forums, part of the General Programming Boards category; I apologize in advance for the less than perfect formatting at the bottom. This compiles but when it runs it ...

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    5

    Compiling fine but crashes

    I apologize in advance for the less than perfect formatting at the bottom. This compiles but when it runs it crashes. Its supposed to print 2 two-dimensional maps but instead it does nothing. I also had an issue trying to define the function prototypes before the main and then actually write them out after. I thought this was possible? any help would be greatly appreciated. Thanks!

    Code:
    /*
    Author: Jaime A. Costilla
    Date:
    File Name: Program 7
    Description:
    */
    #include <stdio.h>
    #include <cstdlib>
    #include<iostream>
    #include <iomanip>
    #include <cmath>
    
    using namespace std;
    
    int board[11][11]={0};
    int x=7,y=7,RW,CL;
    //1=N,2=S,3=W,4=E
    
    
    void PrintBoard(int &Y, int &X, int RW, int CL, int &TotalSteps);
    void PrintBoardB(int CL, int RW, int board[][11]);
    
    int main(int argc, char *argv[])
    {
    
    int steps,seed,X,Y;
    
    cout<<"Random walk simulation"<<endl;
    cout<<"Seed is: "<<endl;
    cin>>seed;
    srand(seed);
    
    while (steps!=0)
    {
        cout<<"Input number of steps (0 to end): ";
        cin>>steps;
        
        int amtStepsArray[steps],a,X,Y,x,y;
        int TotalSteps;
        
        if(steps==0){
            TotalSteps--;
            cout<<"The walk ends after "<<TotalSteps<<" total steps."<<endl;
            }
        else
        {
            for(a=0;a<steps;a++)
            {
             amtStepsArray[a]=rand()%4+1;
            }
        for(a=0;a<steps;a++)
            if(amtStepsArray[a]=1)
             {
              y++,TotalSteps++;
              board[y][x]=board[y][x]+1;
              X=x,Y=y;
              break;
              }
            else if(amtStepsArray[a]=2)
             {
              y--,TotalSteps++;    
              board[y][x]=board[y][x]+1;
              X=x,Y=y;
              break;
             }
            else if(amtStepsArray[a]=3)
            {
             x--,TotalSteps++;
             board[y][x]= board[y][x]+1;
             X=x,Y=y;
             break;
             }
            else
            {
             x++,TotalSteps++;
             board[y][x]=board[y][x]+1;
             X=x,Y=y;
             break;
            }
            
        if((x==11)||(x==-1)||(y==11)||(y==-1))
         {
          cout<<"out of bounds"<<endl;
          steps=0;
         }  
        else{
          cout<<"Coordinate: ("<<y<<","<<x<<")"<<endl;
       
       if((x==11)||(x==-1)||(y==11)||(y==-1))
        {
          cout<<"Total Steps taken = "<<TotalSteps-1<<"."<<endl;
          PrintBoard(Y,X,RW,CL,TotalSteps);
          PrintBoardB(CL,RW,board);
          cout<<"The walk ends after "<<TotalSteps<<" total steps."<<endl;
        }
       else
        {
          cout<<"Total Steps taken = "<<TotalSteps<<"."<<endl;  
           PrintBoard(Y,X,RW,CL,TotalSteps);
           PrintBoardB(CL,RW,board);
                   
        }  
    }
    }
    } 
        system("pause");
        return 0;
    }
    
    void PrintBoard (int &Y, int &X, int RW, int CL, int &TotalSteps)
    {
      RW=0;
      while (RW!=11)
       {
         CL=0;   
         while (CL!=11)
          {
            if ((x==11)||(x==-1)||(y==11)||(y==-1))
            {
              if((RW==Y)&&(CL==X))
              {
               cout<<"X";
              }   
              else
              {
              cout<<"*";
              }
             CL++; 
            }
            else
            {
              if((RW==y)&&(CL==x))
              {
              cout<<"X";
              }   
              else
              {
              cout<<"*";
              }  
             CL++;
            }
         RW++;
        }
      }
    }
    void PrintBoardB(int CL, int RW, int board[][11])
    {
         RW=0;
         while (RW!=11)
       {
         CL=0;  
         while (CL!=11)
          {
               cout<<board[RW][CL];
               CL++;
          }
          cout<<endl;
          RW++;
       }
         
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    First of all, poor indentation is no excuse.
    The primary reason you should want to do it is that it helps YOU to find logic errors in the code to begin with.
    Tracing the program flow in a chaotically indented program is nigh on impossible.

    Code:
    /*
    Author: Jaime A. Costilla
    Date:
    File Name: Program 7
    Description:
    */
    #include <stdio.h>
    #include <cstdlib>
    #include<iostream>
    #include <iomanip>
    #include <cmath>
    
    using namespace std;
    
    int board[11][11] = { 0 };
    
    int x = 7, y = 7, RW, CL;
    //1=N,2=S,3=W,4=E
    
    void PrintBoard(int &Y, int &X, int RW, int CL, int &TotalSteps);
    void PrintBoardB(int CL, int RW, int board[][11]);
    
    int main(int argc, char *argv[])
    {
      int steps, seed, X, Y;
    
      cout << "Random walk simulation" << endl;
      cout << "Seed is: " << endl;
      cin >> seed;
      srand(seed);
    
      while (steps != 0) {
        cout << "Input number of steps (0 to end): ";
        cin >> steps;
    
        int amtStepsArray[steps], a, X, Y, x, y;
        int TotalSteps;
    
        if (steps == 0) {
          TotalSteps--;
          cout << "The walk ends after " << TotalSteps << " total steps." << endl;
        } else {
          for (a = 0; a < steps; a++) {
            amtStepsArray[a] = rand() % 4 + 1;
          }
          for (a = 0; a < steps; a++)
            if (amtStepsArray[a] = 1) {
              y++, TotalSteps++;
              board[y][x] = board[y][x] + 1;
              X = x, Y = y;
              break;
            } else if (amtStepsArray[a] = 2) {
              y--, TotalSteps++;
              board[y][x] = board[y][x] + 1;
              X = x, Y = y;
              break;
            } else if (amtStepsArray[a] = 3) {
              x--, TotalSteps++;
              board[y][x] = board[y][x] + 1;
              X = x, Y = y;
              break;
            } else {
              x++, TotalSteps++;
              board[y][x] = board[y][x] + 1;
              X = x, Y = y;
              break;
            }
    
          if ((x == 11) || (x == -1) || (y == 11) || (y == -1)) {
            cout << "out of bounds" << endl;
            steps = 0;
          } else {
            cout << "Coordinate: (" << y << "," << x << ")" << endl;
            if ((x == 11) || (x == -1) || (y == 11) || (y == -1)) {
              cout << "Total Steps taken = " << TotalSteps - 1 << "." << endl;
              PrintBoard(Y, X, RW, CL, TotalSteps);
              PrintBoardB(CL, RW, board);
              cout << "The walk ends after " << TotalSteps << " total steps." << endl;
            } else {
              cout << "Total Steps taken = " << TotalSteps << "." << endl;
              PrintBoard(Y, X, RW, CL, TotalSteps);
              PrintBoardB(CL, RW, board);
    
            }
          }
        }
      }
      system("pause");
      return 0;
    }
    
    void PrintBoard(int &Y, int &X, int RW, int CL, int &TotalSteps)
    {
      RW = 0;
      while (RW != 11) {
        CL = 0;
        while (CL != 11) {
          if ((x == 11) || (x == -1) || (y == 11) || (y == -1)) {
            if ((RW == Y) && (CL == X)) {
              cout << "X";
            } else {
              cout << "*";
            }
            CL++;
          } else {
            if ((RW == y) && (CL == x)) {
              cout << "X";
            } else {
              cout << "*";
            }
            CL++;
          }
          RW++;
        }
      }
    }
    
    void PrintBoardB(int CL, int RW, int board[][11])
    {
      RW = 0;
      while (RW != 11) {
        CL = 0;
        while (CL != 11) {
          cout << board[RW][CL];
          CL++;
        }
        cout << endl;
        RW++;
      }
    }
    Next, compile with lots of warnings.
    Code:
    $ g++ -Wall bar.cpp
    bar.cpp:15:25: warning: missing braces around initializer for ‘int [11]’ [-Wmissing-braces]
    bar.cpp: In function ‘int main(int, char**)’:
    bar.cpp:47:33: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
    bar.cpp:52:40: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
    bar.cpp:57:40: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
    bar.cpp:25:20: warning: unused variable ‘X’ [-Wunused-variable]
    bar.cpp:25:23: warning: unused variable ‘Y’ [-Wunused-variable]
    $ g++ -Wall -Wshadow bar.cpp
    bar.cpp:15:25: warning: missing braces around initializer for ‘int [11]’ [-Wmissing-braces]
    bar.cpp: In function ‘int main(int, char**)’:
    bar.cpp:36:34: warning: declaration of ‘X’ shadows a previous local [-Wshadow]
    bar.cpp:25:20: warning: shadowed declaration is here [-Wshadow]
    bar.cpp:36:37: warning: declaration of ‘Y’ shadows a previous local [-Wshadow]
    bar.cpp:25:23: warning: shadowed declaration is here [-Wshadow]
    bar.cpp:36:40: warning: declaration of ‘x’ shadows a global declaration [-Wshadow]
    bar.cpp:17:5: warning: shadowed declaration is here [-Wshadow]
    bar.cpp:36:43: warning: declaration of ‘y’ shadows a global declaration [-Wshadow]
    bar.cpp:17:12: warning: shadowed declaration is here [-Wshadow]
    bar.cpp:47:33: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
    bar.cpp:52:40: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
    bar.cpp:57:40: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
    bar.cpp:25:20: warning: unused variable ‘X’ [-Wunused-variable]
    bar.cpp:25:23: warning: unused variable ‘Y’ [-Wunused-variable]
    bar.cpp: In function ‘void PrintBoard(int&, int&, int, int, int&)’:
    bar.cpp:92:64: warning: declaration of ‘CL’ shadows a global declaration [-Wshadow]
    bar.cpp:17:23: warning: shadowed declaration is here [-Wshadow]
    bar.cpp:92:64: warning: declaration of ‘RW’ shadows a global declaration [-Wshadow]
    bar.cpp:17:19: warning: shadowed declaration is here [-Wshadow]
    bar.cpp: In function ‘void PrintBoardB(int, int, int (*)[11])’:
    bar.cpp:118:49: warning: declaration of ‘board’ shadows a global declaration [-Wshadow]
    bar.cpp:15:5: warning: shadowed declaration is here [-Wshadow]
    bar.cpp:118:49: warning: declaration of ‘RW’ shadows a global declaration [-Wshadow]
    bar.cpp:17:19: warning: shadowed declaration is here [-Wshadow]
    bar.cpp:118:49: warning: declaration of ‘CL’ shadows a global declaration [-Wshadow]
    bar.cpp:17:23: warning: shadowed declaration is here [-Wshadow]
    
    Or
    $ g++ -Wall -O2 bar.cpp
    bar.cpp:15:25: warning: missing braces around initializer for ‘int [11]’ [-Wmissing-braces]
    bar.cpp: In function ‘int main(int, char**)’:
    bar.cpp:47:33: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
    bar.cpp:52:40: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
    bar.cpp:57:40: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
    bar.cpp:25:20: warning: unused variable ‘X’ [-Wunused-variable]
    bar.cpp:25:23: warning: unused variable ‘Y’ [-Wunused-variable]
    bar.cpp:88:18: warning: ignoring return value of ‘int system(const char*)’, declared with attribute warn_unused_result [-Wunused-result]
    bar.cpp:49:35: warning: ‘x’ may be used uninitialized in this function [-Wuninitialized]
    bar.cpp:48:28: warning: ‘y’ may be used uninitialized in this function [-Wuninitialized]
    > if ((x == 11) || (x == -1) || (y == 11) || (y == -1))
    Look at this line.
    You might think it is part of your for loop, but it isn't.
    You would never guess this from simply reading the code in the non-indentation style you have chosen.
    One tip would be to always use braces, even where they appear to be optional.

    > if (amtStepsArray[a] = 1)
    What is the difference between = and ==
    See at least one of the warnings which alludes to this problem.

    > int amtStepsArray[steps], a, X, Y, x, y;
    Strictly speaking, C++ does NOT allow variable length arrays.
    Use 'new' to allocate the space, or use a std::vector.
    stahta01 likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    5
    > if ((x == 11) || (x == -1) || (y == 11) || (y == -1))
    Look at this line.
    You might think it is part of your for loop, but it isn't.
    You would never guess this from simply reading the code in the non-indentation style you have chosen.
    One tip would be to always use braces, even where they appear to be optional.
    This wasn't supposed to be in a for loop. Its a while. And It usually works when I put the conditions in parenthesis. Why would this time be any different? So you are saying to use braces around the whole thing with the separate conditions still within parenthesis?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 07-02-2011, 11:18 AM
  2. Program runs fine, then crashes 5% of the time
    By mat1z in forum C++ Programming
    Replies: 5
    Last Post: 01-30-2011, 03:28 AM
  3. Runs fine in IDE but crashes normally!
    By Hunter2 in forum Windows Programming
    Replies: 5
    Last Post: 05-07-2002, 03:47 PM
  4. One fine day...
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 01-11-2002, 06:06 AM
  5. one fine day...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 44
    Last Post: 11-15-2001, 05:45 PM

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