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

  • C/C++
  • Thread starter Whovian
  • Start date
In summary: If you're not familiar with it, you probably won't understand my solution either.Yes, I can see how that might lead one astray.
  • #1
Whovian
652
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
  • #2
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?
 
  • #3
No, it doesn't compile, the error message is put in as a comment in the code.
 
  • #4
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.
 
  • #5
Huh.

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

doesn't help.
 
  • #6
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.
 
  • #7
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:
  • #8
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.
 
  • #9
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.
 

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

1. What are some common errors in C++ programming?

Some common errors in C++ programming include using uninitialized variables, forgetting to include necessary libraries, and syntax errors such as missing semicolons or parentheses.

2. Why is my code not compiling in C++?

There could be several reasons why your code is not compiling in C++. Some possible causes include syntax errors, missing or incorrect header files, and attempting to use undeclared variables or functions.

3. How do I debug my C++ code?

To debug your C++ code, you can use a debugger tool such as GDB or Visual Studio Debugger. You can also use print statements or a logging system to track the execution of your code and identify any errors.

4. How can I fix a segmentation fault in my C++ program?

A segmentation fault in a C++ program is often caused by accessing memory that is not allocated or trying to write to read-only memory. To fix this error, you can check your code for any memory access violations and make sure to properly allocate and deallocate memory.

5. What are some best practices for avoiding errors in C++ programming?

Some best practices for avoiding errors in C++ programming include using proper naming conventions, commenting your code, testing and debugging frequently, and following a consistent coding style. It is also important to thoroughly understand the language and its rules before writing code.

Similar threads

  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
30
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
6
Views
934
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
2
Views
3K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
3
Views
3K
Back
Top