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.

  • 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:string`rather than array of char and `std::vector` for the array of string – bruno Jul 30 '20 at 07: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 for example. The vector replaces the array and the string replaces the c-style '0' terminated string – hetepeperfan Jul 30 '20 at 07:36
  • use `char arrayOfMessages[5][100];` then store message into arrayOfMessages directly. – makerj Jul 30 '20 at 07: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? – Yunnosch Jul 30 '20 at 07:40
  • @Yunnosch for sure, this is why I propose to use C++ string and vector ;) – bruno Jul 30 '20 at 07:41
  • @makerj i thought about that but the instructions for the task say i need to use an array of 5 spaces, not sure if the would allow a matrix of 5 by 100 , but this could be the best solution for now since using vectors and strings wouldn't allow me to the rest of the task requests that i didnt include here. – CHstudent22 Jul 30 '20 at 08:10

2 Answers2

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.
Waqar
  • 8,558
  • 4
  • 35
  • 43
  • I'd use `std::move` when inserting `message` as well. – Benny K Jul 30 '20 at 07:44
  • I wouldn't, until I am sure that `message` is not going to be used or changed to something else after moving. – Waqar Jul 30 '20 at 07:45
  • I was expecting `push_back`. Would you summarise the difference to `emplace_back` please? (honest question, C++ is not my strength) – Yunnosch Jul 30 '20 at 07:46
  • your proposal does not work, the OP said `submit a message like "the house is green"` but *message* will be only `the` – bruno Jul 30 '20 at 07:52
  • wouldn´t using string instead of char, store only the first word, and ignore the rest of the sentence. – CHstudent22 Jul 30 '20 at 07:53
  • @CHstudent22 if you want to read/store only the first word then that answer is what you want – bruno Jul 30 '20 at 07:54
  • @CHstudent22 yes, you will have to use `std::getline()` if you have more than one word in the string. – Waqar Jul 30 '20 at 07:54
  • @Yunnosch it doesn't matter here, both will do the same thing in this particular case. However if the string was temporary, `emplace_back` is more efficient as it constructs the object directly in the vector without creating a temporary. – Waqar Jul 30 '20 at 07:55
  • @waqar So if I use std::getline(message,100) and make message a string it would store the entire message "the house is green", and not just "the", cus tipically of i use cin>>message it would only store the first word – CHstudent22 Jul 30 '20 at 07:56
  • Thanks., that helps me to find the difference in the specs. – Yunnosch Jul 30 '20 at 07:57
  • 1
    @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: http://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](https://stackoverflow.com/a/36919571/2279422) if you want to read about it. – Waqar Jul 30 '20 at 08:05
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!!

  • While this works, you might want to take a look at `std::array` and `std::vector`, and avoid c-style arrays. Also see this: https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – Benny K Jul 30 '20 at 10:21