Int to string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Edoks
    New Member
    • Dec 2022
    • 1

    Int to string

    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(stri ng nome, string cognome, string luogo_nascita, int gg, int mm, int aa){
    this->name=name;
    this->surname=surnam e;
    this->place_birth=pl ace_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_pbirt h(){
    return place_birth;
    }

    string User::get_User_ Date(){
    cout<<"name>> "<<this->name<<std::end l;
    cout<<"surname> > "<<this->surname<<std:: endl;
    cout<<"place of birth>> "<<this->place_birth<<s td::endl;
    return 0;
    }

    when i want print get_date_birth he take me this
    terminate called after throwing an instance of 'std::logic_err or'
    what(): basic_string::_ M_construct null not valid
  • momo1014
    New Member
    • Apr 2023
    • 4

    #2
    I've write a int to string function in C, you can take a look at it.
    Code:
    #include <stdio.h>
    
    void int_to_str(int num,char* s_r)
    {
        char s[100];
        int add_num;
        int num_len = 0;
        for (int i=0;num > 0;i++)
        {
            num_len++;
            add_num = (num%10)+'0';
            s_r[i] = add_num;
            num = num/10;
            
        }
        for (int i=0; i<num_len; i++)
        {
            s[i] = s_r[num_len-i-1];
        }
        for (int i=0; i<num_len; i++)
        {
            s_r[i] = s[i];
        }
        //s_r[num_len+1] = '\0';
    
    }
    
    int main()
    {
        int a;
        scanf("%d",&a);
        char s[100];
        int_to_str(a,&s);
        printf("s = ");
        printf(s);
        printf("\n");
        
    }
    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).

    Comment

    Working...