#need help to fix that

52 messages · Page 1 of 1 (latest)

shadow stratus
#

so you want to manually input a line of text and the program repeats what you typed?

#

#include <iostream>
#iclude <string>

void printText(){
std::getline(std::cin, printText);
std::cout <<printText;
}

int main(){
printText();
}

#

something like that maybe?

lethal folio
#

#incude <iostream> then?

shadow stratus
#

yes

shadow stratus
#

yes, in main you mention printext instead of printText

#

oh nevermind

#

could you send the error message?

slate widget
#

#include <iostream>

#

its a systemic lib

shadow stratus
#

is this in C or in C++? because C++ needs a class, usualy you would use main as class and printext would become a method within that class

slate widget
#

also getline buffer is incorrect

#

ur setting it as the function

#

it should be a char array instead

shadow stratus
#

oh and i just noticed you used prinText for method name as well as the string name(wich you didn't initialize)

slate widget
#

;compile

#include <iostream>

void prinText(){
  char text[1024];
  std::getline(std::cin, text);
  std::cout << text << std::endl;
}

int main(){
    prinText();
    return 0;
}
quasi forgeBOT
#
Compiler Output
<source>: In function 'void prinText()':
<source>:5:15: error: no matching function for call to 'getline(std::istream&, char [1024])'
    5 |   std::getline(std::cin, text);
      |   ~~~~~~~~~~~~^~~~~~~~~~~~~~~~
In file included from /opt/compiler-explorer/gcc-13.2.0/include/c++/13.2.0/string:55,
                 from /opt/compiler-explorer/gcc-13.2.0/include/c++/13.2.0/bits/locale_classes.h:40,
                 from /opt/compiler-explorer/gcc-13.2.0/include/c++/13.2.0/bits/ios_base.h:41,
                 from /opt/compiler-explorer/gcc-13.2.0/include/c++/13.2.0/ios:44,
                 from /opt/compiler-explorer/gcc-13.2.0/include/c++/13.2.0/ostream:40,
                 from /opt/compiler-explorer/gcc-13.2.0/include/c++/13.2.0/iostream:41,
                 from <source>:1:
/opt/compiler-explorer/gcc-13.2.0/include/c++/13.2.0/bits/basic_string.tcc:893:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(basic_is
slate widget
#

its a function

#

not a name

#

;compile

#include <iostream>
#include <string>
void prinText(){
  char text[1024];
  std::getline(std::cin, text);
  std::cout << text << std::endl;
}

int main(){
    prinText();
    return 0;
}
quasi forgeBOT
#
Compiler Output
<source>: In function 'void prinText()':
<source>:5:15: error: no matching function for call to 'getline(std::istream&, char [1024])'
    5 |   std::getline(std::cin, text);
      |   ~~~~~~~~~~~~^~~~~~~~~~~~~~~~
In file included from /opt/compiler-explorer/gcc-13.2.0/include/c++/13.2.0/string:55,
                 from /opt/compiler-explorer/gcc-13.2.0/include/c++/13.2.0/bits/locale_classes.h:40,
                 from /opt/compiler-explorer/gcc-13.2.0/include/c++/13.2.0/bits/ios_base.h:41,
                 from /opt/compiler-explorer/gcc-13.2.0/include/c++/13.2.0/ios:44,
                 from /opt/compiler-explorer/gcc-13.2.0/include/c++/13.2.0/ostream:40,
                 from /opt/compiler-explorer/gcc-13.2.0/include/c++/13.2.0/iostream:41,
                 from <source>:1:
/opt/compiler-explorer/gcc-13.2.0/include/c++/13.2.0/bits/basic_string.tcc:893:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(basic_is
slate widget
#

oh

#

nvm

#

it expects an input lmfao

#

try my code ive given u

#

it should work

#

the buffer is correct now

#

what is it

#

getline expects a buffer where the text is supposed to be written, ur input

shadow stratus
#

he made text char array

#

@slate widget is there a reason to use char array in stead of string?

slate widget
#

string just holds the char array and modifies it

#

according to ur needs

#

why?

#

??

#

can u just provide me the error

#

just copy paste the error lmao

#

or atleast take a screenshot

#

ah

#

so its a must to have a string obj

#
#include <iostream>
#include <string>
void prinText(){
  std::string text;
  std::getline(std::cin, text);
  std::cout << text << std::endl;
}

int main(){
    prinText();
    return 0;
}
#

try now

#

read the error lmao

#

mismatched types

#

read it

shadow stratus
#

no problem

slate widget
#

u should read the docs tho

shadow stratus
#

i m just a bit confused as to why it does not accept char arrays but string is fine