What's Wrong with My C++ Code for Counting Sundays?

  • Context: C/C++ 
  • Thread starter Thread starter Whovian
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around a C++ coding issue related to counting Sundays within a specified range of days. Participants explore potential errors in the code, particularly focusing on function calls and the use of the comma operator in a loop structure.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant shares their C++ code and indicates a compiler error related to the function call for plusone7().
  • Another participant questions whether the issue arises from a compiler error message or unexpected output.
  • A participant suggests that the use of a void function with the comma operator may be causing the issue, proposing to move the function call within the loop body.
  • One participant attempts to modify the loop structure but finds that it does not resolve the issue.
  • Another participant recommends defining the function plusone7 earlier in the script, noting that some compilers may have specific requirements regarding function declarations.
  • A participant claims to have identified the problem and references an external link for further clarification.
  • One participant reflects on the lesson learned regarding the comma operator and its potential pitfalls in coding.
  • A later reply appreciates the complexity of the problem, suggesting it could serve as a good homework or test question.

Areas of Agreement / Disagreement

Participants express various hypotheses about the cause of the compilation error, but there is no consensus on the exact solution or the nature of the problem. Multiple competing views remain regarding the use of the comma operator and function placement.

Contextual Notes

Some participants note that the issue may depend on compiler behavior, and there are unresolved questions about the implications of using the comma operator in this context.

Whovian
Messages
651
Reaction score
3
Since I'm doing this for a problem of sorts on a site anyone can join, I won't release my whole code, but here's what I tried.

Code:
#include <iostream>

enum month {jan,feb,mar,apr,jun,jul,aug,sep,oct,nov,dec};
enum weekday {sun,mon,tue,wed,thur,fri,sat};

int year;
int numberofdays(month);
void plusone7(weekday&);

int main()
{
    int number = 0;
    int suns = 0;
    //Some code
    std::cout << number << " days total." << std::endl;
    weekday dayofweek;
    for (int day = 1,dayofweek = mon;day<=number;day++,plusone7(dayofweek)) //"No matching function for call to plusone7"
    {
        if (dayofweek == sun)
        {
            suns++;
        }
    }
    std::cout << suns << " is the number of sundays" << std::endl;
}

//Defines a few functions

void plusone7(weekday& added)
{
    if (added == sat)
    {
        added = sun;
    }
    else
    {
        added++;
    }
}

But the prototype and the bit at the end would make there a matching function, I thought?
 
Technology news on Phys.org
How do you know there's something wrong? Do you get a compiler error message? Does it compile, but not produce the output you expected?
 
No, it doesn't compile, the error message is put in as a comment in the code.
 
Aha, I missed the error message before because it was out of view to the right. I had to scroll the text-box over, in order to see it.

The prototype for plusone7() looks like it matches OK.

Here's a possibly wild guess: it might be that you can't use a void function with the comma operator. The expression "day++,plusone7(dayofweek)" wants to return the value of plusone7(dayofweek), so the compiler may be looking for a non-void version of plusone7().

Try moving plusone7(dayofweek) to the beginning of the loop body and see if that makes a difference.

Do a Google search for "comma operator in C++" and you'll find some explanations of how the comma operator works.
 
Huh.

Code:
    for (int day = 1,dayofweek = mon;day<=number;day++)
    {
        if (dayofweek == sun)
        {
            suns++;
        }
        plusone7(dayofweek);
    }

doesn't help.
 
Try defining the function plusone7 at the beginning of your script (immediately after calling int main{}, or at least before calling it during your loop). Some compilers are rather funny about these things.
 
Number Nine said:
Try defining the function plusone7 at the beginning of your script (immediately after calling int main{}, or at least before calling it during your loop). Some compilers are rather funny about these things.

Also tried that, unfortunately didn't help. :(

I did figure out what was wrong, though: http://www.cplusplus.com/forum/beginner/72969/
 
Last edited:
Lesson to be learned here: Beware the comma operator my son.

Especially beware thinking that a comma is the comma operator when it is in fact that comma is a separator.

The comma operator can be oh-so-nice in collapsing multiple lines into one, particularly with for loops. My advice is to avoid that temptation toward that kind of cleverness. Don't do it unless the alternative is even harder to understand.
 
Wow, a nice one. I think it has quality of a good homework or test problem for the C++ class.
 
  • #10
@Borek: It's for a problem from a very well-known site.
 

Similar threads

  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
14K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 30 ·
2
Replies
30
Views
5K