Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8714197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:32:10+00:00 2026-06-13T05:32:10+00:00

I was wondering does anyone know how to convert a string into a 2d

  • 0

I was wondering does anyone know how to convert a string into a 2d array? This was my attempt:

string w;
char s[9][9];
int p=0;
getline(cin, w);
while(p != w.size())
{
  for (int k = 0; k < 9; k++)
  {
    for(int j = 0; j < 9; j++)
    {
      s[k][j] = w[p];
      p++;
    }
  }
}
  cout << "nums are: " << endl;
  for(int k = 0; k < 9; k++)
  {
    for(int j = 0; j <9; j++)
    {
      cout << s[k][j];
    }
  }

But the numbers don’t print out correctly. I want s[k][j] to print out everything in w but it simply prints out gibberish. I also noticed if i do string[81] then I get a whole bunch of errors. Could anyone help me? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:32:12+00:00Added an answer on June 13, 2026 at 5:32 am

    Try this:

    const int NUM_ROWS = 9;
    const int NUM_COLS = 9;
    
    string w;
    char s[NUM_ROWS][NUM_COLS];
    
    getline(cin, w);
    
    if (w.size() != (NUM_ROWS * NUM_COLS))
    {
        cerr << "Error! Size is " << w.size() << " rather than " << (NUM_ROWS * NUM_COLS) << endl;
        exit(1);
    }
    
    for (int count = 0; count < w.size(); count++)
    {
        if (!isdigit(w[count]) && w[count] != '.')
        {
            cerr << "The character at " << count << " is not a number!" << endl;
        }
    }
    
    for (int row = 0; row < NUM_ROWS; row++)
    {
        for(int col = 0; col < NUM_COLS; col++)
        {
            s[row][col] = w[col + (row * NUM_COLS)];
        }
    }
    
    cout << "Nums are: " << endl;
    
    for(int row = 0; row < NUM_ROWS; row++)
    {
        for(int col = 0; col < NUM_COLS; col++)
        {
            cout << s[row][col] << " ";
        }
    
        cout << endl;
    }
    

    Based on our chat, you might want this:

    const int NUM_ROWS = 9;
    const int NUM_COLS = 9;
    
    string w;
    char s[NUM_ROWS][NUM_COLS];
    
    while (!cin.eof())
    {
        bool bad_input = false;
    
        getline(cin, w);
    
        if (w.size() != (NUM_ROWS * NUM_COLS))
        {
            cerr << "Error! Size is " << w.size() << " rather than " << (NUM_ROWS * NUM_COLS) << endl;
            continue;
        }
    
        for (int count = 0; count < w.size(); count++)
        {
            if (!isdigit(w[count]) && w[count] != '.')
            {
                cerr << "The character at " << count << " is not a number!" << endl;
                bad_input = true;
                break;
            }
        }
    
        if (bad_input)
            continue;
    
        for (int row = 0; row < NUM_ROWS; row++)
        {
            for(int col = 0; col < NUM_COLS; col++)
            {
                s[row][col] = w[col + (row * NUM_COLS)];
            }
        }
    
        cout << "Nums are: " << endl;
    
        for(int row = 0; row < NUM_ROWS; row++)
        {
            for(int col = 0; col < NUM_COLS; col++)
            {
                cout << s[row][col] << " ";
            }
    
            cout << endl;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know if something like this is possible in Scala: case class Thing(property:String)
Hello: I have been experimenting with WPF this afternoon, Im wondering does anyone know
I was wondering does anyone know if its possible to open a wifi and
I am wondering does anyone know if it is possible to render html in
Does anyone know how the cancelAllLocalNotifications method works on UIApplication? I'm wondering specifically how
I was wondering if anyone knows how to do this... I know how to
Does anyone know why NHibernate generates a field named 'elt' of type int for
I'm just wondering, does anyone know, whether uniqid function in PHP has a certain
Does anyone know of a Python replacement for Matlab / Octave bwdist() function? This
Does anyone know how many concurrent connections IE 9 allows while in other browser

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.