Thread: Help please :-)

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    33

    Help please :-)

    Hey im having trouble, this is only the second program ive dont in c++, anyhow, its a group project for school dealing with math with fractions, here is the code for my header file:

    Code:
    #include<iomanip.h>
    #include<iostream.h>
    #include"16-6.h"
    
    numbers::numbers(int num, int den)
    {
      int x, y;
      x = num;
      y = den;
    }
    
    void numbers::printfrac()
    {
      cout << x << "/" << y; /* prints as fraction */
    }
    
    void numbers::printdec()
    {
      
      cout << dec /* prints in decimal form */
           << setprecision(3) << (float)x/y;
    }
    void numbers::reduction()		/* reduces fraction */
    {
      int i, small;
      if (x < y)
        small = x;
      else
        small = y;
      for (i = small; 1 >= 1; i--)
        if(x % i == 0; && y %i == 0)
          {
    	y = y / i;
    	x = x / i;
          }
    }
    numbers numbers::multiply(numbers a, numbers b)
    {
      int whole = 0;
      numbers d;
      d = a.x * b.y;
      d.reduction();
      return d;
    }
    
    numbers numbers::divide(numbers a, numbers b)
    {
      int whole = 0;
      numbers d;
      d = a.x * b.y; // invert and multiply
      d = b.x * a.y;
      d.reduction();
      return d;
    }
    
    numbers numbers::add(numbers a, numbers b)
    {
      int t1 = 0, t2 = 0, temp_num1 = 0, temp_num2 = 0, temp_den1 = 0,
        temp_den2 = 0, whole = 0;
      numbers d;
      t1 = a.y;
      t2 = b.y;
      temp_num1 = a.x * t2;
      temp_den1 = a.y * t2;
      temp_num2 = b.x * t1;
      temp_den2 = b.y * t1;
      d.x = temp_num1 + temp_num2;
      d.y = temp_den1;
      d.reduction();
      return d;
    }
    
    numbers numbers::subtract(numbers a, numbers b)
    {
      int t1 = 0, t2 = 0, temp_num1 = 0, temp_num2 = 0, temp_den1 = 0,
        temp_den2 = 0, whole = 0;
      numbers d;
      t1 = a.y;
      t2 = b.y;
      temp_num1 = a.x * t2;
      temp_den1 = a.y * t2;
      temp_num2 = b.x * t1;
      temp_den2 = b.y * t1;
      d.x = temp_num1 - temp_num2;
      d.y = temp_den1;
      d.reduction();
      return d;
    }

    here are the errors i get

    "student2n3.h", line 18.11: 1540-0274 (S) The name lookup for "x" did not find a declaration.
    "student2n3.h", line 18.23: 1540-0274 (S) The name lookup for "y" did not find a declaration.
    "student2n3.h", line 25.37: 1540-0274 (S) The name lookup for "x" did not find a declaration.
    "student2n3.h", line 25.39: 1540-0274 (S) The name lookup for "y" did not find a declaration.
    "student2n3.h", line 35.18: 1540-0064 (S) Syntax error: ")" was expected but ";" was found.
    "student2n3.h", line 45.8: 1540-0217 (S) "x" is not a member of "class numbers".
    "student2n3.h", line 45.14: 1540-0217 (S) "y" is not a member of "class numbers".
    "student2n3.h", line 54.8: 1540-0217 (S) "x" is not a member of "class numbers".
    "student2n3.h", line 54.14: 1540-0217 (S) "y" is not a member of "class numbers".
    "student2n3.h", line 55.8: 1540-0217 (S) "x" is not a member of "class numbers".
    "student2n3.h", line 55.14: 1540-0217 (S) "y" is not a member of "class numbers".
    "student2n3.h", line 65.9: 1540-0217 (S) "y" is not a member of "class numbers".
    "student2n3.h", line 66.9: 1540-0217 (S) "y" is not a member of "class numbers".
    "student2n3.h", line 67.16: 1540-0217 (S) "x" is not a member of "class numbers".
    "student2n3.h", line 68.16: 1540-0217 (S) "y" is not a member of "class numbers".
    "student2n3.h", line 69.16: 1540-0217 (S) "x" is not a member of "class numbers".
    "student2n3.h", line 70.16: 1540-0217 (S) "y" is not a member of "class numbers".
    "student2n3.h", line 71.4: 1540-0217 (S) "x" is not a member of "class numbers".
    "student2n3.h", line 72.4: 1540-0217 (S) "y" is not a member of "class numbers".
    "student2n3.h", line 82.9: 1540-0217 (S) "y" is not a member of "class numbers".
    "student2n3.h", line 83.9: 1540-0217 (S) "y" is not a member of "class numbers".
    "student2n3.h", line 84.16: 1540-0217 (S) "x" is not a member of "class numbers".
    "student2n3.h", line 85.16: 1540-0217 (S) "y" is not a member of "class numbers".
    "student2n3.h", line 86.16: 1540-0217 (S) "x" is not a member of "class numbers".
    "student2n3.h", line 87.16: 1540-0217 (S) "y" is not a member of "class numbers".
    "student2n3.h", line 88.4: 1540-0217 (S) "x" is not a member of "class numbers".
    "student2n3.h", line 89.4: 1540-0217 (S) "y" is not a member of "class numbers".


    if i need to post the other header files or the main program for my question to be answered let me know, but what am i doing wrong?


    Thanks in advance


    EDIT::: where it makes the happy face, its because it is ": p" sorry

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    did u declare x and y within your class?

    Code:
    class numbers
    {
    public:
    ...
    
    private:
    int x;
    int y;
    if you didn't, then you should remove the declarations from your constructor as well:
    Code:
    numbers::numbers(int num, int den)
    {
    //  int x, y; <--not this
      x = num;
      y = den;
    }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    33
    well this is the interface header file

    Code:
    #include <iostream.h>
    #ifndef fraction_h
    #define fraction_h
    
    class numbers
    {
     private:
      int num, den;
    
     public:
    
      numbers(int num = 0, int den = 1);
    
      void reduction();
      numbers add(numbers, numbers);
      numbers subtract(numbers, numbers);
      numbers multiply(numbers, numbers);
      numbers divide(numbers, numbers);
      void printfrac();
      void printdec();
      
    };
    #endif

    does that help any more?

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    first of all in your program you are passing in values to the constructor that are declared in the class
    Code:
    numbers::numbers(int num, int den)
    {
      int x, y;
      x = num;
      y = den;
    }
    instead of that you would want to put:
    Code:
    numbers::numbers(int x, int y) //the numbers you pass in 
    //when you create an instance - ie: numbers myNum(1,2)
    {
      //int x, y; dont need this
      //x = num; we want to change the value of variables declared in the class
    
    num = x;
    den = y;
      
    }
    so when you create an instance within main you can set the values of the variables within the class
    in main would be something like this:
    Code:
    numbers myNum(1,5);
    doing it this way, you would have to change x and y
    to num and den whenever they appear in your functions

    at least that is what it seems to look like, without reading too in depth into your code
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    http://cboard.cprogramming.com/annou...p?s=&forumid=6 Check 14.2 on that list. Good job with the code tags, though. There's a little check box below where you type that says "Disable smilies in this post," just fyi, beacuse you asked...well...sorta asked.
    Away.

Popular pages Recent additions subscribe to a feed