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 6690217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:36:41+00:00 2026-05-26T05:36:41+00:00

Quick disclaimer, this is a contrived example meant to simulate an issue I am

  • 0

Quick disclaimer, this is a contrived example meant to simulate an issue I am seeing in my homework problem. To that end, using strings is out of the question; I can only use char arrays as per the instructor 🙁

What I am trying to do is continuously read input from the keyboard and store it in a vector . The problem is, whatever data I add to the vector is lost as soon as the addData function ends (when I try to view it, I see \320\366\277_\377). I believe this is due to the fact I am using a vector<char*>, so the vector can only use the data for as long as the pointer exists. However, my code cannot compile if I change it to a vector<char>, as I get errors saying cannot convert char* to char.

So, how can I save a char array (not a single char) to a vector element? Or, is there perhaps a better approach to this that would avoid the problem altogether?

#include <iostream>
#include <cstring>
#include <vector>

using namespace std;
const int MAX_BUFFER_SIZE = 80;

// class declaration
class Example {
public:
    void getData ();
    void addData ( char * newData );
    void displayData ();

private:
    vector<char*> vec;
};

// main function
int main () {
    bool quitProg;
    int quit;
    quitProg = false;
    Example shoopDaWhoop; // buffers cannot overflow if you shoop da whoop
    while (!quitProg) {
        shoopDaWhoop.getData();
        shoopDaWhoop.displayData();
        cout << "Type 1 if you want to exit... ";
        cin >> quit;
        if (quit == 1) {
            quitProg = true;
        }
    }
    return 0;
}

void Example::getData () {
    char userInput [MAX_BUFFER_SIZE];

    cout << "Enter text: ";
    cin.get(userInput, MAX_BUFFER_SIZE - 1, '\n');
    if ( cin.fail() ) { // data is invalid
        // clear and reset input stream
        cin.clear(ios::goodbit);
        cin.ignore(INT_MAX,'\n');
        // alert user they entered bad data
        cout << "That was bad data!" << endl;
    }
    else {
        // data is good, pass it to addData
        addData( userInput );
    }
}

void Example::addData ( char * newData ) {
    vec.push_back(newData);
    cout << "You entered: " << vec.back() << endl;


}
void Example::displayData () {
    for (int i=0; i<vec.size(); i++) {
        cout << "Item " << i << ": " << vec[i] << endl;
    }

}

  • 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-05-26T05:36:41+00:00Added an answer on May 26, 2026 at 5:36 am

    Use a std::vector<std::string>, that should “just work” with your existing code.

    Since you cant do this with std::string (which would have been the proper way to use the language), the you a nested vector, like this:

    std::vector<std::vector<char> > vex;  // notice the space between the '>' chars, older compilers may need it this way
    

    Then in your addData function:

    std::vector<char> tmp;
    
    while(*newData)
       tmp.push_back(*newData++);
    
    vec.push_back(tmp);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Quick disclaimer: I saw some plugins that truncate text strings but they do not
Quick preview of the problem: using .closest() , I'm able to stick a class
quick question about using ORMLite. I am trying to make sure that my implementation
Quick question. What do you think, I have a few sites that use a
Quick question. There is a legacy website (that is not under my control and
Quick question here about short-circuiting statements in C#. With an if statement like this:
Quick one for you guys. Say I have a function that outputs a string:
Quick question playing with PL SQL it seems that the tables column was named
Quick question. Is there an equivalent of @ as applied to strings in Java:
Quick question, does Kohana (version 3) automatically escape data that is passed into ORM::factory.....

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.