![]() |
|
#1
|
|||
|
|||
'%' illegal, left operand, has type 'double'We are supposed to write a program which asks for the month, date, and yr and then outputs the day of that date.
input: Month: 7 // This is July Day: 4 Year: 1776 output: July 4, 1776 was on a Thursday Code:
this is my code CPP / C++ / C Code:
//when i compile it, i get the error "'%' illegal, left operand, has type 'double"" Last edited by dsmith : 26-Oct-2004 at 07:47.
Reason: Please use [c] & [/c] for syntax highlighting
|
|
#2
|
||||
|
||||
|
The mod operator only works for type int. If you have any questions on that, just re-read my previous statement several times until it's etched into your brain!
__________________
-Aaron |
|
#3
|
||||
|
||||
|
Quote:
this is my code CPP / C++ / C Code:
Quote:
When I compile it (gcc 3.3.3 under cygwin) I get the following warnings now that I am casting like a new flyfisherman: > Executing: C:\Program Files\ConTEXT\ConExec.exe "C:\cygwin\bin\g++.exe" "DateToDay.cpp" -o DateToDay DateToDay.cpp: In function `char Date(int, int, int, int)': DateToDay.cpp:37: warning: converting to `int' from `float' DateToDay.cpp:58: warning: converting to `char' from `float' > Execution finished. When I run the program I don't seem to get the correct answer. For example entering 10, 26, 2004 tells me it is Sunday. I better get ready for some football. But I think you can see what aaroncohn was talking about. This does not fix the problem just shows where the problem lies. All I did was create an int var to cast the float to after returning from ceil(); I guess you just need to make sure you are using the correct variable type as I don't think I am doing it in a very crisp manner. Just 'cause it works don't make it right. Hope it is helpful. Mark Last edited by cable_guy_67 : 26-Oct-2004 at 07:58.
Reason: typos
|
|
#4
|
|||
|
|||
|
Quote:
Here are a few observations that point out some problems with your program. Getting the wrong answer is due to the following: 1. Wrong Math function: The function [x] means the largest integer less than or equal to x. In <math.h> this is floor(x) not ceil(x). 2. Applied function incorrectly Using floor(a+b+c+d+e) is not the same as a + floor(b) + c + d + e. 3. Not understanding how the heck the formula works (maybe the instructor didn't explain it): For months, March = 1, April = 2, ... December = 10. Note that January = 11 of the previous year, February = 12 of the previous year. (That is for February 1, 2004, you call date with yr1 = 20, yr2 = 03, day = 1, month = 2). 4. Not knowing the results of the modulus operator: days will be 0 to 6, not 1 to 7. Sunday is 0, Monday is 1, ... Saturday is 6; 5. Not knowing that the formula can give negative numbers, and the modulo operator can give negative numbers. If i = -23, for example, i % 7 = -2. If the result of the modulo operator is less than zero, you must add 7 to get a number from 0 to 6, which you need for your switch statement. Here's a "big picture" observation: It is completely unnecessary to use any floating point arithmetic. For example if i, j, and k are ints, then the following automatically sets k equal to "the largest integer whose value is less than or equal to the quantity (i/j)": Code:
Then the formula becomes (all in ints) CPP / C++ / C Code:
Of course, your instructor may have wanted for you to use floating arithmetic to see the results of various conversions. You must always complete the assignment to his satisfaction, not mine. Regards, Dave |
|
#5
|
|||
|
|||
|
Quote:
Typographical error: Correction required in my discussion since February is month 12 in the formula. change this Quote:
to this Quote:
also the code snippet: CPP / C++ / C Code:
should be something like CPP / C++ / C Code:
Sorry 'bout that, Dave |
|
#6
|
||||
|
||||
|
I am thinking there is something to the odd way the formula marks the month. It seems like something the program should be dealing with:
EXAMPLE: CPP / C++ / C Code:
At this point the user will have most likely entered the number of the month as it is on the calander. 1=January ... 12=December To get the correct value for the formula (where march is 1): CPP / C++ / C Code:
In the very least it is a way for me to make sure I understand how the modulus operator can be useful. I do think this adresses Dave's post on what numbers actually have to be entered into the formula to work. Mark |
|
#7
|
|||
|
|||
|
Quote:
When you have something like y = x % 12 (where x is non-negative), the value of y will be such that 0 is less than or equal to y and y is less than or equal to 11 (not 1 to 12). (Your formula gives the wrong value for February.) The modulo operator is a Good Thing in certain cases. Instead of forcing it to do something for a particular formula, sometimes you can just use "brute force" instead. Compare the output from your use of the modulus operator in the following with my simple if()else(). How would you adjust the modulus formula to give the right answer for the month? It can be done easily enough, but I don't think the results would be as obvious to a casual observer as the if()else() stuff. (Note that in my (i < 3) case I would also decrement the year for the calendar program.) Try this. CPP / C++ / C Code:
Regards, Dave |
|
#8
|
||||
|
||||
|
Quote:
Duhhhhh, ( 2 + 10 ) % 12 = 0 !12 Quote:
CPP / C++ / C Code:
Thank you for pointing that out Dave. I missed that entirely. Part of the problem was that I was trying to solve it as a math problem and didn't recompile the example. I will go back and work on the origonal source posted and see the changes in effect. Maybe see what my solution to the professors problem would be. I am only as far along as creating classes in the book I am learning from so this seems within (current knowlege) reach. I am curious to see how you put this all together quasimof. Mark OneProblem :: NumerousSolutions <- guy just makes me want to go to 6 Flags! |
|
#9
|
||||
|
||||
Ok, sorry for jumping the gun on that one. Now that I have tried to duplicate your example I see the output. By changing to 13 I move all the months from april on one number higher than they should be. I am going back to just using % 12 to solve most of the problem and another method for Jan and Feb. Will post when I have it solved. Thanks again Dave, better than a tv show anytime. Mark And here is my solution in C++ CPP / C++ / C Code:
Last edited by cable_guy_67 : 26-Oct-2004 at 20:30.
Reason: Added code segment
|
|
#10
|
|||
|
|||
Re: '%' illegal, left operand, has type 'double'CPP / C++ / C Code:
Last edited by cable_guy_67 : 27-Jul-2006 at 22:39.
Reason: Please surround your C++ code with [cpp] ... [/cpp]
|
Recent GIDBlog
Toyota - 2008 November Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| "undefined symbol" error | crystalattice | C++ Forum | 3 | 27-Sep-2004 08:32 |
| some I/O problems...again | cameron | C++ Forum | 3 | 03-Mar-2004 22:39 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The