473,503 Members | 6,385 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Int to string

1 New Member
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 12131
momo1014
4 New Member
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
6703
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
31156
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
7987
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
3668
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
8147
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
4759
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
2393
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
4273
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
50132
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
3035
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
7194
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7070
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
7267
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6976
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
5566
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,...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.