Improper outputs/stringstream help

Feb 2, 2017 at 11:11pm
I am so close to finishing my program for school but I've hit a problem that I really have no idea what's going on. The function determineDayOfBirth is supposed to tell you on which day of the week you were born if you put in your birthday as month / day / year. And it does basically do this, but there are 2 problems:

1. When it outputs what is supposed to be the day of the week, it gives me a set of characters that appear to be different each time, even with the same answer, and

2. After it does that the function will print multiples of "Invalid date". I imagine this has something to do with how I set up the stringstream, which I've never done before.

Sorry for posting a lot, but my univ. c++ class is pretty fast paced and this is the only place I can get decent help. (I can't make it to office hours and my teachers aren't allowed to help with code specifics.) So I really appreciate all the instruction and help. I'm basically learning from you guys and the book at this point.

These are the 3 relevant functions. Let me know if you need more info.

EDIT: int dayDetermined is a global variable.

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
bool isValidDate(int month, int day, int year)
{
    int days_in_month[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };

    if (year % 4 == 0) {
        days_in_month[2] = 29;
    }
        
    if ((month < 1) || (month > 12)) {
        return false;
    }
        
    if ((day < 1) || (day > days_in_month[month])) {
        return false;
    }

    return true;
}

void printDayOfBirth(int dayDetermined) {

    if (dayDetermined = 0) {
        cout << "Saturday";
    }
    else if (dayDetermined = 1) {
        cout << "Sunday";
    }
    else if (dayDetermined = 2) {
        cout << "Monday";
    }
    else if (dayDetermined = 3) {
        cout << "Tuesday";
    }
    else if (dayDetermined = 4) {
        cout << "Wednesday";
    }
    else if (dayDetermined = 5) {
        cout << "Thursday";
    }
    else if (dayDetermined = 6) {
        cout << "Friday";
    }
    return;
}

void determineDayOfBirth() { 

    int month = 0;
    int day = 0;
    int year = 0;
    char x = '/';
    stringstream birthday;
    birthday << month << x << day << x << year;

    cout << "Enter your date of birth" << endl;
    cout << "format: month / day / year  --> ";
    cin >> birthday.str();

    isValidDate(month, day, year);

    if (isValidDate == false) {
        cout << "Invalid date" << endl;
    }
    else {
        determineDay(month, day, year);
        cout << "You were born on a: " << printDayOfBirth << endl;
        cout << "Have a great birthday!!!" << endl;
    }
    
    return;
}
Last edited on Feb 3, 2017 at 12:47am
Feb 2, 2017 at 11:20pm
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/207461/#msg979336
http://www.cplusplus.com/forum/beginner/207463/
Last edited on Feb 2, 2017 at 11:23pm
Feb 3, 2017 at 2:16am
I feel bad because I should be able to figure this out on my own, but for some reason I'm still having trouble with these 2 problems.
Feb 3, 2017 at 2:29am
= is assignment. == is comparison.
Feb 3, 2017 at 3:14am
Taking a closer look at your code...

In the conditions in printDayOfBirth (which might be more aptly named printDayOfWeek you use = which is assignment and not == which is for comparison as previously noted.

In addition, line 57 doesn't do anything sensible. The str method of std::stringstream returns a string object by value, so this expression causes input to be extracted into a temporary which is immediately destroyed. I wouldn't have expected it to compile, but perhaps you have a non-standard extension enabled that allows binding of references to non-const objects like VC++ does by default.

There is no compelling reason to have a std::stringstream involved here. It doesn't do what you seem to think it does.

Also, on line 59 you invoke the function isValidDate but discard the return value (by completely disregarding it.) On line 61 you compare the address of the function to false which is a rather silly thing to do.
Topic archived. No new replies allowed.