C++, Creating Math Funtions For This Weeks Program
Moderator: Moderators
-
- Inmate
- Posts: 128
- Joined: Sun Jan 27, 2002 4:17 pm
C++, Creating Math Funtions For This Weeks Program
Ok, even though I have not written the last two functions (multiplication and divition), I am trying to get the bugs worked out. I currently have seven problems trying to compile:<br><br>(43) : error C2653: 'NumDays' : is not a class or namespace name<br>(45) : error C2065: 'hours' : undeclared identifier<br>(47) : error C2065: 'days' : undeclared identifier<br>(59) : error C2653: 'NumDays' : is not a class or namespace name<br>(59) : error C2146: syntax error : missing ';' before identifier '+'<br>(59) : error C2501: 'NumDays' : missing storage-class or type specifiers<br>(59) : fatal error C1004: unexpected end of file found<br>Error executing cl.exe.<br><br><br>Here is the file I am creating:<br><br>#ifndef NUMDAYS_H<br>#define NUMDAYS_H<br><br>#include <iostream.h><br>#include <stdlib.h><br>#include <math.h><br>#include "numdays.h"<br><br>class NumDays<br>{<br>        private:<br>        double hours;<br>        double days;<br>        void calcDays ();<br><br>        public:<br>        //Constructors<br>        NumDays() {hours = 0; days = 0;}<br>        NumDays(double _hours) {hours = _hours; calcDays();}<br>        //get Function<br>        double getDays() {return days;}<br>        //Addition and Subtraction<br>        NumDays operator+(const NumDays &);<br>        NumDays operator-(const NumDays &);<br>        //Equal to and Not equal to<br>        int operator==(const NumDays &);<br>        int operator!=(const NumDays &);<br>        //Greater than and Less than<br>        int operator>(const NumDays &);<br>        int operator<(const NumDays &);<br>        //Greater than or equal to and Less than or equal to<br>        int operator>=(const NumDays &);<br>        int operator<=(const NumDays &);<br>        //Increments and Decrements<br>        NumDays operator++();<br>        NumDays operator++(double);<br>        NumDays operator--();<br>        NumDays operator--(double);<br>        //Times and Divide<br>        NumDays operator*(double);<br>        NumDays operator/(double);<br>};<br><br>#endif<br><br>/*****************************************************************/<br><br>void NumDays::calcDays()<br>{<br>        if (hours >= <!--EZCODE EMOTICON START 8) --><img src=http://www.ezboard.com/images/emoticons/glasses.gif ALT="8)"><!--EZCODE EMOTICON END--> <br>        {<br>                days += (hours / <!--EZCODE EMOTICON START 8) --><img src=http://www.ezboard.com/images/emoticons/glasses.gif ALT="8)"><!--EZCODE EMOTICON END--> ;<br>                hours = hours % 8;<br>        }<br>        else if (hours < 0)<br>        {<br>                days -= ((abs(hours) / <!--EZCODE EMOTICON START 8) --><img src=http://www.ezboard.com/images/emoticons/glasses.gif ALT="8)"><!--EZCODE EMOTICON END--> + 1);<br>                hours = 8 - (abs(hours) % <!--EZCODE EMOTICON START 8) --><img src=http://www.ezboard.com/images/emoticons/glasses.gif ALT="8)"><!--EZCODE EMOTICON END--> ;<br>        }<br>};<br><br>/*****************************************************************/<br><br>NumDays NumDays:<!--EZCODE EMOTICON START :o --><img src=http://www.ezboard.com/images/emoticons/embarassed.gif ALT=":o"><!--EZCODE EMOTICON END--> perator+(const NumDays &right)<br>{<br>        NumDays temp;<br><br>        temp.hours = hours + right.hours;<br>        temp.days = days + right.days;<br>        temp.calcDays();<br>        return temp;<br>};<br><br>/*****************************************************************/<br><br>NumDays NumDays:<!--EZCODE EMOTICON START :o --><img src=http://www.ezboard.com/images/emoticons/embarassed.gif ALT=":o"><!--EZCODE EMOTICON END--> perator-(const NumDays &right)<br>{<br>        NumDays temp;<br><br>        temp.hours = hours - right.hours;<br>        temp.days = days - right.days;<br>        temp.calcDays();<br>        return temp;<br>};<br><br>/*****************************************************************/<br><br>int NumDays:<!--EZCODE EMOTICON START :o --><img src=http://www.ezboard.com/images/emoticons/embarassed.gif ALT=":o"><!--EZCODE EMOTICON END--> perator==(const NumDays &right)<br>{<br>        if (days == right.days && hours == right.hours)<br>                return 1;<br>        else<br>                return 0;<br>};<br><br>/*****************************************************************/<br><br>int NumDays:<!--EZCODE EMOTICON START :o --><img src=http://www.ezboard.com/images/emoticons/embarassed.gif ALT=":o"><!--EZCODE EMOTICON END--> perator!=(const NumDays &right)<br>{<br>        if (days != right.days && hours != right.hours)<br>                return 1;<br>        else<br>                return 0;<br>};<br><br>/*****************************************************************/<br><br>int NumDays:<!--EZCODE EMOTICON START :o --><img src=http://www.ezboard.com/images/emoticons/embarassed.gif ALT=":o"><!--EZCODE EMOTICON END--> perator>(const NumDays &right)<br>{<br>        if (days > right.days)<br>                return 1;<br>        else if (days == right.days && hours > right.hours)<br>                return 1;<br>        else <br>                return 0;<br>};<br><br>/*****************************************************************/<br><br>int NumDays:<!--EZCODE EMOTICON START :o --><img src=http://www.ezboard.com/images/emoticons/embarassed.gif ALT=":o"><!--EZCODE EMOTICON END--> perator<(const NumDays &right)<br>{<br>        if (days < right.days)<br>                return 1;<br>        else if (days == right.days && hours < right.hours)<br>                return 1;<br>        else <br>                return 0;<br>};<br><br>/*****************************************************************/<br><br>int NumDays:<!--EZCODE EMOTICON START :o --><img src=http://www.ezboard.com/images/emoticons/embarassed.gif ALT=":o"><!--EZCODE EMOTICON END--> perator>=(const NumDays &right)<br>{<br>        if (days >= right.days)<br>                return 1;<br>        else if (days == right.days && hours > right.hours)<br>                return 1;<br>        else<br>                return 0;<br>};<br><br>/*****************************************************************/<br><br>int NumDays:<!--EZCODE EMOTICON START :o --><img src=http://www.ezboard.com/images/emoticons/embarassed.gif ALT=":o"><!--EZCODE EMOTICON END--> perator<=(const NumDays &right)<br>{<br>        if (days <= right.days)<br>                return 1;<br>        else if (days == right.days && hours < right.hours)<br>                return 1;<br>        else<br>                return 0;<br>};<br><br>/*****************************************************************/<br><br>NumDays NumDays:<!--EZCODE EMOTICON START :o --><img src=http://www.ezboard.com/images/emoticons/embarassed.gif ALT=":o"><!--EZCODE EMOTICON END--> perator++()<br>{<br>        ++hours;<br>        calcDays();<br>        return *this;<br>};<br><br>/*****************************************************************/<br><br>NumDays NumDays:<!--EZCODE EMOTICON START :o --><img src=http://www.ezboard.com/images/emoticons/embarassed.gif ALT=":o"><!--EZCODE EMOTICON END--> perator++(double)<br>{<br>        NumDays temp;<br><br>        hours++;<br>        calcDays();<br>        return temp;<br>};<br><br>/*****************************************************************/<br><br>NumDays NumDays:<!--EZCODE EMOTICON START :o --><img src=http://www.ezboard.com/images/emoticons/embarassed.gif ALT=":o"><!--EZCODE EMOTICON END--> perator--()<br>{<br>        --hours;<br>        calcDays();<br>        return *this;<br>};<br><br>/*****************************************************************/<br><br>NumDays NumDays:<!--EZCODE EMOTICON START :o --><img src=http://www.ezboard.com/images/emoticons/embarassed.gif ALT=":o"><!--EZCODE EMOTICON END--> perator--(double)<br>{<br>        NumDays temp(hours, days);<br><br>        hours--;<br>        calcDays();<br>        return temp;<br>};<br><br>/*****************************************************************/<br><br>NumDays NumDays:<!--EZCODE EMOTICON START :o --><img src=http://www.ezboard.com/images/emoticons/embarassed.gif ALT=":o"><!--EZCODE EMOTICON END--> perator*(double)<br>{<br>};<br><br>/*****************************************************************/<br><br>NumDays NumDays:<!--EZCODE EMOTICON START :o --><img src=http://www.ezboard.com/images/emoticons/embarassed.gif ALT=":o"><!--EZCODE EMOTICON END--> perator/(double)<br>{<br>};<br><br>/*****************************************************************/<br><br><br><br>The main file was given to us by the teacher:<br><br>#include <iostream.h><br>#include "numdays.h"<br><br>void main()<br>{<br>        NumDays one(25),two(15),three,four;<br>        NumDays five(12),six(1<!--EZCODE EMOTICON START 8) --><img src=http://www.ezboard.com/images/emoticons/glasses.gif ALT="8)"><!--EZCODE EMOTICON END--> ,seven(1<!--EZCODE EMOTICON START 8) --><img src=http://www.ezboard.com/images/emoticons/glasses.gif ALT="8)"><!--EZCODE EMOTICON END--> ,eight(36);<br>        NumDays nine;<br><br>        cout << "One: " << one.getDays() << endl;<br>        cout << "Two: " << two.getDays() << endl;<br>        three = one + two;<br>        cout << "Three = one + two: " << three.getDays() << endl;<br>        three = one - two;<br>        cout << "Three = one - two: " << three.getDays() << endl;<br>        four = three++;<br>        cout << "Four = Three++: " << four.getDays() << endl;<br>        four = ++three;<br>        cout << "Four = ++Three: " << four.getDays() << endl;<br>        four = three--;<br>        cout << "Four = Three--: " << four.getDays() << endl;<br>        four = --three;<br>        cout << "Four = --Three: " << four.getDays() << endl;<br>        cout << "Five: " << five.getDays() << endl;<br>        cout << "Six: " << six.getDays() << endl;<br>        cout << "Seven: " << seven.getDays() << endl;<br>        cout << "Eight: " << eight.getDays() << endl;<br>        nine = five * six;<br>        cout << "Nine = five * six: " << nine.getDays() << endl;<br>        nine = six/eight;<br>        cout << "Nine = six/eight: " << nine.getDays() << endl;<br>        if(six == seven) cout << "Six and seven are equal" << endl;<br>        else cout << "Six and seven are not equal" << endl;<br>        if(six != seven) cout << "Six and seven are not equal" << endl;<br>        else cout << "Six and seven are equal" << endl;<br>        if(seven > five) cout << "Seven is greater than five" << endl;<br>        else cout << "Seven is not greater than five" << endl;<br>        if(five > six) cout << "Five is greater than six" << endl;<br>        else cout << "Five is not greater than six" << endl;<br>        if(seven >= five) cout << "Seven is greater than or equal to five" << endl;<br>        else cout << "Seven is not greater than or equal to five" << endl;<br>        if(six >= seven) cout << "Six is greater than or equal to seven" << endl;<br>        else cout << "Six is not greater than or equal to seven" << endl;<br>        if(seven < five) cout << "Seven is less than five" << endl;<br>        else cout << "Seven is not less than five" << endl;<br>        if(five < six) cout << "Five is less than six" << endl;<br>        else cout << "Five is not less than six" << endl;<br>        if(seven <= five) cout << "Seven is less than or equal to five" << endl;<br>        else cout << "Seven is not less than or equal to five" << endl;<br>        if(six <= seven) cout << "Six is less than or equal to seven" << endl;<br>        else cout << "Six is not less than or equal to seven" << endl;<br><br>}<br><br>/*<br>Correct output:<br><br>One: 3.125<br>Two: 1.875<br>Three = one + two: 5<br>Three = one - two: 1.25<br>Four = Three++: 1.25<br>Four = ++Three: 1.5<br>Four = Three--: 1.5<br>Four = --Three: 1.25<br>Five: 1.5<br>Six: 2.25<br>Seven: 2.25<br>Eight: 4.5<br>Nine = five * six: 3.375<br>Nine = six/eight: 0.5<br>Six and seven are equal<br>Six and seven are equal<br>Seven is greater than five<br>Five is not greater than six<br>Seven is greater than or equal to five<br>Six is greater than or equal to seven<br>Seven is not less than five<br>Five is less than six<br>Seven is not less than or equal to five<br>Six is less than or equal to seven<br>Press any key to continue<br>*/ <p>-LoS-TimberWolf{FF}<br>Vehicle Specilist</p><i></i>
- XMEN Gambit
- Site Admin
- Posts: 4122
- Joined: Thu Nov 18, 1999 12:00 am
Re: C++, Creating Math Funtions For This Weeks Program
I was able to change up the picture considerably by removing the line <br>#include "numdays.h"<br><br>From the file "numdays.h". Recursive inclusion is generally a no-no. Try it, you'll like it. (Even though the number of errors actually increases, it gets rid of THOSE.) <p><!--EZCODE IMAGE START--><img src="http://www.xmenclan.org/xmengambit.gif"/><!--EZCODE IMAGE END--><br>XMEN member<br>Card-carrying DTM<br>OKL Fish-napper<br><br>Though a program be but three lines long, someday it will have to be maintained.<br><!--EZCODE ITALIC START--><em> The Tao of Programming</em><!--EZCODE ITALIC END--></p><i></i>
-
- Inmate
- Posts: 966
- Joined: Thu Jan 17, 2002 5:26 pm
Re: C++, Creating Math Funtions For This Weeks Program
recursive inclusion is fun... also try recursive constructors <!--EZCODE EMOTICON START :) --><img src=http://www.ezboard.com/images/emoticons/smile.gif ALT=":)"><!--EZCODE EMOTICON END--> <br><br>Just to be dumb, I once had a constructor that created an object of its own type... it compiled just fine, but when i ran it it never did anything but fill memory <!--EZCODE EMOTICON START :lol --><img src=http://www.ezboard.com/images/emoticons/laugh.gif ALT=":lol"><!--EZCODE EMOTICON END--> <br><br>I once had a friend that would e-mail me once a week with programs he couldn't get to compile. After something like 5 weeks of me just debugging his programs I got quite sick of it. So, this last time, instead of fixing his code for him I gave him some simple advice.<br><br>1) Until you are practiced enough to write 100+ line programs w/o a single compile, don't try it. Develope your programs bit by bit. When I was learning I started with an outside in approach. I'd start with the most basic functions/classes and wouldn't even add functionality to them until I was ready. Just build a little bit and compile and run. This drastically reduces where your errors could be. After my programs started getting far too complex for the outside in method, I'd use a modular method on top of outside in. I'd break the program down into as many parts as reasonable and then I'd work on one part at a time. Just don't tackle large projects all at once.<br><br>2) If ever you should run across a concept you don't understand, there are many good C++ books on the market. Don't get the new ones, not much has changed. I went and got me 5 books that ranged in price from $0.25 to $1.00 each. If I didn't understand a concept in one book I'd read about it in another. I works! I picked up the C++ language in a matter of hours... much less than 21 days! <!--EZCODE EMOTICON START :) --><img src=http://www.ezboard.com/images/emoticons/smile.gif ALT=":)"><!--EZCODE EMOTICON END--> <br><br>3) Its nice to ask friends for questions and help, but try to avoid relying on them. Crutches are nice when you can't get about without them, but if you rely on them too heavily your leg will never heal. (Sorry, I'm not too good with analogies)<br><br>I don't have a problem with your asking questions and seeking help. I'm just offering some help. <!--EZCODE EMOTICON START :) --><img src=http://www.ezboard.com/images/emoticons/smile.gif ALT=":)"><!--EZCODE EMOTICON END--> So I hope this helps.<br><br><br><br>Outside in, bit by bit... <p><!--EZCODE FONT START--><span style="color:red;font-size:large;">C</span><!--EZCODE FONT END--><!--EZCODE FONT START--><span style="color:orange;font-size:large;">O</span><!--EZCODE FONT END--><!--EZCODE FONT START--><span style="color:green;font-size:large;">L</span><!--EZCODE FONT END--><!--EZCODE FONT START--><span style="color:blue;font-size:large;">O</span><!--EZCODE FONT END--><!--EZCODE FONT START--><span style="color:indigo;font-size:large;">R</span><!--EZCODE FONT END--><!--EZCODE FONT START--><span style="color:violet;font-size:large;">S</span><!--EZCODE FONT END--><!--EZCODE FONT START--><span style="color:pink;font-size:xx-large;">!</span><!--EZCODE FONT END--><br><br><!--EZCODE FONT START--><span style="color:red;font-size:xx-large;"> I LOVE EZBOARD... WAIT...</span><!--EZCODE FONT END--></p><i></i>
-
- Inmate
- Posts: 128
- Joined: Sun Jan 27, 2002 4:17 pm
Re: C++, Creating Math Funtions For This Weeks Program
I have gotten all but one problem from the error list worked out. The modular, %, does not like using a double and is preventing my program from compiling. Is there any way to either trick it to work or do I have to change a double into an int to work with it? <p>-LoS-TimberWolf{FF}<br>Vehicle Specilist</p><i></i>
-
- Inmate
- Posts: 966
- Joined: Thu Jan 17, 2002 5:26 pm
Re: C++, Creating Math Funtions For This Weeks Program
mod only works with ints... <!--EZCODE EMOTICON START :( --><img src=http://www.ezboard.com/images/emoticons/frown.gif ALT=":("><!--EZCODE EMOTICON END--> <p><!--EZCODE FONT START--><span style="color:red;font-size:large;">C</span><!--EZCODE FONT END--><!--EZCODE FONT START--><span style="color:orange;font-size:large;">O</span><!--EZCODE FONT END--><!--EZCODE FONT START--><span style="color:green;font-size:large;">L</span><!--EZCODE FONT END--><!--EZCODE FONT START--><span style="color:blue;font-size:large;">O</span><!--EZCODE FONT END--><!--EZCODE FONT START--><span style="color:indigo;font-size:large;">R</span><!--EZCODE FONT END--><!--EZCODE FONT START--><span style="color:violet;font-size:large;">S</span><!--EZCODE FONT END--><!--EZCODE FONT START--><span style="color:pink;font-size:xx-large;">!</span><!--EZCODE FONT END--><br><br><!--EZCODE FONT START--><span style="color:red;font-size:xx-large;"> I LOVE EZBOARD... WAIT...</span><!--EZCODE FONT END--></p><i></i>
-
- Posts: 44
- Joined: Wed Feb 19, 2003 3:17 am
- Location: Texas
Re: C++, Creating Math Funtions For This Weeks Program
and put it in a 10X13 pan and bake for 45 min. at 350 ice when cooled <!--EZCODE EMOTICON START :duh --><img src=http://www.contrabandent.com/cwm/s/cont ... freakB.gif ALT=":duh"><!--EZCODE EMOTICON END--> <p></p><i></i>
-
- Inmate
- Posts: 128
- Joined: Sun Jan 27, 2002 4:17 pm
Re: C++, Creating Math Funtions For This Weeks Program
Well, the program has been running most of today, but it has become aparrent over the past several hours that the overloaded operators are not easy to figure out exactly how the teacher got the answeres he did. So its back to the paper to figure out how to get a mathimatical equation to work in the program with signs that are changed. Its like trying to find the top of a Morbius strip, there is only one side. <p>-LoS-TimberWolf{FF}<br>Vehicle Specilist</p><i></i>
Re: C++, Creating Math Funtions For This Weeks Program
Wow! Far out dude...<!--EZCODE EMOTICON START :smokin --><img src=http://www.ezboard.com/images/emoticons/smokin.gif ALT=":smokin"><!--EZCODE EMOTICON END--> This stuff blows right over my head..<!--EZCODE EMOTICON START :lol --><img src=http://www.ezboard.com/images/emoticons/laugh.gif ALT=":lol"><!--EZCODE EMOTICON END--> <p><!--EZCODE IMAGE START--><img src="http://homepage.ntlworld.com/planetburt ... <!--EZCODE IMAGE END--></p><i></i>