473,326 Members | 2,012 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,326 software developers and data experts.

strings

C
I want to write a function that returns a data as a string object like
this:

string function(int day, int month, int year){
return(string(day) + "/" + string(month) + "/" + string(year))
};

But this doesn't work.
Can anyone tell me the best solution to do this ?
Oct 2 '05 #1
5 2302
C@limero wrote:
I want to write a function that returns a data as a string object like
this:

string function(int day, int month, int year){
return(string(day) + "/" + string(month) + "/" + string(year))
};

But this doesn't work.
Can anyone tell me the best solution to do this ?


That's not how you convert an integer value to a string in C++.

http://www.gotw.ca/publications/mill19.htm
Oct 2 '05 #2

"C@limero" <no****@nep.invalid> wrote in message
news:9e********************************@4ax.com...
I want to write a function that returns a data as a string object like
this:

string function(int day, int month, int year){
return(string(day) + "/" + string(month) + "/" + string(year))
};
Use atoi function to convert integers to chars;
for example;

string all;
char *zz;
zz = new char[20]; // change 20 to max # of chars
all += atoi(day,zz,10);
all += atoi(month,zz,10);
all += atoi(year,zz,10);
delete [] zz;
return all;


But this doesn't work.
Can anyone tell me the best solution to do this ?

Oct 2 '05 #3

"C@limero" <no****@nep.invalid> wrote in message
news:9e********************************@4ax.com...
I want to write a function that returns a data as a string object like
this:

string function(int day, int month, int year){
return(string(day) + "/" + string(month) + "/" + string(year))
};

But this doesn't work.
Can anyone tell me the best solution to do this ?


#include <iostream>
#include <sstream>
#include <string>

string function(int day, int month, int year, char delim = '/')
{
std::ostringstream oss;
oss << day << delim << month << delim << year;
return oss.str();
};

int main()
{
std::cout << function(2, 10, 2005) << '\n';
return 0;
}

-Mike

Oct 2 '05 #4
C
On Sun, 02 Oct 2005 17:50:39 +0200, "C@limero" <no****@nep.invalid>
wrote:
I want to write a function that returns a data as a string object like
this:

string function(int day, int month, int year){
return(string(day) + "/" + string(month) + "/" + string(year))
};

But this doesn't work.
Can anyone tell me the best solution to do this ?


Thanks to all !
Oct 2 '05 #5
On Sun, 2 Oct 2005 12:13:09 -0400, "Someonekicked"
<so***********@comcast.net> wrote in comp.lang.c++:

"C@limero" <no****@nep.invalid> wrote in message
news:9e********************************@4ax.com...
I want to write a function that returns a data as a string object like
this:

string function(int day, int month, int year){
return(string(day) + "/" + string(month) + "/" + string(year))
};
Use atoi function to convert integers to chars;
for example;


No, please don't. The ato... functions are antique hacks that produce
undefined behavior if the converted value is out of range for the
destination type. They should never be used in any C or C++ program.
string all;
char *zz;
zz = new char[20]; // change 20 to max # of chars
all += atoi(day,zz,10);
all += atoi(month,zz,10);
all += atoi(year,zz,10);
delete [] zz;
return all;


And the ato... functions go the wrong way. They convert text
representations of numerical values to numeric representation.

The OP wants to go in the opposite direction.

But this doesn't work.
Can anyone tell me the best solution to do this ?


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Oct 2 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Ravi | last post by:
Hi, I have about 200GB of data that I need to go through and extract the common first part of a line. Something like this. >>>a = "abcdefghijklmnopqrstuvwxyz" >>>b = "abcdefghijklmnopBHLHT"...
17
by: Gordon Airport | last post by:
Has anyone suggested introducing a mutable string type (yes, of course) and distinguishing them from standard strings by the quote type - single or double? As far as I know ' and " are currently...
16
by: Paul Prescod | last post by:
I skimmed the tutorial and something alarmed me. "Strings are a powerful data type in Prothon. Unlike many languages, they can be of unlimited size (constrained only by memory size) and can hold...
4
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three...
25
by: Rainmaker | last post by:
Hi, Can anyone tell me an efficient algorithm to sort an array of strings? Keep in mind that this array is HUGE and so the algorithm should me efficient enough to deal with it. Thanks
6
by: Broeisi | last post by:
Hello, I wrote the tiny progam below just to understand arrays and strings better. I have 2 questions about arrays and strings in C. 1. Why is it that when you want to assign a string to an...
2
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be...
19
by: pkirk25 | last post by:
I wonder if anyone has time to write a small example program based on this data or to critique my own effort? A file called Realm List.html contains the following data: Bladefist-Horde...
95
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.