2

So, I have a code when I prompt the user to enter a number, I want the size of the 2D array to be changed according to what the user entered, for example:

Enter a number between 1 and 12:
3

The 2D array then will change according to that size to become a 3x3 2D array, I hope I am as clear as I could be, I would really appreciate it if someone can please guide me to a solution, Thank you everyone !!

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>

using namespace std;

double Atemp = 0;
double Utemp = 0;
double Working = 0;
double Total = 0;
char Answer = 'x';
int Umain;



void displayOverview ();

void playOrQuit();

void promptNumber();

void fillUserArray(char grid[]);

int main(){

    displayOverview();

    playOrQuit();

    promptNumber();


    return 0;
}

void displayOverview(){

   }

void playOrQuit(){

    string playOrNot;

    cout << "If you want to play please press 'p' for play, and 'q' if you wish to quit\n";
    cin >> playOrNot;

    if(playOrNot == "p"){
        cout << "Awesome, lets start playing !!! \n";

    }if(playOrNot == "q"){
        cout << "Alright then, see you soon !!\n";
        exit(0);
    }if(playOrNot != "p" && playOrNot != "q"){
        cout << "Invalid entry !!\n";
        exit(0);
    }
}


void promptNumber(){

    do{
    cout << "Please Enter numbers between 1 and 12: ";
    cin >> Umain;
    if(Umain <= 12){
            for (Utemp = Umain; Utemp > 0; Utemp--){
            cout << "Please enter a number: ";
            cin >> Atemp;
        }
        }else{
            cout << "Not within limit :(\n";
        }
    }while (Answer == 'y');
}

void fillUserGrid (char grid[]){

    for( int row = 0; row < Umain; row++ ) {
        for(int col = 0; col < Umain; col++){
            grid[row][col] = userInput.at( row * Umain + col );
        }
    }
}

void outputUserGrid(char grid[]){

    for (int row = 0; row < Umain; row++){
        for(int col = 0; col < Umain; col++){
              cout << grid[row][col]<<" ";
        }
    cout << endl;
  }
}
DanielAA9
  • 91
  • 7
  • 1
    Possible duplicate of [How do I declare a 2d array in C++ using new?](https://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new) – Ken Y-N Nov 21 '18 at 04:47
  • @KenY-N I don't agree because OP didn't explicitly ask for `new`. – Swordfish Nov 21 '18 at 04:59
  • 1
    If you want an array-like thingy that you can resize, please use `std::vector grid;`, set its size to `rows * columns` and access it using `grid[y * columns + x];`. Thanks. – Swordfish Nov 21 '18 at 05:01
  • @Swordfish and neither did they ask for a 1D `std::vector` - they have `grid[row][col]` (although that function's parameter is wrong...). – Ken Y-N Nov 21 '18 at 05:03
  • This feels like XY Problem. I've answered accordingly. If it turns out you absolutely need a data structure that is the exact size you need, then the duplicate question mentioned earlier provides solutions... But scroll down to some of the [lesser-voted answers](https://stackoverflow.com/a/28841507/1553090) to find the _good_ solutions. – paddy Nov 21 '18 at 05:18

1 Answers1

2

It's quite legitimate to just use a 12x12 array for your game. When the user wants to play on a smaller grid, you can just use part of it.

The only times this might be an issue are if you're running into L1/L2 cache issues for performance-critical programs, or if you're storing lots of these grids and need to reduce memory use. I would be surprised if either of these scenarios applies to you.

paddy
  • 60,864
  • 6
  • 61
  • 103