Functions

closed account (2UX23TCk)
....
Last edited on
closed account (2UX23TCk)
...
Last edited on
I'm thinking to do it as a character array and then do a loop to go through it,


Rather than a char array, use a std::string which is like a char array but much better. Read it in with std::getline. You can still loop thorough it. Can also use std::stringstream because you know the format.

Good Luck !!
closed account (2UX23TCk)
I did something similar to that, but what I'm stuck on and don't know how to do is how am I supposed to split the month, day, and year?
closed account (2UX23TCk)
...
Last edited on
1
2
3
4
5
6
7
8
int day ;
int month ;
int year ;
char s1, s2 ;

std::cout << "enter date as month-day-year (eg. 1-1-2006): " ;
std::cin >> month >> s1 >> day >> s2 >> year ;
// validate: day, month and year hold valid values; s1 and s2 are the character '-' 
closed account (2UX23TCk)
So this question does not have to necessarily be done with functions?
Hi,

Please always use code tags: http://www.cplusplus.com/articles/z13hAqkS/

Are you allowed to use std::string? It's easier if you can, even easier if allowed to use std::stringstream. These containers have built-in functions like substr.

Some pseudo-code:

//Find position of all the "-"
// make sub strings using those positions
// convert sub strings to numbers, possibly with atoi function

Using std::stringstream :

// read in unsigned int, "-" , unsigned int, "-", unsigned int, into month, day, year variables.
> So this question does not have to necessarily be done with functions?

Consider writing functions to
a. validate that the tuple day, month, year represent a valid calendar date.
b. convert the validated tuple day, month, year to number of days elapsed since 1-1-2006.

To just read in three integers separated by '-', doing anything more than
1
2
std::cin >> month >> s1 >> day >> s2 >> year ;
// validate: day, month and year hold valid values; s1 and s2 are the character '-' 

is downright silly. Write simple, elegant code.
Topic archived. No new replies allowed.