1

Why does printf output characters instead of data? Looking at the code, you can relatively understand what I want to do, but it is unclear why the output is like this

#include <vector>
#include <string>
#include <cstdio>

class Person
{
public:
    Person(const std::string& name, uint16_t old)
        : m_Name(name)
        , m_Old(old) 
    {
    }

public:
    std::string GetName() const { return m_Name; }
    uint16_t GetOld() const { return m_Old; }

private:
    std::string m_Name;
    uint16_t m_Old;
};

int main()
{
    std::vector<Person> person = { Person("Kyle", 26), Person("Max", 20), Person("Josiah", 31) };
    for (uint16_t i = 0; i < person.size(); ++i)
    {
        printf("Name: %s Old: %u\n", person[i].GetName(), person[i].GetOld());
    }
    return 0;
}


> // output
>     Name: Ь·╣ Old: 1701607755 
>     Name: Ь·╣ Old: 7889229 
>     Name: Ь·╣ Old: 1769172810
8
  • 2
    You need to use std::string::c_str() to print a std::string with "%s" in printf. Commented May 26, 2022 at 8:31
  • 2
    Alternatively, use std::cout which supports printing std::string as is. Commented May 26, 2022 at 8:33
  • 1
    In case if you build the code with -Wall or -W3 (MSVC) you get a warning with explanation, that %s is not compatible to std::string Commented May 26, 2022 at 8:37
  • 1
    @fabian or, you can use %hu instead, then no cast is needed Commented May 26, 2022 at 8:38
  • 2
    OT: Note that returning std::string from GetName() is inefficient, and usually unnecessary if you don't want to work with the copy of the string. Returning const std::string& may make more sense in this regard. Commented May 26, 2022 at 8:43

2 Answers 2

6

std::printf is a function from the C standard library. It does not know about the std::string class. You need to supply a C string (const char*) to %s. You can do that by calling the std::string::c_str() method.

printf("Name: %s Old: %u\n", person[i].GetName().c_str(), person[i].GetOld());
Sign up to request clarification or add additional context in comments.

Comments

4

Using printf() with "%s" requires a (const) char* (or something that decays into a (const) char*, like (const) char[]).

std::string has a c_str() method which returns a char const* that you can pass to printf().

Therefore, your printf() line should be:

printf("Name: %s Old: %hu\n", person[i].GetName().c_str(), person[i].GetOld());

Note: I also changed %u to %hu - see @RemyLebeau's comment.

Alternatively, you can use C++'s std::cout stream to print the std::string as-is:

#include <iostream>

std::cout << "Name: " << person[i].GetName() << " Old: " << person[i].GetOld() << std::endl;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.