dimanche 23 décembre 2018

Compiling error while using getline() in C++

I keep getting this error (it's a really long one but I think the most important part is this):

main.cpp:9:30: note:   mismatched types 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>' and 'const char [2]'

while compiling this bit of code:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string x = getline(cin, " ");
    return 0;
}

The lines in the error won't match with the ones in the code I brought up here because I don't know how to make a new line whilst writing code in the Stack Overflow editor; I'm new here ;) Anyways, the error points to the line with the declaration of string x.

Basically what I want this code to do is to get a line from the user until he/she hits space. Maybe I'm doing something wrong from the beginning, so I'm open for suggestions of fixing this problem. (I'm not very experienced in C++, it's just that my teacher required to complete a task using this language.) Thanks,

Anthony

Aucun commentaire:

Enregistrer un commentaire