7 Replies - 3153 Views - Last Post: 05 May 2014 - 09:50 AM Rate Topic: -----

#1 jcn1991   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-May 14

Snakes and Ladders Codeblocks error

Posted 05 May 2014 - 09:00 AM

My error on code blocks is fatal error no such file found in iostream, I don't understand this error!!

#include <iostream>
#include <cstdlib>
#include <string>
#include <ctime>
#include <fstream>
#include <vector>
#include <sstream>
#include <iterator>

int Random(int random)
{
        return (rand() % random) + 1;
}

    using namespace std;

    class CBoard {
    private:
            vector <int> board;
            vector <bool> board_squares_in_use;
    public:
            CBoard() {
                    board.resize(100);
                    board_squares_in_use.resize(100);
            }

            int GetUnusedSquare()
            {
                    int rand_square;
                    int retry_count;

                    retry_count = 0;

                    // find an unused square to start the snake/ladder
                    // can't have multiple snakes/ladders on one square
                    for(;;)/>
                    {
                            // generate a random square (don't allow a snake/ladder to be created on square #1)
                            rand_square = 1 + Random(100 - 1);

                            if(board_squares_in_use.at(rand_square) == false)
                            {
                                    // found an unused square
                                    board_squares_in_use.at(rand_square) = true;

                                    break;
                            }

                            // this square is already used, try again
                            retry_count++;

                            // only retry 9999 times
                            if(retry_count > 9999)
                            {
                                    cout << "Failed to find unused square for snake/ladder after 9999 attempts (use less snakes or ladders)" << endl;

                                    exit(0);
                            }
                    }

                    return rand_square;
            }

            void InitBoard(int nbL, int nbS) {
                    // set all entries in board_squares_in_use to false
                    for(int i = 0; i < 100; i++)
                      {
                            board_squares_in_use.at(i) = false;
                    }

                    for (i = 0; i < nbS; i++){
                            // find an unused square to add the snake
                                                        int rand_square = GetUnusedSquare();

                                                        // calculate end of snake (must be lower than start position)
                                                        int end_pos = Random(rand_square + 1);

                            board.at(rand_square) = end_pos;
                    }
                    for (i = 0; i < nbL; i++) {
                            // find an unused square to add the ladder
                            int rand_square = GetUnusedSquare();

                                                        // calculate end of ladder (must be higher than start position but not higher than the grid size)
                                                        int end_pos = (rand_square + 1) + Random(100 - (rand_square + 1));

                            board.at(rand_square) = end_pos;
                    }
                    DisplayBoard();
                    system("pause");
                    cout << "Board initialised!" << endl;
            }

            int GetSquare(int ind) {
                    return board[ind];
            }

            void DisplayBoard() {
                    for (int i = 0; i < 100; i++) {
                            cout << i << " : " << board[i] << endl;
                    }
            }
    };

    void landOnBoard(int *pos, int player, CBoard* board) {
            int stock = board->GetSquare(*pos);
            if (stock != 0) {
                    if (stock > *pos) {
                            cout << "Player " << player << " hits a ladder and goes from " << *pos << " to " << stock << endl << endl;
                    }
                    else if (stock < *pos) {
                            cout << "Player " << player << " hits a snake and goes from " << *pos << " to " << stock << endl << endl;
                    }

                                        // update player position
                                        *pos = stock;
            }
            else {
                    cout << "Player " << player << " is now in position " << *pos << endl << endl;
            }
    }

int main(int argc, char* argv[])
    {
            int nbL = 0, nbS = 0, p1Pos = 0, p2Pos = 0, die = 0, nbRounds = 0;
            bool end = false;
            CBoard* board = new CBoard();

                        // set random seed (to ensure the values are different each time the program is run)
                        srand(time(NULL));

            cout << "How many ladders? (0-10)" << endl;;
            cin >> nbL;

                        // validate input
                        if(nbL == 0 || nbL > 10)
                        {
                                return 1;
                        }

            cout << "How many snakes? (0-10)" << endl;
            cin >> nbS;

                        // validate input
                        if(nbS == 0 || nbS > 10)
                        {
                                return 1;
                        }

            board->InitBoard(nbL, nbS);
            cout << "Start of the game" << endl << endl;
            while (!end) {
                    die = Random(6);
                    cout << "Player one rolls the die: " << die << endl;
                    p1Pos = p1Pos + die;
                    if (p1Pos == 100){
                            cout << "Player 1 wins!" << endl << endl;
                            break;
                    }
                    else if (p1Pos > 100) {
                            cout << "Player 1 is further than 100, back to the start." << endl;
                            p1Pos = 0;
                    }
                    landOnBoard(&p1Pos, 1, board);
                    die = Random(6);
                    cout << "Player two rolls the die: " << die << endl;
                    p2Pos = p2Pos + die;
                    if (p2Pos == 100){
                            cout << "Player 2 wins!" << endl << endl;
                            break;
                    }
                    else if (p2Pos > 100) {
                            cout << "Player 2 is further than 100, back to the start." << endl;
                            p2Pos = 0;
                    }
                    landOnBoard(&p2Pos, 2, board);
                    nbRounds++;
            }
            cout << "Game has been finished in " << nbRounds << " rounds" << endl;
            system("pause");
            return 0;
    }



Is This A Good Question/Topic? 0
  • +

Replies To: Snakes and Ladders Codeblocks error

#2 tarmizi_adam2005   User is offline

  • جوروترا

Reputation: 287
  • View blog
  • Posts: 986
  • Joined: 18-April 09

Re: Snakes and Ladders Codeblocks error

Posted 05 May 2014 - 09:11 AM

Hi,

Could you copy and paste the exact error massage that your compiler is telling you.
Was This Post Helpful? 0
  • +
  • -

#3 jcn1991   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-May 14

Re: Snakes and Ladders Codeblocks error

Posted 05 May 2014 - 09:12 AM

C:\Users\jonothan\Downloads\Laders\Untitled2.c|4|fatal error: iostream: No such file or directory|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 0 seconds) ===|
Was This Post Helpful? 0
  • +
  • -

#4 tarmizi_adam2005   User is offline

  • جوروترا

Reputation: 287
  • View blog
  • Posts: 986
  • Joined: 18-April 09

Re: Snakes and Ladders Codeblocks error

Posted 05 May 2014 - 09:17 AM

Try renaming your file from untitled2.c to untitled2.cpp instead. does it run ?
Was This Post Helpful? 0
  • +
  • -

#5 jcn1991   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-May 14

Re: Snakes and Ladders Codeblocks error

Posted 05 May 2014 - 09:19 AM

C:\Users\jonothan\Downloads\Laders\snicks.cpp||In member function 'void CBoard::InitBoard(int, int)':|
C:\Users\jonothan\Downloads\Laders\snicks.cpp|74|error: name lookup of 'i' changed for ISO 'for' scoping [-fpermissive]|
C:\Users\jonothan\Downloads\Laders\snicks.cpp|74|note: (if you use '-fpermissive' G++ will accept your code)|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 1 seconds) ===|
Was This Post Helpful? 0
  • +
  • -

#6 tarmizi_adam2005   User is offline

  • جوروترا

Reputation: 287
  • View blog
  • Posts: 986
  • Joined: 18-April 09

Re: Snakes and Ladders Codeblocks error

Posted 05 May 2014 - 09:26 AM

Your variable i is just in the scope of the first for loop on line 66:

for(int i = 0; i < 100; i++)


Try declaring i for the other loops. Instead of:

for (i = 0; i < nbS; i++)


try declaring int i for the other loop on line 71 and 80 also.

This post has been edited by tarmizi_adam2005: 05 May 2014 - 09:27 AM

Was This Post Helpful? 0
  • +
  • -

#7 jcn1991   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-May 14

Re: Snakes and Ladders Codeblocks error

Posted 05 May 2014 - 09:38 AM

It does now compile thanks, however it doesn't do exactly what I expected... I was hoping it you could press a key for each role of the dice, instead it just runs the through all the moves
Was This Post Helpful? 0
  • +
  • -

#8 tarmizi_adam2005   User is offline

  • جوروترا

Reputation: 287
  • View blog
  • Posts: 986
  • Joined: 18-April 09

Re: Snakes and Ladders Codeblocks error

Posted 05 May 2014 - 09:50 AM

Well then, maybe you would want to look back at your program logic.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1