Thread: Does This Program Work For Anyone???

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    49

    Question Does This Program Work For Anyone???

    Here is the program:

    DineOut.h

    Code:
    class DineOut
    {
     public:
     DineOut(); //void constructor
     double FiftyTimes(double x);
     double Twice(double x);
     double AddFive(double x);
     double HadBirth(double x);
     double NotHadBirth(double x);
    
     protected:
     double x;
    };
    
    double FiftyTimes(double x)
    {
     return x*50;
    }
    
    double Twice(double x)
    {
     return x*2;
    }
    
    double AddFive(double x)
    {
     return x+5;
    }
    
    double HadBirth(double x)
    {
     return x+1753;
    }
    
    double NotHadBirth(double x)
    {
     return x+1752;
    }
    DineOut.cpp

    Code:
    #include <iostream>
    #include <cstdlib>
    #include "DineOut.h"
    
    int main()
    {
     using namespace std;
     int DineOut;
     int YearBorn;
     int FinalNumber;
     int HaveYouHadBirth;
    
     cout<<"DINING OUT MATHEMATICS.\n\n";
     system("PAUSE");
     cout<<"\n";
     cout<<"\t"<<"Here is some really neat math!\n";
     cout<<"\a\v";
     cout<<"First, enter the number of times that \n";
     cout<<"you would like to dine out a week.\n";
     cout<<"(This number must be >1 but <10.)";
     cout<<"\n\n\t";
     cin>>DineOut;
     cout<<"\n\n";
     cout<<"Now, we will multiply this number by 2.\n";
     cout<<"(Just to be bold!)";
     cout<<"\n\n\t";
     cout<<DineOut<<" x 2 = "<<Twice(DineOut)<<"\n\n";
     system("PAUSE");
     cout<<"\n\n";
     cout<<"Now, we will add 5 for Sunday.";
     cout<<"\n\n\t";
     cout<<DineOut<<" + 5 = "<<AddFive(DineOut)<<"\n\n";
     system("PAUSE");
     cout<<"\n\n";
     cout<<"Now Multiply it by 50.";
     cout<<"\n\n\t";
     cout<<DineOut<<" x 50 = "<<FiftyTimes(DineOut)<<"\n\n";
     system("PAUSE");
     cout<<"\n\n";
     cout<<"Now, if you've had your birthday, please press 1. ";
     cout<<"\n"<<"If you haven't, please press two.";
     cout<<"\n\t";
     cin>>HaveYouHadBirth;
     if (HaveYouHadBirth==1)
     {
     cout<<"\n\n";
     cout<<"Since you've had your birthday, we'll add 1753.";
     cout<<"\n\n\t"<<DineOut<<" + 1753 = "<<HadBirth(DineOut);
     cout<<"\n\n";
     system("PAUSE");
     }
     else if (HaveYouHadBirth==2)
     {
     cout<<"\n\n";
     cout<<"Since you haven't had your birthday, we'll add 1752.";
     cout<<"\n\n\t"<<DineOut<<" + 1752 = "<<NotHadBirth(DineOut);
     cout<<"\n\n";
     system("PAUSE");
     }
     else
     {
     cout<<"You have not entered a valid number.\a\n\a\n";
     system("PAUSE");
     cout<<"This program will therefore not work correctly.";
     }
     cout<<"\n\n";
     cout<<"Now, enter the year you were born.";
     cout<<"\n\n\t";
     cin>>YearBorn;
     cout<<"\n\n";
     cout<<"Now, we will subtract the year you were born.";
     FinalNumber=DineOut - YearBorn;
     cout<<"\n\n\t"<<DineOut<<" - "<<YearBorn<<" = "<<FinalNumber;
     cout<<"\n\n";
     system("PAUSE");
     cout<<"\n\n";
     cout<<"Finally, you should be left with a three digit number.";
     cout<<"\n\t"<<FinalNumber;
     cout<<"The first digit is the number of times you would like to\n";
     cout<<"dine out a week.\n";
     cout<<"The last two numbers are:\n"<<"duh duh duh duuuuuuuuuhhhhhhh!!!\n";
     cout<<"YOUR AGE!!! HAHAHA!!!";
     system("PAUSE");
     return 0;
    
    
    }
    DJGPP-Complier
    Windows 98-(Shouldn't need to explain)

    I like plants.

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    All those couts...

    What is the problem your having?
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Okay, I see some potential problems:

    int DineOut will probably cause some ambiguities with the class DineOut.

    To construct a member of the class:
    DineOut myObj;

    Then, you access functions like this:
    int y = myObj.FiftyTimes(4); // y = 200

    And member functions are defined as follows:
    Code:
    class c
    {
    public:
       void f();
    }
    
    void c::f()
    {
      // definition
    }
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    49
    As I said the other time I posted this program, none of the iostream.h functions are working in the executable. I just want to know it the program works perfectly in anyone else's complier.
    DJGPP-Complier
    Windows 98-(Shouldn't need to explain)

    I like plants.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    As at least one person said in the other thread, don't use "\n" for a line break, use endl. E.g. do NOT do this:

    cout<<"DINING OUT MATHEMATICS.\n\n";

    and instead do this:

    cout<<"DINING OUT MATHEMATICS." << endl << endl;

    they are NOT exactly the same, at least historically, and I wager the difference between them is the culprit.

    Consider this code:

    Code:
    int i, j;
    cout << "Line 1\n";
    cin >> i;
    cout << "Line 2\n";
    cin >> j;
    Pop quiz: How many different (from the user's perspective) ways might this show on the screen?

    Answer: Either one or five. In C++98, there is only one legal way for this input/output to happen, but in compilers that don't fully support C++98, there are 5 possibilities:

    [Line 1 written to screen]
    [i read]
    [Line 2 written to screen]
    [j read]

    [Line 1 written to screen]
    [i read]
    [j read]
    [Line 2 written to screen]

    [i read]
    [Line 1 written to screen]
    [Line 2 written to screen]
    [j read]

    [i read]
    [Line 1 written to screen]
    [j read]
    [Line 2 written to screen]

    [i read]
    [j read]
    [Line 1 written to screen]
    [Line 2 written to screen]

    Only the first is truly legal, for fully C++98 compilers, because cin and cout are supposed to be synchronized by the standard. Older compilers, however, do not obey this criteria, and the latter 4 sequences will be seen on those compilers.

    However, the following code:

    Code:
    int i, j;
    cout << "Line 1" << endl;
    cin >> i;
    cout << "Line 2" << endl;
    cin >> j;
    has only one possible output, regardless of whether the compiler fully supports C++98:

    [Line 1 written to screen]
    [i read]
    [Line 2 written to screen]
    [j read]

    which is probably what was desired. Regardless of the age or compliance of the compiler, the second code should work as desired.
    Last edited by Cat; 07-08-2003 at 07:19 PM.

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    49
    Here is the revised program:

    DineOut.cpp

    Code:
    #include <iostream>
    #include <cstdlib>
    #include "DineOut.h"
    
    int main()
    {
     using namespace std;
     int DineOut;
     int YearBorn;
     int FinalNumber;
     int HaveYouHadBirth;
    
     cout<<"DINING OUT MATHEMATICS."<<endl<<endl;
     system("PAUSE");
     cout<<endl;
     cout<<"\t"<<"Here is some really neat math!"<<endl;
     cout<<"\a\v";
     cout<<"First, enter the number of times that "<<endl;
     cout<<"you would like to dine out a week."<<endl;
     cout<<"(This number must be >1 but <10.)";
     cout<<endl<<endl<<"\t";
     cin>>DineOut;
     cout<<endl<<endl;
     cout<<"Now, we will multiply this number by 2."<<endl;
     cout<<"(Just to be bold!)";
     cout<<endl<<endl<<"\t";
     cout<<DineOut<<" x 2 = "<<Twice(DineOut)<<endl<<endl;
     system("PAUSE");
     cout<<endl<<endl;
     cout<<"Now, we will add 5 for Sunday.";
     cout<<endl<<endl<<"\t";
     cout<<DineOut<<" + 5 = "<<AddFive(DineOut)<<endl<<endl;
     system("PAUSE");
     cout<<endl<<endl;
     cout<<"Now Multiply it by 50.";
     cout<<endl<<endl<<"\t";
     cout<<DineOut<<" x 50 = "<<FiftyTimes(DineOut)<<endl<<endl;
     system("PAUSE");
     cout<<endl<<endl;
     cout<<"Now, if you've had your birthday, please press 1. ";
     cout<<endl<<"If you haven't, please press two.";
     cout<<endl<<"\t";
     cin>>HaveYouHadBirth;
     if (HaveYouHadBirth==1)
     {
     cout<<endl<<endl;
     cout<<"Since you've had your birthday, we'll add 1753.";
     cout<<endl<<endl<<"\t"<<DineOut<<" + 1753 = "<<HadBirth(DineOut);
     cout<<endl<<endl;
     system("PAUSE");
     }
     else if (HaveYouHadBirth==2)
     {
     cout<<endl<<endl;
     cout<<"Since you haven't had your birthday, we'll add 1752.";
     cout<<endl<<endl<<"\t"<<DineOut<<" + 1752 = "<<NotHadBirth(DineOut);
     cout<<endl<<endl;
     system("PAUSE");
     }
     else
     {
     cout<<"You have not entered a valid number.\a"<<endl<<"\a"<<endl;
     system("PAUSE");
     cout<<"This program will therefore not work correctly.";
     }
     cout<<endl<<endl;
     cout<<"Now, enter the year you were born.";
     cout<<endl<<endl<<"\t";
     cin>>YearBorn;
     cout<<endl<<endl;
     cout<<"Now, we will subtract the year you were born.";
     FinalNumber=DineOut - YearBorn;
     cout<<endl<<endl<<"\t"<<DineOut<<" - "<<YearBorn<<" = "<<FinalNumber;
     cout<<endl<<endl;
     system("PAUSE");
     cout<<endl<<endl;
     cout<<"Finally, you should be left with a three digit number.";
     cout<<endl<<endl<<"\t"<<FinalNumber;
     cout<<"The first digit is the number of times you would like to"<<endl;
     cout<<"dine out a week."<<endl;
     cout<<"The last two numbers are:"<<endl<<"duh duh duh duuuuuuuuuhhhhhhh!!!"<<endl;
     cout<<"YOUR AGE!!! HAHAHA!!!";
     system("PAUSE");
     return 0;
    
    
    }
    And BTW: It still will not work!!!:z
    DJGPP-Complier
    Windows 98-(Shouldn't need to explain)

    I like plants.

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Read Zach's post, all your functions are wrong, and you use the same name for a variable and a class. The inputs and outputs work fine, those are correct, it's the actual class and function calls that are wrong.

    It shouldn't even compile, and it probably doesn't, I'm assuming.
    Last edited by Cat; 07-08-2003 at 07:32 PM.

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    49
    I changed the code, but it still doesn't work for me.

    DineOut.h

    Code:
    class DineOut
    {
     public:
     DineOut(); //void constructor
     double FiftyTimes(double x);
     double Twice(double x);
     double AddFive(double x);
     double HadBirth(double x);
     double NotHadBirth(double x);
    
     protected:
     double x;
    };
    
    double DineOut::FiftyTimes(double x)
    {
     return x*=50;
    }
    
    double DineOut::Twice(double x)
    {
     return x*=2;
    }
    
    double DineOut::AddFive(double x)
    {
     return x+=5;
    }
    
    double DineOut::HadBirth(double x)
    {
     return x+=1753;
    }
    
    double DineOut::NotHadBirth(double x)
    {
     return x+=1752;
    }
    DineOut.cpp

    Code:
    #include <iostream>
    #include <cstdlib>
    #include "DineOut.h"
    
    int main()
    {
     using namespace std;
     int EatOut;
     int YearBorn;
     int FinalNumber;
     int HaveYouHadBirth;
    
     cout<<"DINING OUT MATHEMATICS."<<endl<<endl;
     system("PAUSE");
     cout<<endl;
     cout<<"\t"<<"Here is some really neat math!"<<endl;
     cout<<"\a\v";
     cout<<"First, enter the number of times that "<<endl;
     cout<<"you would like to dine out a week."<<endl;
     cout<<"(This number must be >1 but <10.)";
     cout<<endl<<endl<<"\t";
     cin>>EatOut;
     cout<<endl<<endl;
     cout<<"Now, we will multiply this number by 2."<<endl;
     cout<<"(Just to be bold!)";
     cout<<endl<<endl<<"\t";
     cout<<EatOut<<" x 2 = "<<Twice(EatOut)<<endl<<endl;
     system("PAUSE");
     cout<<endl<<endl;
     cout<<"Now, we will add 5 for Sunday.";
     cout<<endl<<endl<<"\t";
     cout<<EatOut<<" + 5 = "<<AddFive(EatOut)<<endl<<endl;
     system("PAUSE");
     cout<<endl<<endl;
     cout<<"Now Multiply it by 50.";
     cout<<endl<<endl<<"\t";
     cout<<EatOut<<" x 50 = "<<FiftyTimes(EatOut)<<endl<<endl;
     system("PAUSE");
     cout<<endl<<endl;
     cout<<"Now, if you've had your birthday, please press 1. ";
     cout<<endl<<"If you haven't, please press two.";
     cout<<endl<<"\t";
     cin>>HaveYouHadBirth;
     if (HaveYouHadBirth==1)
     {
     cout<<endl<<endl;
     cout<<"Since you've had your birthday, we'll add 1753.";
     cout<<endl<<endl<<"\t"<<EatOut<<" + 1753 = "<<HadBirth(EatOut);
     cout<<endl<<endl;
     system("PAUSE");
     }
     else if (HaveYouHadBirth==2)
     {
     cout<<endl<<endl;
     cout<<"Since you haven't had your birthday, we'll add 1752.";
     cout<<endl<<endl<<"\t"<<EatOut<<" + 1752 = "<<NotHadBirth(EatOut);
     cout<<endl<<endl;
     system("PAUSE");
     }
     else
     {
     cout<<"You have not entered a valid number.\a"<<endl<<"\a"<<endl;
     system("PAUSE");
     cout<<"This program will therefore not work correctly.";
     }
     cout<<endl<<endl;
     cout<<"Now, enter the year you were born.";
     cout<<endl<<endl<<"\t";
     cin>>YearBorn;
     cout<<endl<<endl;
     cout<<"Now, we will subtract the year you were born.";
     FinalNumber=EatOut - YearBorn;
     cout<<endl<<endl<<"\t"<<EatOut<<" - "<<YearBorn<<" = "<<FinalNumber;
     cout<<endl<<endl;
     system("PAUSE");
     cout<<endl<<endl;
     cout<<"Finally, you should be left with a three digit number.";
     cout<<endl<<endl<<"\t"<<FinalNumber;
     cout<<"The first digit is the number of times you would like to"<<endl;
     cout<<"dine out a week."<<endl;
     cout<<"The last two numbers are:"<<endl<<"duh duh duh duuuuuuuuuhhhhhhh!!!"<<endl;
     cout<<"YOUR AGE!!! HAHAHA!!!";
     system("PAUSE");
     return 0;
    
    
    }
    Now, may I please ask that you try this program on your complier, like it asks in the topic title?
    DJGPP-Complier
    Windows 98-(Shouldn't need to explain)

    I like plants.

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    It won't even compile, there are a lot of errors left. As was stated before, you're not correctly calling any functions at all. For example:

    cout<<EatOut<<" x 2 = "<<Twice(EatOut)<<endl<<endl;

    now, *I* know that you're trying to call DineOut::Twice(EatOut), but the compiler won't. You don't even make an object of class DineOut, you can't call ANY of DineOut's functions without doing that first.

    I think you should go all the way back to basics. You need to do some more reading on classes and objects before you can actually make this work.

    Here's a little short thing to show you something that will work:

    Code:
    // test.h
    #ifndef INCLUDED_TEST_H
    #define INCLUDED_TEST_H
    
    class MyClass{
    private:
      int _val;
    public:
      MyClass(int number):_val(number){}
      int Double(){
        return _val *= 2;
      }
      int AddNinety(){
        return _val += 90;
      }
    };
    
    #endif
    
    // test.cpp
    
    #include <iostream>
    #include "test.h"
    using namespace std;
    
    int main(){
      int i;
      cout << "Enter the number: " <<flush;
      cin >> i;
      MyClass myObject(i);
      cout << i << " doubled is " << myObject.Double() << endl;
      cout << "After adding 90: " << myObject.AddNinety() << endl;
    }
    Note how we create an object from out class, we initialize its member variable, and then we call its member functions.

    Sample output:

    Code:
    Enter the number: 21
    21 doubled is 42
    After adding 90: 132
    Last edited by Cat; 07-08-2003 at 07:57 PM.

  10. #10
    Registered User
    Join Date
    Sep 2002
    Posts
    49
    DineOut.h

    Code:
    #indef INCLUDED_DINEOUT_H
    #define INCLUDED_DINEOUT_H
    
    class DineOut
    {
     public:
     DineOut(); //void constructor
     int FiftyTimes(int x);
     int Twice(int x);
     int AddFive(int x);
     int HadBirth(int x);
     int NotHadBirth(int x);
    
     private:
     int x;
    };
    
    int DineOut::FiftyTimes(int x)
    {
     return x*=50;
    }
    
    int DineOut::Twice(int x)
    {
     return x*=2;
    }
    
    int DineOut::AddFive(int x)
    {
     return x+=5;
    }
    
    int DineOut::HadBirth(int x)
    {
     return x+=1753;
    }
    
    int DineOut::NotHadBirth(int x)
    {
     return x+=1752;
    }
    
    
    #endif
    It is telling me: "3 diningoutmath.cpp
    C:\DEV-C_~1\INCLUDE\DineOut.h:44: unbalanced `#endif"


    The linker is telling me:

    "g++: c:\windows\desktop\folders\jason's folders\programs\programming\diningoutmath.o: No such file or directory

    g++: file path prefix `C:\DEV-C_~1\BIN\' never used"



    The linker is right, there is no diningoutmath.o, how do I create one?
    DJGPP-Complier
    Windows 98-(Shouldn't need to explain)

    I like plants.

  11. #11
    Registered User codingmaster's Avatar
    Join Date
    Sep 2002
    Posts
    309
    the .o file is the compiled, executable file...

    u create it using gcc...

    gcc -c sourcecode.c -o outputfile.o

    [edit]
    you're using c++
    so:

    g++ -c sourcecode.cpp -o outputfile.o
    [/edit]
    Last edited by codingmaster; 07-11-2003 at 09:14 AM.

  12. #12
    Registered User
    Join Date
    Sep 2002
    Posts
    49
    What about my other problems???
    DJGPP-Complier
    Windows 98-(Shouldn't need to explain)

    I like plants.

  13. #13
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    You misspelled #ifndef

  14. #14
    Registered User
    Join Date
    Sep 2002
    Posts
    49
    Originally posted by Cat

    Code:
    MyClass myObject(i);
      cout << i << " doubled is " << myObject.Double() << endl;
      cout << "After adding 90: " << myObject.AddNinety() << endl;
    }
    Note how we create an object from out class, we initialize its member variable, and then we call its member functions.

    Does myObject have to all ready be declared in my class or do I make it up on the spot??
    DJGPP-Complier
    Windows 98-(Shouldn't need to explain)

    I like plants.

  15. #15
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    myObject can't be declared in the class; myObject is an instance of the class.

    It's the same thing like this:

    int x;

    This declares a variable 'x', which is an instance of the type 'int'.

    MyClass myObject;

    This declares an object 'myObject' which is an instance of the class 'MyClass'.

    I guess you can think of a class as a blueprint -- it tells the compiler how to build objects. You use the objects themselves. You can't drive the blueprint of a car, but you can use the blueprint to make a car to drive.

    There are exceptions to the rules, but for now, just use as a rule of thumb that you use classes to make objects, and you then use the objects themselves.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. threaded program doesn't work?
    By OcTO_VIII in forum Linux Programming
    Replies: 1
    Last Post: 12-11-2003, 12:37 PM
  3. The Bludstayne Open Works License
    By frenchfry164 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-26-2003, 11:05 AM
  4. Can't get program to work
    By theirishrover12 in forum C Programming
    Replies: 1
    Last Post: 06-08-2002, 11:10 AM
  5. how does this program work
    By ssjnamek in forum C++ Programming
    Replies: 2
    Last Post: 01-02-2002, 01:48 PM