1

My goal is to prompt user to enter a message / sentence and then print it out on the screen, using getline(). The following is two different attempts I have tried out.

First Attempt:

#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
int main(){

    chat message[80];
    cout << "\n what is your message today?" << endl;

    cin.getline( message, 80); // Enter a line with a max of 79 characters.
  if( strlen( message) > 0)  // If string length is longer than 0.
    {

      for( int i=0; message[i] != '\0'; ++i)
          cout << message[i] << ' ';
      cout << endl;

   }
 }

Second Attempt:

#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
int main(){

    string a = "a string";
    cout << "\n what is your message today?" << endl;
    while(getline(cin,a))
        cout << a;
    cout<<endl

   }
 }

For the fist attempt, the code simply print out "what is your message today?" and quit. I do not have a chance to enter any string at all. For the second attempt, it keeps asking me enter the message. Each time, when I enter something with the "\n", it would display what I entered on the screen. I use control + c to interrupt the running process to make it stop.

EDIT: To clarify and explain on my side, I extract the first attempt from a longer code, which is as the following.

#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;

char header[] = "\n *** C Strings ***\n\n";  // define a c string 
int main()
{
  char hello[30] = "Hello ", name[20], message[80];  // define a c string hello, declare two other c strings name and message
  string a="fivelength";
  cout << header << "Your first name: ";
  cin >> setw(20) >> name;      // Enter a word.


  strcat( hello, name);      // Append the name.
  cout << hello << endl;
  cin.sync();                // No previous input.
  cout << "\nWhat is the message for today?"
       << endl;

  cin.getline( message, 80); // Enter a line with a max of 79 characters.
  if( strlen( message) > 0)  // If string length is longer than 0.
    {

      for( int i=0; message[i] != '\0'; ++i)
          cout << message[i] << ' ';
      cout << endl;

   }
return 0;
}

For the above code, it does not give me a chance to enter a message on the screen. I will put it as another question.

kkxx
  • 571
  • 1
  • 5
  • 16

1 Answers1

6

You are overcomplicating this, you can simply use std::string, which is the de-facto C++ string, and call the method, without using a loop.

You don't need a loop, since you are not going to repeatedly read lines, but only want to read one line, so no loop is needed.

#include <iostream>
#include <string> // not cstring, which is the C string library

using namespace std;

int main(void)
{
    string message; // it can be an empty string, no need to initialize it
    cout << "What is your message today?" << endl;
    getline(cin, message);
    cout << message;
    cout<<endl;

    return 0;
}

Output (Input: "Hello Stack Overflow!"):

What is your message today?
Message: Hello Stack Overflow!

PS: As @fredLarson commented, if you change chat to char in your first example, it should work. However, that code has a lot of commonalities with C.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 2
    Yeah, I like the `std::string` version better too. I think the only error the OP had on that one was putting it in a loop. – Fred Larson Oct 31 '19 at 18:18
  • Ah @FredLarson good observation, I saw the `cstring` header and was discouraged, I updated my answer, thanks. – gsamaras Oct 31 '19 at 18:21