简体   繁体   中英

What is causing this output typo?

I am coding this simple program which is supposed to create a user profile with given attributes. I am testing the method view_profile() which is supposed to just return a string with the values given from the constructor. When I run the program I get no errors, but the output is not expected. I am wondering why in the output the Name: Sam Drakillanouns is appearing, instead of Name: Sam Drakilla, also, age variable is not showing.

#include <iostream>
#include <vector>
using namespace std;

class Profile {
private:

  string name;
  int age;
  string city;
  string country;
  string pronouns;
  vector<string> hobbies;

public:
  
  //Constructor
  Profile(string name1, int age1, string city1, string country1, string pronouns1 = "they/them") {
    name = name1;
    age = age1;
    city = city1;
    country = country1;
    pronouns = pronouns1;
  }

  //View Profile method
  string view_profile() {
    string bio = "Name: " + name;
    bio += "\nAge: " + age;
    bio += "\nCity: " + city;
    bio += "\nCountry: " + country;
    bio += "\nPronouns: " + pronouns;
    bio += "\n";
    return bio;
  }

};

int main() {
  Profile sam("Sam Drakkila", 30 , "New York", "USA", "he/him");
  cout << sam.view_profile();
}

My output is:

Name: Sam Drakillanouns:
City: New York
Country: USA
Pronouns: he/him

when is should be:

Name: Sam Drakilla
Age: 30
City: New York
Country: USA
Pronouns: he/him

I tried to check the constructor and method view_profile() but everything seems to be correct. I may be overlooking something since I am new to the C++ language. Any help is appreciated thank you.

bio += "\nAge: " + age;

The issue is that age is an int , and there is no overload of + that converts an int into a type that can be concatenated onto a std::string .

To fix this, the age must be converted to a string. A simple way is to use std::to_string :

bio += "\nAge: " + std::to_string(age);


As to what is happening when you didn't convert the integer to a string:

Since (I am assuming ASCII) the value of 30 is the "record separator", one of the control characters. So you basically wound up concatenating a control character onto the string, and not the string "30" .

The operator + and += for std::string will allow single characters to be concatenated, and unfortunately, the code compiled with no errors due to the integer being converted to a character value.


Edit:

After looking more closely at your original code, it seems with the code you did have, the actual behavior is to increment the string literal "\nAge: " by 30 bytes, pointing to who-knows-where. This will actually yield undefined behavior if std::string uses this value as one to concatenate with.

Regardless, the age is converted to an integer 30, and whether it was used to "add" to the string as mentioned before, or increment a literal by 30 bytes, it still yielded the undesired behavior.

Adding an int into a string is the source of your problem. I suggest you use a std::stringstream to build up your string, and view_profile can be marked const .

  std::string view_profile() const {
    std::ostringstream ss;
    ss << "Name: "     << name     << std::endl
       << "Age: "      << age      << std::endl
       << "City: "     << city     << std::endl
       << "Country: "  << country  << std::endl
       << "Pronouns: " << pronouns << std::endl;

    return ss.str(); 
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM