# Does This Program Work For Anyone???

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 07-08-2003
Bluesun159
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; }```
• 07-08-2003
Zach L.
All those couts...

What is the problem your having?
• 07-08-2003
Zach L.
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 }```
• 07-08-2003
Bluesun159
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.
• 07-08-2003
Cat
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";

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]
[Line 2 written to screen]

[Line 1 written to screen]
[Line 2 written to screen]

[Line 1 written to screen]
[Line 2 written to screen]

[Line 1 written to screen]
[Line 2 written to screen]

[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]
[Line 2 written to screen]

which is probably what was desired. Regardless of the age or compliance of the compiler, the second code should work as desired.
• 07-08-2003
Bluesun159
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:mad:
• 07-08-2003
Cat
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.
• 07-08-2003
Bluesun159
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; }```
• 07-08-2003
Cat
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```
• 07-11-2003
Bluesun159
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"

"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?
• 07-11-2003
codingmaster
the .o file is the compiled, executable file...

u create it using gcc...

gcc -c sourcecode.c -o outputfile.o

you're using c++
so:

g++ -c sourcecode.cpp -o outputfile.o
[/edit]
• 07-11-2003
Bluesun159
• 07-11-2003
Cat
You misspelled #ifndef
• 07-11-2003
Bluesun159
Quote:

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??
• 07-11-2003
Cat
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.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last