GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 26-Oct-2004, 01:14
quasimof quasimof is offline
New Member
 
Join Date: Oct 2004
Posts: 4
quasimof is on a distinguished road

'%' 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:
day = (r + [2.6m - .2] - 2c + d + [c/4.0] +[d/4.0] ) % 7 [x] denotes the greatest integer less than or equal to x (look in cmath). c = first two digits of the year. (If the year is 1898, c = 18). d = the last two digits of the year. (If the year is 1998, d = 98). r = the day of the month (If the date is May 23, 1998, r = 23). m = the month. March is considered month 1, April is 2, May is 3...February is month 12.

this is my code
CPP / C++ / C Code:
:
==========================================================
#include<iostream>
#include<cmath>
using namespace std;

char Date(int c, int d, int r, int m);

int main()
{
	
	int month, day, yr, yr1, yr2;
	
	cout<<"month:"<<endl;
	cin>>month;

	cout<<"day:"<<endl;
	cin>>day;

	cout<<"year:"<<endl;
	cin>>yr;

	yr1=yr/100;
	yr2=yr%100;

	cout<<month<<" "<<day<<" "<<yr<<" "<<Date(yr1, yr2, day, month);
}

	char Date(int c, int d, int r, int m)
	{

		char result;
		result=ceil(r+(2.6*m-.2)-2*c+d+(c/4.0)+(d/4.0))%7;

		switch(result)
		{
			case 1: cout<<" was on a sun";
		break;
			case 2: cout<<" was on a mon";
				break;
			case 3: cout<<" was on a tues";
				break;
			case 4: cout<<" was on a wed";
				break;
			case 5: cout<<" was on a thurs";
				break;
			case 6: cout<<" was on a fri";
				break;
			case 7: cout<<" was on a sat";
				break;
			default: cout<<"invalid";

				return result;
		}
	}

//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  
Old 26-Oct-2004, 03:25
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
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  
Old 26-Oct-2004, 07:49
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
Quote:
Originally Posted by quasimof
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

day = (r + [2.6m - .2] - 2c + d + [c/4.0] +[d/4.0] ) % 7

[x] denotes the greatest integer less than or equal to x (look in cmath).
c = first two digits of the year. (If the year is 1898, c = 1.
d = the last two digits of the year. (If the year is 1998, d = 9.
r = the day of the month (If the date is May 23, 1998, r = 23).
m = the month. March is considered month 1, April is 2, May is 3...February is month 12.

this is my code
CPP / C++ / C Code:
:

#include<iostream>
#include<cmath>
using namespace std;

char Date(int c, int d, int r, int m);

int main()
{

    int month, day, yr, yr1, yr2;

    cout<<"month:"<<endl;
    cin>>month;

    cout<<"day:"<<endl;
    cin>>day;

    cout<<"year:"<<endl;
    cin>>yr;

    yr1=yr/100;
    yr2=yr%100;

    cout<<month<<" "<<day<<" "<<yr<<" "<<Date(yr1, yr2, day, month);
    cin.ignore(100,'\n');    // clear the input stream 
    cin.get();                 // Stay open when launched from ConTEXT
    return 0;
}

char Date(int c, int d, int r, int m)
{

    float result;
    int result_int;

    result=ceil(r+(2.6*m-.2)-2*c+d+(c/4.0)+(d/4.0));
    result_int = result;
    result_int = result_int % 7;

    switch(result_int)
    {
        case 1:
            cout<<" was on a sun";
        break;
    
        case 2:
            cout<<" was on a mon";
        break;
    
        case 3:
            cout<<" was on a tues";
        break;
    
        case 4:
            cout<<" was on a wed";
        break;
    
        case 5:
            cout<<" was on a thurs";
        break;
    
        case 6:
            cout<<" was on a fri";
        break;
    
        case 7:
            cout<<" was on a sat";
        break;
    
        default:
            cout<<"invalid";
        break;
   }
return result;
}
Quote:
Originally Posted by quasimof

//when i compile it, i get the error "'%' illegal, left operand, has type 'double""

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. If I count from march than oct is month 8 and I get the correct day.

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  
Old 26-Oct-2004, 11:29
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by cable_guy_67

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. If I count from march than oct is month 8 and I get the correct day.

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
You are correct in believing that you can eliminate all compiler errors and warnings and still get the wrong answer.

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:
k = i / j;

Then the formula becomes (all in ints)
CPP / C++ / C Code:
    result = (r + (13 * m - 1) / 5 - 2 * c + d + (c / 4) + (d /4 )) % 7;

    if (resultx < 0) {
      resultx += 7;
    }

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  
Old 26-Oct-2004, 14:58
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by davekw7x
You are correct in believing that you can eliminate all compiler errors and warnings and still get the wrong answer.

Here are a few observations that point out some problems with your program. Getting the wrong answer is due to the following:

.... Some stuff here

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).

.... Some more stuff here

Dave

Typographical error: Correction required in my discussion since February is month 12 in the formula.
change this
Quote:
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).

to this
Quote:
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 = 12).

also the code snippet:

CPP / C++ / C Code:
result = (r + (13 * m - 1) / 5 - 2 * c + d + (c / 4) + (d /4 )) % 7;

    if (resultx < 0) {
      resultx += 7;
    }

should be something like

CPP / C++ / C Code:
resultx = (r + (13 * m - 1) / 5 - 2 * c + d + (c / 4) + (d /4 )) % 7;

    if (resultx < 0) {
      resultx += 7;
    }



Sorry 'bout that,

Dave
  #6  
Old 26-Oct-2004, 17:03
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
Lightbulb

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:
int main()
{

    int month, day, yr, yr1, yr2;

    cout<<"month:"<<endl;
    cin>>month;

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:
int main() {
  
    int month, day, yr, yr1, yr2;
  
    cout<<"month:"<<endl;
    cin>>month;

    cout<<"day:"<<endl;
    cin>>day;

    cout<<"year:"<<endl;
    cin>>yr;

    month = (month +10) % 12  //Set month to the function requirements

    if( month > 10 ){
        yr--;                 // If jan or feb subtract 1 from yr
    }

    yr1=yr/100;
    yr2=yr%100;

    cout<<month<<" "<<day<<" "<<yr<<" "<<Date(yr1, yr2, day, month);


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  
Old 26-Oct-2004, 17:33
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by cable_guy_67
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:



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

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:
#include <stdio.h>
int main()
{
  int i; 
  int month;
  for (i = 1; i <= 12; i++) {
    month = (i +10) % 12;
    printf("i = %2d, (i + 10) %% 12 = %2d   ", i, month);
    if (i < 3) {
      month = i + 10;
    }
    else {
      month = i - 2;
    }
    printf("i = %2d, month = %2d\n", i, month);
  }
  return 0;
}

Regards,

Dave
  #8  
Old 26-Oct-2004, 18:59
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
Quote:
Originally Posted by davekw7x
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.)

Duhhhhh, ( 2 + 10 ) % 12 = 0 !12


Quote:
Originally Posted by davekw7x
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?

CPP / C++ / C Code:
// Mark's Solution to Dave's question
// old was month = ( month +10 ) % 12
// that will set the month to 0 instead of 12

month = (month +10) % 13  //Set month to the function requirements

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  
Old 26-Oct-2004, 20:07
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough


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:
// DaveQuestion.cpp
// Mark's C++ solution to Dave's C example

#include <iostream>

int main(){

    using namespace std;

    int formulaMonth;
    int enteredYear = 2004;   // For testing
    int adjustedYear;

    for ( int enteredMonth = 1 ; enteredMonth <= 12 ; enteredMonth++ ){
        formulaMonth = ( enteredMonth +10 ) % 12;
        if ( formulaMonth == 0 ){
            formulaMonth = 12;
        }
        if ( formulaMonth > 10 ){
            adjustedYear = enteredYear - 1;
        }else adjustedYear = enteredYear;
        cout << "User Enters Month     : " << enteredMonth << endl;
        cout << "Passed to the Formula : " << formulaMonth << endl;
        cout << "User Enters Year      : " << enteredYear << endl;
        cout << "Passed to the Formula : " << adjustedYear << endl << endl;
    }
    return 0;
}
Last edited by cable_guy_67 : 26-Oct-2004 at 20:30. Reason: Added code segment
  #10  
Old 27-Jul-2006, 14:28
parug1987 parug1987 is offline
New Member
 
Join Date: Jul 2006
Posts: 1
parug1987 is on a distinguished road

Re: '%' illegal, left operand, has type 'double'


CPP / C++ / C Code:
#include<iostream.h>
#include<conio.h>
class calender
{int yr,mth,date,leap_status,yr2,mth2,date2;
 public:
 calender()
 {leap_status=0;}
 void getdata();
 void display();
 void calc();
 int leap(int);
};
void calender::getdata()
{int flag=1;

 cout<<"\nThis Calender finds day corresponding to the date between 1901 and 2100";
 cout<<"\nEnter year between 1901 and 2100: ";
	do{cin>>yr;
	if(yr<1901 || yr>2100)
	cout<<"\nInvalid year, please enter again: ";
	}while(yr<1901 || yr>2100);
leap_status=leap(yr);
	cout<<"\nEnter month: ";
	do{cin>>mth;
	if(mth<0 || mth>12)
	cout<<"\nInvalid month, please enter again: ";
	}while(mth<0 || mth>12);
cout<<"\nEnter date: ";
do{ flag=1;
    cin>>date;
    switch(mth)
    {case 1: case 3: case 5: case 7: case 8: case 10: case 12:
     if(date<1 || date>31)
     {cout<<"\nInvalid date, please enter again: ";
     flag=0;}
     break;
    }
    switch(mth)
    {case 4: case 6: case 9: case 11:
     if(date<1 || date>30 )
     {cout<<"\nInvalid date, please enter again: ";
     flag=0;} break;
    }
    if(mth==2 && leap_status==1)
       if(date<1 || date>29)
       {cout<<"\nInvalid date, please enter again: ";flag=0;}
    if(mth==2 && leap_status==0)
       if(date<1 || date>28 )
       {cout<<"\nInvalid date, please enter again: ";flag=0;}
}while(flag==0 );
yr2=yr; mth2=mth; date2=date;
}
void calender::calc()
{yr=yr-1900;
	while(yr>28 )
	   {if((yr-28 )>0)  yr-=28;}
switch(date)  //switching from date
{ case 1: case 8: case 15: case 22: case 29:
  date=0; break;
  case 2: case 9: case 16: case 23: case 30:
  date=1; break;
  case 3: case 10: case 17: case 24: case 31:
  date=2; break;
  case 4: case 11: case 18: case 25:
  date=3; break;
  case 5: case 12: case 19: case 26:
  date=4; break;
  case 6: case 13: case 20: case 27:
  date=5; break;
  case 7: case 14: case 21: case 28:
  date=6; break;
}
switch(mth) //switching from month
{ case 1: case 10:
  mth=0; break;
  case 5:
  mth=1; break;
  case 8:
  mth=2; break;
  case 2: case 3: case 11:
  mth=3; break;
  case 6:
  mth=4; break;
  case 9: case 12:
  mth=5; break;
  case 4: case 7:
  mth=6; break;
}
switch(yr)  //switching from year
{ case 1: case 7: case 18: case 24:
  yr=0; break;
  case 2: case 8: case 13: case 19:
  yr=1; break;
  case 3: case 14: case 20: case 25:
  yr=2; break;
  case 4: case 9: case 15: case 26:
  yr=3; break;
  case 10: case 16: case 21: case 27:
  yr=4; break;
  case 5: case 11: case 22: case 28:
  yr=5; break;
  case 6: case 12: case 17: case 23:
  yr=6; break;
}
}//end of calc
int calender::leap(int a)
{
 if(((a%100==0)&&(a%400==0))||((a%100!=0)&&(a%4==0)))
 return 1;
 else
 return 0;
}
void calender::display()
{int total=yr+mth+date+leap_status;
 while(total>6)
 {if((total-7)>=0)
 total-=7;}
 cout<<"\nDate you entered:";
 switch(mth2)
 {case 1:cout<<" January "; break;
  case 2:cout<<" February "; break;
  case 3:cout<<" March "; break;
  case 4:cout<<" April "; break;
  case 5:cout<<" May "; break;
  case 6:cout<<" June "; break;
  case 7:cout<<" July "; break;
  case 8:cout<<" August "; break;
  case 9:cout<<" September "; break;
  case 10:cout<<" October "; break;
  case 11:cout<<" November "; break;
  case 12:cout<<" December "; break;
 }
 cout<<date2<<", "<<yr2;
 switch(total)
 {case 0:cout<<"\nDay: \t\t  Tuesday"; break;
  case 1:cout<<"\nDay: \t\t  Wednesday"; break;
  case 2:cout<<"\nDay: \t\t  Thursday"; break;
  case 3:cout<<"\nDay: \t\t  Friday"; break;
  case 4:cout<<"\nDay: \t\t  Saturday"; break;
  case 5:cout<<"\nDay: \t\t  Sunday"; break;
  case 6:cout<<"\nDay: \t\t  Monday"; break;
 }
}

void main()
{char ch;
 do{clrscr();
 calender c1;
 c1.getdata();
 c1.calc();
 c1.display();
 cout<<"\n\nDo you want to continue? (y/n):  ";
 cin>>ch;
 }while(ch=='y'||ch=='Y');
 cout<<"\nHit any key to exit...";
 getch();

}
Last edited by cable_guy_67 : 27-Jul-2006 at 22:39. Reason: Please surround your C++ code with [cpp] ... [/cpp]
 
 

Recent GIDBlogToyota - 2008 November Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 05:41.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.