Results 1 to 5 of 5

Thread: Compilation problem with c++ program

  1. #1

    Compilation problem with c++ program

    Hey everyone. Me again with another problem.. :
    I cant get this program to compile using a class I wrote for my C++ class. This is my first attempt at using/writing classes, so its probably OE on my part. Any tips are more than welcomed!!

    Code:
    THis is the included Class I wrote
    /*
    
    Program Name:
    Author: Mark Birchfield
    Course: CSC 2134
    Date Due:
    
    Purpose:
        
    Data Dictionary
    Variable Name               Data Type               Use/Value
    
    
    */
    
    
    #include <math.h>
    #include <ctype.h>
    
    class arch_win
    {
        public:
        //methods go here
        float set_height_width()
        {height=h; width=w; set_radius();set_area();}
        float set_price()
        {price=p;set_totalprice();}
        void arch_win()
        {h=0,w=0,p=0, set_pi();}
        
        private:
            float pi;
            float height,width,h,w,p;
            float price,totalprice, radius, area;
            char aORr;
            float set_pi(){pi=3.14159;}
            float set_radius()
            {radius=(width * .5);};
            float set_area();
            float set_totalprice(){ totalprice=price*area; };
            float get_height(){return height;}
            float get_width(){return width;}
            float get_price(){return price;}
            float get_area(){return area;}
            float get_totalprice(){return totalprice;}
    };
    
    float arch_win::set_area()
            { 
             
              if (toupper(aORr)=='A')
              {
                area=((height*width)+ (.5*(pow(radius,2)* pi))); 
              }
              else 
              {
                area=height*width;
              }
            }
    And here is the main program:
    Code:
    /*
    Program Name:
    Author: Mark Birchfield
    
    */
    
    #include <iostream>
    #include <conio.h>
    #include <iomanip>
    #include "arch_rect_win.cpp"
    
    using namespace std;
    
    int main()
    {
     float h=0,w=0,p=0; //set all float variables to 0
     char aORr;
     arch_win();
     while (h<w)
     {
     cout<<"\nEnter Window Height: ";
     cin>>h;
     cout<<"\nEnter Window Width: ";
     cin>>w;
     if (h<w)
       cout<<"Height must be greater than or Equal to Width!!\n";
       
     }
     
     cout<<"\nEnter Price Per Sq in: ";
     cin>>p;
     cout<<"\nEnter A for Arch, R for Rectangle: ";
     cin>>aORr;
     set_height_width();
     set_price();
     
     //-------------------Formatting and Displaying Results----------------
     
     cout <<"\n\nHeight"<<setw(7)<<"Width"<<setw(13)<<"PPSqinch";
    cout <<setw(8)<<"Area" <<setw(15)<<"Price\n";
    cout <<"___________________________________________________\n";
    cout<<setiosflags(ios::showpoint|ios::fixed)<< setprecision(1)<<get_height();
    cout<<setw(8)<<setprecision(1)<<get_width();
    cout<<setw(8)<<setprecision(2)<<"$"<<get_price();
    cout<<setw(10)<<setprecision(1)<<get_area();
    cout<<setw(8)<<setprecision(2)<<"$"<<get_totalprice();
    getch();
    }

  2. #2

    Re:Compilation problem with c++ program

    What's the error?

    Google turned up this about how to use a class. I've never seen a constructor implicitly declared within the class like you've done, but if it works then that's ok.

    (Link Fixed ) This will give you the low down on how to build a constructor and how to initialize an instance of your class in the main program.

    Also, once you've initialized an instance of your class, you have to specify when you're calling a method from that class. ie,

    arch_win a;
    a.set_height_width(... )

  3. #3
    Moderator
    Advisor
    redhead's Avatar
    Join Date
    Jun 2001
    Location
    Copenhagen, Denmark
    Posts
    811

    Re:Compilation problem with c++ program

    You need to redefine your entire class, since it is not following the way you define and use classes.
    Something like this migth do the job.. But it hasn't been tested, it's just to give you an idear.
    Code:
    #include <iostream>
    #include <iomanip>
    #include <math.h>
    #include <ctype.h>
    
    using namespace std;
    
    const float pi = 3.14159; // this will NEVER change
    
    class arch_win
    {
    private:
      /* anything that shouldn't be tampered with outside the class */
      /* in case of anything dosn't add up, everything is 0 */
      float height, width, price, totalprice, radius, area;
    
    
      void set_radius(void)
      {
        radius = (width * .5);
      }
      void set_total_price(void)
      {
        totalprice = price * area;
      }
    
    public:
      /* all things that should be accessible outside the class */
      void set_area(char ar)
      {
        if (toupper(ar)=='A')
          area=((height*width)+ (.5*(pow(radius,2)* pi)));
        else      
          area=height*width; 
      };
      /* Any combination of constructor for the class */
      arch_win()
      {
        height = 0;
        width  = 0;
        radius = 0;
        area   = 0;
      }
      arch_win(float h)
      {
        height = h;
        width  = 0;
        radius = 0;
        area   = 0;
      }
      arch_win(float h, float w)
      {
        height = h;
        width  = w;
        set_radius(w);
        area   = 0;
        /* theres no aORr to decide area here set_area() will
         * have to explicit be called from outside 
         */
      }
      arch_win(float h, float w, char ar)
      {
        height  = h;
        width   = w;
        set_radius();
        set_area(ar);
      }
      void set_price(float p)
      {
        price = p;
        set_total_price();
      }
      /* we should be able to fetch any internal info */
      float get_height()
      {
        return height;
      }
      float get_width()
      {
        return width;
      }
      float get_price()
      {
        return price;
      }
      float get_area()
      {
        return area;
      }
      float get_totalprice()
      {
        return totalprice;
      }
    };
    
    
    /* try to use the class in a program */
    
    int main()
    {
      float h=0, w=0, p=0; //set all float variables to 0
      char aORr = '\0';
      while ((!h&&!w)|| (h < w))
        {
          cout << "\nEnter Window Height: ";
          cout.flush();
          cin >> h;
          cout << "\nEnter Window Width: ";
          cout.flush();
          cin >> w;
          if (h < w)
       cout << "Height must be greater than or Equal to Width!!\n";
        }
      
      cout << "\nEnter Price Per Sq in: ";
      cout.flush();
      cin >> p;
      cout << "\nEnter A for Arch, R for Rectangle: ";
      cout.flush();
      cin >> aORr;
    
      /* call the constructor which match the input */
      arch_win AW(h, w, aORr);
      AW.set_price(p);
      //-------------------Formatting and Displaying Results----------------
      
      cout << "\n\nHeight" << setw(7) << "Width" << setw(13) << "PPSqinch";
      cout << setw(8) << "Area" << setw(15) << "Price\n";
      cout << "___________________________________________________\n";
      cout << setiosflags(ios::showpoint|ios::fixed) << setprecision(1)
        /* get the height from our class */
           << AW.get_height();
      cout << setw(8) << setprecision(1)
        /* get width from class */
           << AW.get_width();
      cout << setw(8) << setprecision(2) << "$" 
        /* get price from class */
           << AW.get_price();
      cout << setw(10) << setprecision(1)
        /* get area from class */
           << AW.get_area();
      cout << setw(8) << setprecision(2) << "$"
        /* get total price */
           << AW.get_totalprice();
      cout << endl; /* flush any output */
      /* halt user input */
      cin >> aORr;
      return 0;
    }
    //edit// OK, its been tested, and it does what it should.
    redhead@fenris{80} ~> g++ -o ib ib.cpp
    redhead@fenris{81} ~> ./ib

    Enter Window Height: 2

    Enter Window Width: 1

    Enter Price Per Sq in: 12

    Enter A for Arch, R for Rectangle: a


    Height Width PPSqinch Area Price
    __________________________________________________ _
    2.0 1.0 $12.00 2.4 $28.71
    redhead@fenris{82} ~>

  4. #4

    Re:Compilation problem with c++ program

    I figured I was having problems with my constructors. Im still in the process of learning c++. I would put it in the same file as the program, but the teacher explicitly specicified two files. Thanks for the help!!

  5. #5
    Moderator
    Advisor
    redhead's Avatar
    Join Date
    Jun 2001
    Location
    Copenhagen, Denmark
    Posts
    811

    Re:Compilation problem with c++ program

    Just create a header file ie:
    Code:
    // arch_win.h
    
    #ifndef __ARCH_WIN__H__
    #define __ARCH_WIN__H__
    
    const float pi = 3.14159; // this will NEVER change
    
    class arch_win
    {
    private:
      /* anything that shouldn't be tampered with outside the class */
      /* in case of anything dosn't add up, everything is 0 */
      float height, width, price, totalprice, radius, area;
    
    
      void set_radius(void){  };
      void set_total_price(void){};
    
    public:
      /* all things that should be accessible outside the class */
      void set_area(char ar){};
      /* Any combination of constructor for the class */
      arch_win(){};
      arch_win(float h){};
      arch_win(float h, float w){};
      arch_win(float h, float w, char ar){};
      void set_price(float p){};
      /* we should be able to fetch any internal info */
      float get_height(){};
      float get_width(){};
      float get_price(){};
      float get_area(){};
      float get_totalprice(){};
    };
    
    #endif
    And a code file ie:
    Code:
    // arch_win.cpp
    
    #include <math.h>
    #include "arch_win.h"
    
    void arch_win::set_radius(void)
      {
        radius = (width * .5);
      }
    void arch_win::set_total_price(void)
      {
        totalprice = price * area;
      }
    
      /* all things that should be accessible outside the class */
    void arch_win::set_area(char ar)
      {
        if (toupper(ar)=='A')
          area=((height*width)+ (.5*(pow(radius,2)* pi)));
        else      
          area=height*width; 
      };
      /* Any combination of constructor for the class */
    void arch_win::arch_win()
      {
        height = 0;
        width  = 0;
        radius = 0;
        area  = 0;
      }
    void arch_win::arch_win(float h)
      {
        height = h;
        width  = 0;
        radius = 0;
        area  = 0;
      }
    void arch_win::arch_win(float h, float w)
      {
        height = h;
        width  = w;
        arch_win::set_radius(w);
        area  = 0;
        /* theres no aORr to decide area here set_area() will
        * have to explicit be called from outside 
        */
      }
      
    void arch_win::arch_win(float h, float w, char ar)
      {
        height  = h;
        width  = w;
        arch_win::set_radius();
        arch_win::set_area(ar);
      }
    
    void arch_win::void set_price(float p)
      {
        price = p;
        arch_win::set_total_price();
      }
    
      /* we should be able to fetch any internal info */
    float arch_win::get_height()
      {
        return height;
      }
    float arch_win::get_width()
      {
        return width;
      }
    float arch_win::get_price()
      {
        return price;
      }
    float arch_win::get_area()
      {
        return area;
      }
    float arch_win::get_totalprice()
      {
        return totalprice;
      }
    And a main file ie:
    Code:
    // my_project.cpp
    
    #include <iostream>
    #include <iomanip>
    #include <math.h>
    #include <ctype.h>
    
    #include "arch_win.h"
    
    using namespace std;
    
    int main()
    {
      float h=0, w=0, p=0; //set all float variables to 0
      char aORr = '\0';
      while ((!h&&!w)|| (h < w))
        {
          cout << "\nEnter Window Height: ";
          cout.flush();
          cin >> h;
          cout << "\nEnter Window Width: ";
          cout.flush();
          cin >> w;
          if (h < w)
      cout << "Height must be greater than or Equal to Width!!\n";
        }
      
      cout << "\nEnter Price Per Sq in: ";
      cout.flush();
      cin >> p;
      cout << "\nEnter A for Arch, R for Rectangle: ";
      cout.flush();
      cin >> aORr;
    
      /* call the constructor which match the input */
      arch_win AW(h, w, aORr);
      AW.set_price(p);
      //-------------------Formatting and Displaying Results----------------
      
      cout << "\n\nHeight" << setw(7) << "Width" << setw(13) << "PPSqinch";
      cout << setw(8) << "Area" << setw(15) << "Price\n";
      cout << "___________________________________________________\n";
      cout << setiosflags(ios::showpoint|ios::fixed) << setprecision(1)
        /* get the height from our class */
          << AW.get_height();
      cout << setw(8) << setprecision(1)
        /* get width from class */
          << AW.get_width();
      cout << setw(8) << setprecision(2) << "$" 
        /* get price from class */
          << AW.get_price();
      cout << setw(10) << setprecision(1)
        /* get area from class */
          << AW.get_area();
      cout << setw(8) << setprecision(2) << "$"
        /* get total price */
          << AW.get_totalprice();
      cout << endl; /* flush any output */
      /* halt user input */
      cin >> aORr;
      return 0;
    }
    Then compile it like:
    ~ > g++ -o project arch_win.cpp my_project.cpp

Similar Threads

  1. warning during compilation of a C program
    By explorer in forum Linux - Software, Applications & Programming
    Replies: 8
    Last Post: 08-12-2007, 03:14 PM
  2. Compilation of IDE's
    By SkyNet in forum Linux - Software, Applications & Programming
    Replies: 1
    Last Post: 05-01-2004, 09:26 PM
  3. Apache 2 compilation
    By vwgtiturbo in forum Linux - Software, Applications & Programming
    Replies: 0
    Last Post: 02-02-2004, 07:33 AM
  4. Apache compilation Problem--Pls help me
    By rhonneil in forum Linux - Software, Applications & Programming
    Replies: 2
    Last Post: 11-03-2003, 09:39 PM
  5. compilation (in general)
    By tetrahymenix in forum Linux - Software, Applications & Programming
    Replies: 4
    Last Post: 01-19-2003, 04:08 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •