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?

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:

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

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:

#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 << "\n" << month << "\n" << day << "\n" << year;
    system("pause");
    
    return 0;
}
void getData(int& month, int& day, int& year)
{
    
    cout << "Enter your birthday in the form of: mm/dd/yyyy";
    scanf("%s %s %s", month, day, year);

    return;
}

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 :)

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

This article has been dead for over six months. Start a new discussion instead.