Thread: Help with Concatenating 2 integers

  1. #1
    Registered User
    Join Date
    Oct 2016
    Posts
    7

    Help with Concatenating 2 integers

    Hello everyone. It's already been 2 days since i've been stuck with one problem. Professor wants us to read the user's date of birth: 2 digits for the month and 2 digits for the day into a c-string that has room for the 4 digits entered example (0506) and then print the date of birth in May 6 format. Any help is appreciated. Thanks

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Ooh, do you have to use C-strings? If you can use std::string, it automatically supports string concatenation through operator+.

  3. #3
    Registered User
    Join Date
    Oct 2016
    Posts
    7
    Yes. He wants us to use C-strings. Im not sure how that would work.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What have you tried?

    Jim

  5. #5
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Well, basically allocate data to store the user input and allocate data to store the result of the string concatenation.

    Read in user input to allocated blocks, then using something strncat to concatenate.

  6. #6
    Registered User
    Join Date
    Oct 2016
    Posts
    7
    I tried this but it didnt work.
    Code:
    #include <iostream>
    #include <conio.h>
    #include <string>
    using namespace std;
    int main()
    
    
    {
    	//c-string: the original c language string.
    	int month, day;
    	string str;
    	char month1[3], day1[3];
    
    
    	cout << "Enter birth date: ";
    	cin >> str;
    	day = atoi(day1);
    	str = (month1 + day);
    	
    	month = atoi(month1);
    	
    	if (month == 01)
    		cout << "january";
    	else if (month == 02)
    		cout << "february";
    	else if (month == 03)
    		cout << "march";
    	else if (month == 04)
    		cout << "april";
    	else if (month == 05)
    		cout << "may";
    	else if (month == 06)
    		cout << "june";
    	else if (month == 07)
    		cout << "july";
    	else if (month == 8)
    		cout << "august";
    	else if (month == 9)
    		cout << "september";
    	else if (month == 10)
    		cout << "october";
    	else if (month == 11)
    		cout << "november";
    	else if (month == 12)
    		cout << "december";
    	else if (month > 12)
    		cout << "Invalid";
    
    
    	cout << str;
    	_getch();//to pause the program
    	return 0;
    }

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I thought you needed to use a C-string, not a std::string. Where is your code using a C-string to get the date from the user?

    Jim

  8. #8
    Registered User
    Join Date
    Oct 2016
    Posts
    7
    That's the part I cant figure out.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What exactly don't you understand?

    You were told to use a C-string that is large enough to hold the date entered by the user.
    Professor wants us to read the user's date of birth: 2 digits for the month and 2 digits for the day into a c-string that has room for the 4 digits entered example (0506)
    How do you create a C-string that is large enough to hold 4 digits?

    Jim

  10. #10
    Registered User
    Join Date
    Oct 2016
    Posts
    7
    Would i have Declare
    Char str[5]?
    Then use atoi to for conversion?

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The declaration is correct by before you can use atoi() you will need to somehow separate the day and the month. You may want to review your C textbook on how to split the C-strings.

    And don't forget to review your C textbook for methods on limiting how many characters the input will try to retrieve to avoid buffer overflows.

    Jim

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Just letting you know that any class that tells the students to use C-style strings over C++ strings isn't going to do you any favours out on the market. You're going to have to be prepared to relearn (and discard) most (or all) you've been taught in class.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    Why not just do integer addition?

    06/08

    (06 * 100) + 08 = 608

    Then a format specifier to output 4 digits with leading 0's if necessary. (0608)

    edit: Or the reverse I can see is what you're asking. If you just read both 2 digits into a 4 digit integer you can separate it with math...

    The integer 608.

    608 / 100 = 6
    608 % 100 = 8
    Last edited by cstryx; 10-11-2016 at 07:54 PM.

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I would do what cstryx recommends. Dates are small enough to fit in a long int.

    Just letting you know that any class that tells the students to use C-style strings over C++ strings isn't going to do you any favours out on the market.
    C++ in general might not do any favors out in the market. If you're a hobbyist like you claim you are, don't give people employment advice.
    Last edited by whiteflags; 10-12-2016 at 10:53 AM.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by whiteflags View Post
    C++ in general might not do any favors out in the market. If you're a hobbyist like you claim you are, don't give people employment advice.
    I'm not giving employment advice!
    I'm just saying - if you're going to work with C++ in the future--in whatever form, employed or not!--it's not going to do you any favour.
    It's not going to you any favour if you end up working with it.
    It's not going to you any favour if you end up just doing it for fun.
    It's not going to you any favour if you end up....
    ...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-26-2012, 10:50 AM
  2. concatenating a string
    By simpatico_qa in forum C Programming
    Replies: 8
    Last Post: 04-08-2009, 04:06 PM
  3. concatenating strings?
    By liri in forum C Programming
    Replies: 11
    Last Post: 08-24-2007, 05:34 PM
  4. concatenating two strings
    By js_badboy in forum C Programming
    Replies: 2
    Last Post: 09-08-2003, 08:27 PM
  5. Concatenating characters
    By fkheng in forum C Programming
    Replies: 4
    Last Post: 08-22-2003, 09:48 PM

Tags for this Thread