6970 Shares

How do I form a string using various variables in C++?

How do I form a string using various variables in C++? Topic: Case string c#
November 20, 2019 / By Airlia
Question: I have three ints: d, m and y which store the day, month and ,year respectively of a person's date of birth. How can I create a string using these three variables to output their birthday? For example, their birthday is January 1st, 2000. So d = 1, m = 1, and y = 2000. How can I pass these into a function which then forms the string "1/1/2000" and returns that as their date of birth?
Best Answer

Best Answers: How do I form a string using various variables in C++?

Tracey Tracey | 7 days ago
In the following code you also can find some number formatting in case - deviating from your assignment - you want to have a date like 01/01/2000. This is also to stress the flexibility of stringstream. #include #include #include using namespace std; string get_birthday(int d, int m, int y) { stringstream ss (stringstream::in | stringstream::out); ss << setfill('0'); ss << setw(2) << d << "/" << setw(2) << m << "/" << setw(4) << y; return ss.str(); } int main() { cout << get_birthday(1,1,1990); return 0; } -------------------- |error: 'struct std::ostream' has no member named 'str'|
👍 122 | 👎 7
Did you like the answer? How do I form a string using various variables in C++? Share with your friends

We found more questions related to the topic: Case string c#


Tracey Originally Answered: Write a method that returns a sorted string using the following header: public static String sort(String s)?
Guys, don't waste memory. public static String sort(String s) { if (s.length()==1) return s; char lowestChar = s.charAt(0); int lowestCharIndex = 0; for (int i = 1; i < s.length(); i++) if (s.charAt(i) < lowestChar) { lowestChar = s.charAt(i); lowestCharIndex = i; } return lowestChar + sort(s.substring(0, lowestCharIndex) + s.substring(lowestCharIndex + 1)); }

Red Red
I usually use a stringstream to work with numeric values and string values. Eg: #include #include using namespace std; int main(void) { stringstream ss; int myInt = 10; ss << "Concatenating text and integers: " << myInt; cout << ss.str() << endl; return 0; }
👍 40 | 👎 6

Mattie Mattie
Suppose, you have as ur example: // start of code #include void Birthday(int , int, int); int main() { int d = 1; int m = 1; int y = 2000; Birthday(d, m, y); return 0; } void Birthday (int d, int m, int y) { cout << d << "/" <
👍 34 | 👎 5

Joash Joash
stringstream, as already mentioned: std::string make_date(int d, int m, int y) {      std::ostringstream os;      os << d << '/' << m << '/' << y;      return os.str(); }
👍 28 | 👎 4

Joash Originally Answered: How can I use a function to split an integer (ex: date in yyyymmdd form) into 3 variables? (month, day & year)
You can use integer division (\) note the left leaning slash and MOD (modulo) division. (See Note below for C) Integer division only returns an integer without any remainder MOD returns only the remainder of a division Regular division for example 10/3 = 3.33333 Integer Division 10\3 = 3 Modulo 10 MOD 3 = 1 For today 1 April 2008 your integer YYYYMMDD is 20080401 To extract the year perform integer division 20080401 \ 10000 = 2008 save this result in your year variable recover the mmdd 20080401 mod 10000 = 401 (the remainder) now repeat the above extraction proceedure but using the mmdd remainder 401 \ 100 = 4 save as month 401 mod 100 = 1 save as day ***** NOTE : ******* The modulo operator in C is % In C the / division operator is used for integer division when dividing one integer by an other (NO Type casting) the result will be an integer even if the result is a real number EDIT: IQBAL brings up a valid point about the size of the INT value ONLY if you use an UNSIGNED SHORT integer which is limited to the value of 65535. An unsigned LONG integer can hold a value of 4,294,967,295 which is large enough to hold your date code. Since you specifically asked about processing an integer as a custom date code it is assumed that you know about variable types. For those who don't here's a list of INT types and there numeric values they may hold. unsigned short int 0 to 65535 short int -32768 to 32767 unsigned long int 0 to 4294967295 long int -2147483648 to 2147483647

If you have your own answer to the question case string c#, then you can write your own version, using the form below for an extended answer.
Descargar ebook Pdb Dylan dog 69: caccia alle streghe, Aprendo a dibujar 8 Descargue libros electrónicos en pdf gratuitos para esto, Descargas gratuitas de libros electrónicos de Adobe 978-8496764378 Dios es de derechas, Don paterson - 40 Sonnets 978-0571310890 Descargador de libros de google google, El divorci de berta barca mkt-0002293815 PDF uTorrent por Baltasar porcel mkt-0002293815, Descargas gratuitas de libros electrónicos para computadora .Net framework essentials por Thuan thaihoang q. lam DJVU EPUB 978-0596005054, Gestión de proyectos Epub ebooks para descargar gratis Las matemáticas y lo concreto, Simbologia del temple de la sagrada familia FB2 iBook EPUB 978-8484784043, David brin Gente de barrio. mkt-0003635467, Granada arabe. por Antonio torréns lópez DJVU PDF Antonio torréns lópez.