Pulling two digits from integer?

Pages: 12
Sep 9, 2008 at 11:40pm
I had previously posted about a program that is converting a numerical date over into the correct spelled expression. Example:Input 20080101 Output: January 1, 2008.

My instructor tells me to use one variable for the input of the date. And then pull out the two digits for my if statements to convert that number into the correct month.

Here is my if statements:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

// THIS CODE IS INCOMPLETE, MY WORKING PROGRESS
#include <iostream>
using namespace std;

int main () {
    
    int month, date;

    
    cout << "Please enter a date in the following format: YYYYMMDD" << endl;
    cin >> date;
    
    if ( month >= 1 && month <= 12 ){
       else if (month == 1)
            cout << January << endl;
       else if (month == 2)
            cout << February << endl;
       else if (month == 3 )
            cout << March << endl;
       else if (month == 4 )
            cout << April << endl;
       else if (month == 5 )
            cout << May << endl;
       else if (month == 6)
            cout << June << endl;
       else if (month == 7)
            cout << July << endl;
       else if (month == 8)
            cout << August << endl;
       else if (month == 9)
            cout << September << endl;
       else if (month == 10)
            cout << October << endl;
       else if (month == 11)
            cout << November << endl;
       else if (month == 12)
            cout << December << endl;
    }
    else
        cout << "Invaid Format" << endl;  
    
    cout << "**************************************" << endl;
    cout << date << " is the date:" << endl;
    
    system("PAUSE");
    return 0;
   
  
}


I'm not asking for my homework to be completed for me. I'm just looking for a start on the "pulling out the digits from the integer". Thanks for any help you may have.
Sep 10, 2008 at 12:57am
You can try taking in the date as a string. With string you can use thing like length, position, find pos...etc. To seperate them....Then use stringstream to convert it back into int. hehe. I don't know, but that is how I would go about it. :). Ex:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <sstream>
#include <string>

using namespace std;
int main(){
	cout << "Please enter date: "; 
	string input("0"); 
	cin >> input; 
	
	string year = input.substr(0,4); 
	int yearInt(0); 
	
	stringstream(year) >> yearInt; //<--convert the string into an integer. 
	cout << yearInt; //<---Do what you want with the number. 
}//main
Last edited on Sep 10, 2008 at 1:11am
Sep 10, 2008 at 1:34am
I wish I could do it like that. There expecting me to do this(convert the expression) with Arithmetic Operators (specifically the % and /). I'm totally lost on this issue.
Sep 10, 2008 at 8:26am
Just use appropriate multiples of 10 as the divisor,
EG
20080101 / 10000 = 2008
20080101 % 10000 = 101
(Provided you are using int types).
Sep 10, 2008 at 9:06am
Do you have to use formulae to convert this or will simple subtraction and addition be acceptable?
For a simple method I suggest:
yyyymmdd - yyyy, then mmdd - dd, if you get my meaning?

Its possible to do it this way but I'm not sure if your instructor will accept it. If not you should try working with Faldrax's method.
Sep 10, 2008 at 9:29am
I wish the following code is useful for you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

int main ()
{
	int year, month, day;
	string date;
	cout << "Please enter a date in the following format: YYYYMMDD" << endl;
	cin >> date;
	
	year = atof(date.substr(0, 4).data());
	month = atof(date.substr(4, 2).data());
	day = atof(date.substr(6, 2).data());

	cout << year << month << day << endl;
	
  
	return 0;
}
Sep 10, 2008 at 5:13pm
Faldrax's method looks like the winner. I've almost got it working correctly.

I've successfully pulled the digits from the year and day positions, but how do I pull the (MM) middle digits out? I've tried everything...
Last edited on Sep 10, 2008 at 5:47pm
Sep 10, 2008 at 7:22pm
This is what I have so far. Only thing I'm lacking is my Day to print out correct.

Tell me your thought please:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
using namespace std;

int main () {
    
    int month, date;
    int res1, res2;
    
    cout << "Please enter a date in the following format: YYYYDDMM" << endl;
    cin >> date; //Example: 20080105
    
    res1= date / 10000; //Posts the Year
    res2= res1 - date;  //<------NOT CORRECT// Posts the day
    month= date % 10; //Posts the month
    
    // For my testing // Not for actual Program
    cout << res1 << endl; //YEAR 
    cout << res2 << endl; //Day
    cout << month << endl; //Month
    
    cout << "**************************************" << endl;
    cout << date << " is the date:" << endl;
   
    if ( month >= 1 && month <= 12 )
       if (month == 1)
            cout << "January";
       else if (month == 2)
            cout << "February";
       else if (month == 3 )
            cout << "March";
       else if (month == 4 )
            cout << "April";
       else if (month == 5 )
            cout << "May";
       else if (month == 6)
            cout << "June";
       else if (month == 7)
            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
        cout << "Invaid Format" << endl;
    cout << res2 << ", " << res1 << endl;
    
    system("PAUSE");
    return 0;
    
}
Sep 10, 2008 at 7:46pm
If you can pull out the day and year then do a modulo so that you get the DDMM part (%10000 I believe) and subtract the MM that you already pulled out. That will leave you with DD00, then just divide that number by 100 and bam, you have DD.

btw- your month is wrong right now. You need to mod by 100, not 10. Otherwise your remainder is the last digit, not the last two. If you're not sure what I mean, try using November or December (11 or 12) in your program.

edit: getting the Month and Day in the right order is confusing me, lol.

edit2: btw, it's a lil sloppy to just divide by 10000 and consider that the equivalent of the year. All you've really done is hide behind the fact that there's no decimal places for an int. Well...I say sloppy...but really it's more just from a pure math standpoint.
Last edited on Sep 10, 2008 at 7:57pm
Sep 10, 2008 at 7:57pm
Also, not programming related, The example date you stated in your comment, May 1, is my B-day. Creepy.
Sep 10, 2008 at 7:59pm
You were born in 2008? Holy cow, QWERTYman is the baby who was born speaking code. The legends have long spoken of the child who can run programs without compiling and whose stack is limitless. I never thought I'd live to see it!
Sep 10, 2008 at 8:35pm
Great catch on the Mod by 100 instead of 10!

As for pulling the day. I still can't figure it out.

This is what I put. But it outputs about 15 digits..

1
2
3
res1= date / 10000; //Posts the Year
    res2= ((date % 10000) - month) / 100; //Post the Day
    month= date % 100; //Posts the month 
Sep 10, 2008 at 8:39pm
You have to put the month calculation before the res2 calculation.
Sep 10, 2008 at 8:44pm
Oh and one more thing, you need brackets after your if ( month >= 1 && month <= 12 )
to contain all your month statements, otherwise your else for the invalid input will never trigger.

edit: and make sure you put your cout for the day and month INSIDE those brackets, or it will print even when the invalid input cout triggers.
Last edited on Sep 10, 2008 at 8:47pm
Sep 10, 2008 at 9:07pm
1
2
3
4
5
d=x%100;
x/=100;
m=x%100;
x/=100;
y=x;

There. Was that so hard?
Sep 10, 2008 at 9:13pm
lol, helios needs a nap...or a cookie.
Sep 11, 2008 at 1:02am
Everything seems to be working. But jpeg mentioned the date format off. If I put the format as YYYYMMDD I can't get it to print the MM in my if's it only recognizes the last digits? How can I change this by my code?

Seems I have something backwards.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
using namespace std;

int main () {
    
    int month, date;
    int res1, res2;
    
    cout << "Please enter a date in the following format: YYYYDDMM" <<endl;
    cin >> date; //Example: 20080105
    
    res1= date / 10000; //Posts the Year
    month= date % 100; //Posts the month
    res2= ((date % 10000) - month) / 100; //Posts the Day
    
    
    
    
    cout << "**************************************" << endl;
    cout << date << " is the date:" << endl;
   
    if ( month >= 1 && month <= 12 ){
       if (month == 1)
            cout << "January";
       else if (month == 2)
            cout << "February";
       else if (month == 3 )
            cout << "March";
       else if (month == 4 )
            cout << "April";
       else if (month == 5 )
            cout << "May";
       else if (month == 6)
            cout << "June";
       else if (month == 7)
            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
        cout << "Invaid Format" << endl;
    
    cout << res2 << ", " << res1 << endl;
    
    system("PAUSE");
    return 0;
    
}
Sep 11, 2008 at 1:25am
The -month on line 14 is unnecessary.
Other than that the code seems fine (except for the fact that you're using a completely retarded date format).

If I put the format as YYYYMMDD I can't get it to print the MM in my if's it only recognizes the last digits?

You do know you're doing the processing as YYYYDDMM, right?
Sep 11, 2008 at 2:28am
Yes. I would like to process as YYYYMMDD. Would this be hard to change?
Sep 11, 2008 at 2:38am
*Teeth are ground into a fine powder*

1
2
3
4
5
d=x%100;
x/=100;
m=x%100;
x/=100;
y=x;
Pages: 12