| New Reply |
C++: what did I do wrong? |
Share Thread | Thread Tools |
| Jun10-12, 07:59 PM | #1 |
|
|
C++: what did I do wrong?
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++;
}
}
|
| PhysOrg.com |
science news on PhysOrg.com >> Hong Kong launches first electric taxis >> Morocco to harness the wind in energy hunt >> Galaxy's Ring of Fire |
| Jun10-12, 08:42 PM | #2 |
|
Mentor
|
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?
|
| Jun10-12, 09:00 PM | #3 |
|
|
No, it doesn't compile, the error message is put in as a comment in the code.
|
| Jun10-12, 09:36 PM | #4 |
|
Mentor
|
C++: what did I do wrong?
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. |
| Jun10-12, 09:54 PM | #5 |
|
|
Huh.
Code:
for (int day = 1,dayofweek = mon;day<=number;day++)
{
if (dayofweek == sun)
{
suns++;
}
plusone7(dayofweek);
}
|
| Jun10-12, 10:01 PM | #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.
|
| Jun10-12, 10:07 PM | #7 |
|
|
I did figure out what was wrong, though: http://www.cplusplus.com/forum/beginner/72969/ |
| Jun10-12, 10:37 PM | #8 |
|
Mentor
|
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. |
| Jun11-12, 01:59 AM | #9 |
|
Admin
|
Wow, a nice one. I think it has quality of a good homework or test problem for the C++ class.
|
| Jun14-12, 03:34 PM | #10 |
|
|
@Borek: It's for a problem from a very well-known site.
|
| New Reply |
| Thread Tools | |
Similar Threads for: C++: what did I do wrong?
|
||||
| Thread | Forum | Replies | ||
| Differentiation quotient rule question, am i wrong or is my book wrong? | Calculus & Beyond Homework | 1 | ||
| Capacitance... do I have the totally wrong values/equation or am I converting wrong?! | Introductory Physics Homework | 0 | ||
| Need help about math is my calculation wrong the answer from the text book is wrong | Calculus & Beyond Homework | 2 | ||
| Wrong war, wrong place, wrong time, and Bush's Desert Shield Jr. | Current Events | 6 | ||