0

Hi so I'm writing code in C++ that asks the user to submit a message like "the house is green" and then store it an array that stores messages so this is what i have so far

#include <iostream> 

using namespace std;

char message[100];//limits the message to 99 characters.

char arrayOfMessages [5];

cout<<"Please enter the message";

cin>>message; 

I can´t figure out a way to make

arrayOfMessages[0]= message; // since doing this only stores the first position 

Appreciate the help or suggestions if I should do something different in obtaining the message. Also this is an over simplified version but is the gist of what im trying, however im trying to make the array message be temporary so i can reuse it to request up to 5 messages from the user , in my code version I did this with a while cycle.

6
  • 3
    use char * arrayOfMessages [5]; and you will able dto do arrayOfMessages[0]= message; ... but the best is to not use these arrays and use a std:stringrather than array of char and std::vector<std::string> for the array of string Commented Jul 30, 2020 at 7:35
  • 1
    If you are using C++, you probably shouldn't be using raw arrays and c-style character strings. You should be using std::vector<std::string> for example. The vector replaces the array and the string replaces the c-style '0' terminated string Commented Jul 30, 2020 at 7:36
  • use char arrayOfMessages[5][100]; then store message into arrayOfMessages directly. Commented Jul 30, 2020 at 7:36
  • @bruno That seems to risk filling all array entries with pointers to the same buffer, resulting in probably undesired behaviour of all messages on output being the last one from input. Or am I wrong? Commented Jul 30, 2020 at 7:40
  • @Yunnosch for sure, this is why I propose to use C++ string and vector ;) Commented Jul 30, 2020 at 7:41

2 Answers 2

4

Use std::vector and std::string:

#include <iostream>
#include <vector>
#include <string> 

int main() {
    //char message[100];//limits the message to 99 characters.
    std::string message; //use std::string instead of char[]
    
    std::vector<std::string> arrayOfMessages;
    arrayOfMessages.reserve(5); //reserve space for 5 strings

    std::cout << "Please enter the message";

//    std::cin >> message; 
    std::getline(std::cin, message); //use this if there's more than one word

    arrayOfMessages.emplace_back(message); // put the message in the array
}
  • std::vector is a dynamic array which can contain elements of any one type. Here we store std::string type in it. It will automatically grow. For example if you have 6 strings, it's size will automatically increase to 6 when you emplace_back another element.
  • std::string is how we do strings in C++. char [] is also possible, but don't use it unless you really have a very good reason to.
  • emplace_back will append the string to the end of the array.
Sign up to request clarification or add additional context in comments.

11 Comments

I'd use std::move when inserting message as well.
I wouldn't, until I am sure that message is not going to be used or changed to something else after moving.
I was expecting push_back. Would you summarise the difference to emplace_back please? (honest question, C++ is not my strength)
your proposal does not work, the OP said submit a message like "the house is green" but message will be only the
@Yunnosch also, emplace_back can call explicit constructors directly without explictly mentioning them, while with push_back either uses implicit constructors or you have to explictly call the constructor. This can be good / bad depending upon the situation. See: coliru.stacked-crooked.com/a/fb0abf2c0a217454 This can sometimes lead to problems, which are hard to explain in a comment so I will link to this SO Answer if you want to read about it.
|
0

So I found the simplest answer was to simply change the

char arrayOfMessages [5];

to

string arrayOfMessages [5];

and then just do a simple

arrayOfMessages [0]=message;

And that worked, so thanks for everyone's help and suggestions!!

1 Comment

While this works, you might want to take a look at std::array and std::vector, and avoid c-style arrays. Also see this: stackoverflow.com/questions/1452721/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.