83.5k views
0 votes
I'm new to programming and I'm starting with c++, so the first program I want to write should take a string of characters and output it to the console afterwards, but the execution isn't working, I can't find the issue.

'#include
#include
#include
using namespace std;

int main()
{
short T;
cin >> T;

char *str;
//cin.clear();
cin.sync();
cin.ignore(1000, '\\');

while(cin >> *str++)
;

cin.sync();
cin.ignore(1000, '\\');
while(*str++ != '\0')
cout << *str;

return 0;
}
'

2 Answers

4 votes
Just reading and writing a string should be as easy as:

string msg;
getline(cin, msg);
cout << "Hello " << msg;
return 0;
User Jumshud
by
7.3k points
4 votes
#include <iostream>
#include <string>

using namespace std;

int main() {

string userInput;
cout << "Enter word" << endl;

cin >> userInput;

cout << "you entered" << endl;
cout << userInput;

return 0;

}
User Abhinavkulkarni
by
7.0k points