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

  • Home
  • SEARCH
  • 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 8984135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:01:08+00:00 2026-06-15T21:01:08+00:00

I am trying to get user input and put it into an array of

  • 0

I am trying to get user input and put it into an array of cstrings separated by a space. When I print the array to the screen though I get nothing. I am sure I am doing something stupid, but I have been stuck trying different ways for a while. Any help would be appreciated. Here is the code.

#include <iostream>
#include <cstring>

using namespace std;

void stuff(char command[][25], int length)
{
    char ch;


    for(int i = 0; i < length; i ++)
    {
        int b = 0;
        cin.get(ch);
        while(!isspace(ch))
        {
            command[i][b++] = ch;
            cin.get(ch);
        }

        command[i][b] = '\0';
        cout << endl;
    }


}

int main()
{
    char cha[10][25];
    char ch;
    int len = 0;
    while(ch != '\n')
    {
        cin.get(ch);
        if(isspace(ch))
            {
                len++;
            }
    }
    stuff(cha,len);
    for(int i = 0; i < len; i++)
    {
        cout << cha[i] << endl;
    } 
    cout << len << endl;

    return 0;
}
  • 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-15T21:01:09+00:00Added an answer on June 15, 2026 at 9:01 pm

    a) ch is undefined when you first test it with while (ch != '\n'). Initialize it to zero or something.

    b) You don’t write any values into cha in the while loop. Perhaps something like:

    int pos = 0;
    while(ch != '\n') {
        cin.get(ch);
        if (isspace((unsigned)ch)) {
           if (pos > 0) {
               ++len;
               pos = 0;
           }
        }
        else {
           cha[len][pos] = ch;
           ++pos;
        }
    }
    

    c) You are reading the stream again in stuff(...) but have already consumed what you wanted to get from the stream in main().

    d) This code suffers from a number of buffer overrun and stack corruption opportunities. perhaps use a std::vector<std::string> instead. If it runs out of memory it will throw an exception rather than make a mess on the stack.
    Perhaps something like this (untested):

    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    void main()
    {
        typedef std::vector<std::string> strvec;
        strvec cha;
        std::string s;
        char ch = 0;
        while(ch != '\n') {
            cin.get(ch);
            if (isspace((unsigned)ch)) {
                if (!s.empty()) {
                    cha.push_back(s);
                    s.clear();
                }
            }
            else {
                s.push_back(ch);
            }
        }
        // don't need to call 'stuff' to null terminate.
        for (strvec::iterator i = cha.begin(); i != cha.end(); ++i) {
            cout << *i << endl;
        }
        cout << cha.size() << endl;
    }
    

    This could be a little more efficient than it is but hopefully it is easy to understand.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get edit text to behave in this way: User can input
Hello I am trying to get the user profile picture and then merge into
I'm trying to get the user to put in a specific word. my code:
I am trying to get input from the user at the command prompt. The
I'm trying to figure out how to use scanf to get user input. I
I'm trying to get user GUID from Active Directory. My code: DirectoryEntry entry =
hi I am trying to get user profile information like this once the authentication
I am trying to get the user defined global hot key for my application.
I am trying to get if user is logined in facebook, if yes then
I'm trying to get the user to confirm if they want to delete a

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.