Home c String input and output

String input and output

Author

Date

Category

I want to read a string from the keyboard and display it. & Lt; null & gt; is displayed. What am I doing wrong?


Answer 1, authority 100%

if you want to read a string, then declare a string, not a character.

# include & lt; stdio.h & gt;
int main ()
{
  char f [1024]; // I think 1024 characters will be enough for the first time
  scanf_s ("% s", & amp; f); // this is actually a Microsoft hack,
  // not a normal function, but let it be.
  printf ("% s", f);
}

Answer 2, authority 99%

@Dazar your first fundamental mistake is that instead of a line, you use everything one character

...
char f;
...

exactly the string must be used, for example:

# include & lt; iostream & gt;
#include & lt; string & gt;
using namespace std;
int main ()
{
   char s [255];
   cin.getline (s, 255);
   cout & lt; & lt; s & lt; & lt; endl;
}

Answer 3

This is how it should be in C++

# include & lt; iostream & gt;
#include & lt; string & gt;
using namespace std;
int main ()
{
   string s;
   cin & gt; & gt; s;
   cout & lt; & lt; s;
}

Answer 4

#include & lt; iostream & gt;
 #include & lt; string & gt;
 using namespace std;
 int main () {
   cout & lt; & lt; "write smth:";
   string s;
   getline (cin, s);
   cout & lt; & lt; "u typed:" & lt; & lt; s;
 }

Another option for solving the problem


Answer 5

Or else so in C++

# include & lt; iostream & gt;
#include & lt; cstdio & gt;
using namespace std;
int main ()
{
char f [80];
cout & lt; & lt; "Vvedite stroku";
gets (f);
cout & lt; & lt; "Stroka";
cout & lt; & lt; f;
return 0;
}

Answer 6

A string is a String
string S;
And you write Char
char S;

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions