473,396 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Int to string

1
So basically I want to convert a birth date, set to integers, and I want to convert it into a string. I recently got into object programming and created a method that would convert the date of birth into a string to print it

User.h
#pragma once
#include <string>
#include <iostream>
using std::cout;
using std::string;
class User
{
private:
string name;
string surname;
string place_birth;
int day;
int month;
int year;

public:
//setter e getter del name, surname, place birth and birth date
User();
User(string name, string surname, string place_birth, int day, int month, int year);

//Getter date birth
string get_date_birth();

//getter del palce birth
string get_pbirth();

//get user date
string get_User_Date();
};

---------------------------------------------------------

User.cpp
#include "Utente.h"

User::User(){
this-> name=" ";
this-> surname=" ";
this-> place_birth=" ";
this-> day=0;
this-> month=0;
this-> year=0;
}

User::User(string nome, string cognome, string luogo_nascita, int gg, int mm, int aa){
this->name=name;
this->surname=surname;
this->place_birth=place_birth;
this->day=day;
this->month=month;
this->year=year;
}

string User::get_date_birth(){
return "Date of birth>> " + std::to_string(this->date) + "/" + std::to_string(this->month) + "/" + std::to_string(this->year);[/b][/b][/b]
}

std::string User::get_pbirth(){
return place_birth;
}

string User::get_User_Date(){
cout<<"name>> "<<this->name<<std::endl;
cout<<"surname>> "<<this->surname<<std::endl;
cout<<"place of birth>> "<<this->place_birth<<std::endl;
return 0;
}

when i want print get_date_birth he take me this
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_M_construct null not valid
Dec 16 '22 #1
1 12090
momo1014
4 Nibble
I've write a int to string function in C, you can take a look at it.
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. void int_to_str(int num,char* s_r)
  4. {
  5.     char s[100];
  6.     int add_num;
  7.     int num_len = 0;
  8.     for (int i=0;num > 0;i++)
  9.     {
  10.         num_len++;
  11.         add_num = (num%10)+'0';
  12.         s_r[i] = add_num;
  13.         num = num/10;
  14.  
  15.     }
  16.     for (int i=0; i<num_len; i++)
  17.     {
  18.         s[i] = s_r[num_len-i-1];
  19.     }
  20.     for (int i=0; i<num_len; i++)
  21.     {
  22.         s_r[i] = s[i];
  23.     }
  24.     //s_r[num_len+1] = '\0';
  25.  
  26. }
  27.  
  28. int main()
  29. {
  30.     int a;
  31.     scanf("%d",&a);
  32.     char s[100];
  33.     int_to_str(a,&s);
  34.     printf("s = ");
  35.     printf(s);
  36.     printf("\n");
  37.  
  38. }
  39.  
first define a string (array of chars) and pass its POINTER to int_to_string(), along with the number you want to convert. The result will be stored in the array you passed.

I hope this can help. If it dose't work, don't take it to seriously. I've just began to learn C and not very good it (and not very good at English aswell).
Apr 30 '23 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
2
by: Andrew | last post by:
I have written two classes : a String Class based on the book " C++ in 21 days " and a GenericIpClass listed below : file GenericStringClass.h // Generic String class
18
by: JKop | last post by:
Can some-one please point me to a nice site that gives an exhaustive list of all the memberfunctions, membervariables, operators, etc. of the std::string class, along with an informative...
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
11
by: ramu | last post by:
Hi, Suppose I have a string like this: "I have a string \"and a inner string\\\" I want to remove space in this string but not in the inner string" In the above string I have to remove...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.