Why can't I read the input string using this scanf function?

CodeX

Input: 3 Elephant

Output: Ele

but the input string is not read.. any help?

Thanks in advance

#include <iostream>
#include <string>

using namespace std;

int main ()
{
    int n;
    string input;

    while ( scanf ("%d %s",&n, input ) != EOF ) 
    {
        string sub = input.substr(0,n);
        cout<< sub;
    }
    getchar();
    return 0;
}
Vlad from Moscow

scanf - is a C function. It knows nothing about C++ class std::string. Its format specifier %s is used to enter data in a character array.

You mixed two languages: C and C++. It is a bad style of programming. Instead of the loop

while ( scanf ("%d %s",&n, input ) != EOF ) 
{

        string sub = input.substr(0,n);
        cout<< sub;
}

use the following loop

while ( std::cin >> n >> input ) 
{

        string sub = input.substr(0,n);
        cout<< sub;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Read a string as an input using scanf

From Dev

I can´t print a string that was read using the "gets" function

From Dev

Why can't I pass a string to @input

From Dev

Why can't I scanf and printf an integer?

From Dev

Why can't I read Http Request Input stream twice?

From Dev

Why I can't read the change of global variables in a nested function?

From Dev

Why scanf can't parse the input separate by / mark?

From Dev

Why i can't read the text file using javascript and jquery?

From Dev

Why can't I read indefinately doubles using this loop?

From Dev

AT&T assembly + C functions. Using Scanf for string input

From Dev

I can't figure out why the input is not updating using MobX

From Dev

Read into std::string using scanf

From Dev

Why can't I find `"az"` in a string with this function?

From Dev

Why scanf cannot read my input?

From Dev

Why can't I get latitude and longitude using getBounds function?

From Java

Why can't I set the 'prototype' of a function created using 'bind'?

From Dev

why can't I change the variables using kwargs inside a function?

From Dev

Why can't I assign a function to a variable using prototype?

From Dev

why can't I change the variables using kwargs inside a function?

From Dev

Why can't I get latitude and longitude using getBounds function?

From Dev

Why I can't match a string using SQL and Access?

From Dev

Why does scanf function asks for an extra input?

From Dev

Why does scanf skip getting string input?

From Dev

Why doesn't scanf() wait for the next input if I previously entered a certain input

From Dev

Why can't I read whole file?

From Dev

Why can't I read whole file?

From Dev

Why I can't read file?

From Dev

Why doesn't scanf read the value correctly?

From Dev

Why can't I input it at once and output it?

Related Related

  1. 1

    Read a string as an input using scanf

  2. 2

    I can´t print a string that was read using the "gets" function

  3. 3

    Why can't I pass a string to @input

  4. 4

    Why can't I scanf and printf an integer?

  5. 5

    Why can't I read Http Request Input stream twice?

  6. 6

    Why I can't read the change of global variables in a nested function?

  7. 7

    Why scanf can't parse the input separate by / mark?

  8. 8

    Why i can't read the text file using javascript and jquery?

  9. 9

    Why can't I read indefinately doubles using this loop?

  10. 10

    AT&T assembly + C functions. Using Scanf for string input

  11. 11

    I can't figure out why the input is not updating using MobX

  12. 12

    Read into std::string using scanf

  13. 13

    Why can't I find `"az"` in a string with this function?

  14. 14

    Why scanf cannot read my input?

  15. 15

    Why can't I get latitude and longitude using getBounds function?

  16. 16

    Why can't I set the 'prototype' of a function created using 'bind'?

  17. 17

    why can't I change the variables using kwargs inside a function?

  18. 18

    Why can't I assign a function to a variable using prototype?

  19. 19

    why can't I change the variables using kwargs inside a function?

  20. 20

    Why can't I get latitude and longitude using getBounds function?

  21. 21

    Why I can't match a string using SQL and Access?

  22. 22

    Why does scanf function asks for an extra input?

  23. 23

    Why does scanf skip getting string input?

  24. 24

    Why doesn't scanf() wait for the next input if I previously entered a certain input

  25. 25

    Why can't I read whole file?

  26. 26

    Why can't I read whole file?

  27. 27

    Why I can't read file?

  28. 28

    Why doesn't scanf read the value correctly?

  29. 29

    Why can't I input it at once and output it?

HotTag

Archive