Help with dates

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2004
Posts: 16
Reputation: phr0stbyt3 is an unknown quantity at this point 
Solved Threads: 1
phr0stbyt3 phr0stbyt3 is offline Offline
Newbie Poster

Help with dates

 
0
  #1
Nov 1st, 2004
Im writing a program that when given a date in the form of mm/dd/yyyy will calculate how old the person is in years... right now im stuck at getting the data as i cant figure out how to separate the data when it's entered in mm/dd/yyyy format... any suggestions?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 185
Reputation: Stack Overflow is an unknown quantity at this point 
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: Help with dates

 
0
  #2
Nov 1st, 2004
Greetings phr0stbyt3,

Getting the data is not difficult at all. We can either use scanf() or fgets().

scanf()
int scanf(const char *format [, argument, ...]);
> Reads data from the standard input (stdin) and stores it into the locations given by argument(s). Locations pointed by each argument are filled with their corresponding type of value requested in the format string.

In this case, we could make a simple program as the following:
#include <stdio.h>

int main() {
	int a;

	printf("Enter a number: ");
	scanf("%d", &a);
	printf("You entered %d\n", a);

	return 0;
}
The way this works is simple. We call scanf() to get any input from the input stream. Once we get the stream we use scanf() to pull out what we want. For instance if we had:
  1. scanf("%s %s %s", s1, s2, s3);
That means if we entered in "Hello there world" s1 would be Hello, s2 would be there and s3 would be world.

* scanf() note: These arguments must be pointers: if you want to store the result of a scanf operation on a standard variable you should precede it with the reference operator, i.e. an ampersand sign (&). It is not neccessary to add the (&) symbol when working with character arrays since they respond similiarly to pointers.

fgets()
char *fgets (char *string, int num, FILE *stream);
> Reads characters from stream and stores them in string until (num -1) characters have been read or a newline or EOF character is reached, whichever comes first.

fgets() is used mostly when receiving a buffer from the stream. Because so far we don't know what is coming, and scanf() could overflow. How? scanf() has no limitation of how much we try to read, and forcing the function to read could cause issues. Though in fact scanf() returns the number of items succesfully read. So in that case we could do:
if (scanf("%s %d", s1, &i) == 2) {
	// ...
}
We could get away with using scanf(). It's not recommended to use often though there are the exceptions. Back on topic, fgets() is safter to use because we get to set how much of our string the stream sends to. For instance, if we used scanf() and had a input size of 100 and our local variable is only a size of 50 there could be a problem. fgets() relieves us of this. In this case we could do:
#include <stdio.h>

int main() {
	char str[25];

	fgets(str, 24, stdin);
	printf("Input stream: %s", str);

	return 0;
}
This saves us from overflow. As you may have seen, we used stdin as our FILE stream. That's fine, since the stream stdin is our input from the console window. Even if our input was 2000 bytes, fgets() only sends the first 24 bytes to our character array str.

On the other hand
Though, if you were to use scanf(), you could get away with it since you are dealing with integer values only. For example:
int month, day, year;

if (scanf("%d/%d/%d", &month, &day, &year) == 3) {
	// ...
}
This will make sure our scanf() is complete with three entries and so forth.

I hope this helps. If you have further questions, please feel free to ask.


- Stack Overflow
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.

IRC
Channel: irc.daniweb.com
Room: #c, #shell
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 16
Reputation: phr0stbyt3 is an unknown quantity at this point 
Solved Threads: 1
phr0stbyt3 phr0stbyt3 is offline Offline
Newbie Poster

Re: Help with dates

 
0
  #3
Nov 1st, 2004
aha makes sense now, thanks for the reply
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 16
Reputation: phr0stbyt3 is an unknown quantity at this point 
Solved Threads: 1
phr0stbyt3 phr0stbyt3 is offline Offline
Newbie Poster

Re: Help with dates

 
0
  #4
Nov 1st, 2004
ok i thought it made sense, but now that im trying to work with it in dev c++ its actin up... what i want to do is read 12/25/2004 etc and store 12, 25, and 2004 in main in int's... we havent really covered the stdio.h in my class yet. this is what i have so far:
  1. #include <iostream>
  2. #include <stdio.h>
  3. using namespace std;
  4.  
  5. void getData(int&, int&, int&);
  6.  
  7. int main()
  8. {
  9. int month;
  10. int day;
  11. int year;
  12.  
  13. getData(month, day, year);
  14. cout << "\n" << month << "\n" << day << "\n" << year;
  15. system("pause");
  16.  
  17. return 0;
  18. }
  19. void getData(int& month, int& day, int& year)
  20. {
  21.  
  22. cout << "Enter your birthday in the form of: mm/dd/yyyy";
  23. scanf("%s %s %s", month, day, year);
  24.  
  25. return;
  26. }

i dont think we can use streams or arrays. also, when i run this program it performs an illegal operation after i put in "12/12/2004" i need something a little less advanced
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 185
Reputation: Stack Overflow is an unknown quantity at this point 
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: Help with dates

 
0
  #5
Nov 1st, 2004
Alright,

Here are the changes:
#include <iostream>
#include <stdio.h>
using namespace std;

void getData(int *, int *, int *);

int main() {
	int month;
	int day;
	int year;

	getData(&month, &day, &year);
	cout << "Month: " << month << endl << "Day: " << day << endl << "Year: " << year << endl;
	system("pause");

	return 0;
}

void getData(int *month, int *day, int *year) {
	int m, d, y;
	cout << "Enter your birthday in the form of: mm/dd/yyyy" << endl;
	if (scanf("%d/%d/%d", &m, &d, &y) == 3) {
		*month = m;
		*day = d;
		*year = y;
	}

	return;
}
Change 1
We changed
void getData(int&, int&, int&);
To:
void getData(int *, int *, int *);
Why?
> Because we are passing the addresses variable to our function getData().

Change 2
getData(&month, &day, &year);
> This was changed because this is actually passing the variables memory address. Hence the reason we used the unary dereferencing operator '*' in our function declaration

Change 3
void getData(int *month, int *day, int *year) {
> We changed this because we changed our first declaration above.

Change 4
int m, d, y;
> We need some local variables if we intend to change the variables from within the function.

Change 5
if (scanf("%d/%d/%d", &m, &d, &y) == 3) {
> Just to ensure that we got all 3 variables. A previous problem was that you used %s instead of %d or %i. The %s goes for strings, %d for doubles and %i for integers.

Change 6
*month = m;
*day = d;
*year = y;
> Remember how we passed our address? Well now we need to send the new data to our variable. The only thing is our data can only be accessed if we use the '*' operator. We can't set the data to the address, we have to set the data to the data.


Hope this helps,
- Stack Overflow
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.

IRC
Channel: irc.daniweb.com
Room: #c, #shell
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 16
Reputation: phr0stbyt3 is an unknown quantity at this point 
Solved Threads: 1
phr0stbyt3 phr0stbyt3 is offline Offline
Newbie Poster

Re: Help with dates

 
0
  #6
Nov 1st, 2004
AHHHH much better, thanks a million
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC