A Couple Errors

This is a discussion on A Couple Errors within the C++ Programming forums, part of the General Programming Boards category; Hey guys, I've got a few programs that aren't running for different reasons. Program 1: Code: //Debug 6-3 #include<iostream> #include<conio.h> ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    12

    A Couple Errors

    Hey guys, I've got a few programs that aren't running for different reasons.
    Program 1:
    Code:
    //Debug 6-3
    #include<iostream>
    #include<conio.h>
    using namespace std;
    
    class ClubMember
    {
      public:
        static int count;
        char name[20];
        ClubMember(char* name);
        ~ClubMember();
     };
    int ClubMember::count = 0;
    ClubMember::ClubMember(char* name)
     {
        ++count;
        strcpy(name,name);
        cout<<"ClubMember #"<<count<<" created: ";
        cout<<name<<" has joined the club"<<endl;
     }
    ClubMember::~ClubMember()
      {
        --count;
        cout<<name<<" has left the club"<<endl;
        cout<<count<<" ClubMembers left"<<endl;
      }
    void main()
    {
      {
        cout<<"This program shows five club members"<<endl<<"joining, then leaving, the club"<<endl;
    	
        ClubMember club[5] = {ClubMember("Jim"),ClubMember("Tom"),ClubMember("Julie"),ClubMember("Jane"),ClubMember("Kate")};
      }
      std::cin.clear();
      std::cin.ignore();
      getchar();
    }
    It runs, and starts with the line This program shows five club member joining, then leaving, the club but then it freezes and the computer tries to debug the program itself, but then even that fails.

    Program 2:
    Code:
    //Debug 6-2
    #include<iostream>
    #include<conio.h>
    using namespace std;
    
    class BirthdayCake
    {
      public:
    	  //made entire class public
        int orderNumber;
        char cakeFlavor[20];
        char frostingFlavor[20];
        int numCandles;
        BirthdayCake(int num, char cake[], char frost[], int numCandles);
    	//got rid of intialization of cake, frost and numCandles. Void added.
        void displayBirthdayCake();
     };
    BirthdayCake::BirthdayCake(int num, char cake[] = "white", char frost[] = "vanilla", int candles = 1)
     {
        orderNumber = num;
        cake = cakeFlavor;
        frost = frostingFlavor;
        numCandles = candles;
     }
    void BirthdayCake::displayBirthdayCake()
     {
        cout<<"Order #"<<orderNumber<<" "<<cakeFlavor<<" cake with "<<
        	frostingFlavor<<" frosting and "<<numCandles;
        if(numCandles == 1)
          cout<<" candle";
        else cout<<" candles";
        cout<<endl;
     }
    void main()
    {
      cout<<"Cakes are white with vanilla frosting and 1 candle unless otherwise indicated. "<<endl;
      BirthdayCake a(111,"chocolate","chocolate",8);
      BirthdayCake b(222,"yellow", "chocolate");
      BirthdayCake c(333,"banana");
      BirthdayCake d(444);
      a.displayBirthdayCake();
      b.displayBirthdayCake();
      c.displayBirthdayCake();
      d.displayBirthdayCake(); 
    
      std::cin.clear();
      std::cin.ignore();
      getchar();
    }
    Works fine, except instead of showing the cake flavor or frosting flavor it just shows a lot of piping.

    Program 3:
    Code:
    //Chapter 6 Debug 1
    #include<iostream>
    #include<conio.h>
    using namespace std;
    
    class Auto
    {
      public:
        int autoId;
        char mechanicName[20];
        double fee;
        Auto(char name[], int id, double amt);
        //switched above line with Auto1 function, above line initialized variables and added void
    	//also switched so char name[] was first
        void showAuto();
     };
    
    Auto::Auto(char name[], int id = 999, double amt = 25.00)  // Valid, name (no default) comes before all others with default 
     {
       autoId = id;
       strcpy(mechanicName, name);		//this block switches the names passing to and from the functions
       fee = amt;
     }
    
    void Auto::showAuto()
     {
       cout<<"Auto #"<<autoId<<" worked on by "<<mechanicName<<" Amount due $"<<fee<<endl;	//this function shows the information for each car
     }
    void main()
    {
      cout<<"Mike's Service "<<endl<<"Autos Worked on Today"<<endl<<endl;
      cout<<"Mike works on most cars. Occasionally he assigns a job to another mechanic"<<endl;			
      cout<<"Minimum charge $25"<<endl<<endl;
      //the above 3 lines give the introduction to the program
      Auto car1(""), car2("", 321), car3("Amy", 456), car4("Jeremy", 567, 149);		//provides the data for the function
      car1.showAuto();
      car2.showAuto();
      car3.showAuto();
      car4.showAuto();
      //the above 4 lines call the showAuto() function for each car
      std::cin.clear();
      std::cin.ignore();
      getchar();
    }
    Runs, but it's supposed to put the name "Mike" for the mechanic when it's not already given and it just gives a blank. Originally I had it so that char name[] wasn't first, it was in the middle, but then I didn't have the empty quotations marks in void main() when it's giving the parameters for each car.

    Program 4:
    Code:
    // Debug 6-4
    #include<iostream>
    #include<conio.h>
    using namespace std;
    
    class Dress
     {
      private:
       char material[20];
       int size;
       char style[10];
       double price;
    
      public:
        Dress(int sz, char mtrl[], char stl[]);
        void displayDress();
      };
    Dress::Dress(int sz, char mtrl[] = "cotton", char stl[] = "daytime")
    {
      strcpy(material,mtrl);
      sz = size;
      strcpy(style,stl);
      price = 29.99;
      if(strcmp(material,"silk") == 0)
        price += 20.00;
      if(strcmp(style, "evening")==0)
        price += 40.00;
     }
    
    void Dress::displayDress()
     {
       cout<<"A size "<<size<<" dress made of "<<material<<" and suitable for "<<
       	style<<" wear costs $"<<price<<endl;
      }
    
    void main()
      {
         cout<<"Dress sale! $29.99. Only $20 more for"
    		 <<"sil. Only $40 more for evening designs."<<endl;
         Dress dressA(), dressB("wool"), dressC(14,"silk","evening"),
         	 dressD(4,"denim","casual"),dressE(8,"silk","business");
         dressA.displayDress();
         dressB.displayDress();
         dressC.displayDress();
         dressD.displayDress();
         dressE.displayDress();
         getch();
      }
    "wool" by itself causing an error (no constructor available) and dressA.displayDress() is getting and error (under dressA) and it's saying Dress dressA() must have a class type.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,370
    O_o

    I remember seeing these problems repeated over and over before... do I know you?

    *searching*

    Yep. And the problems you've had help fixing before are some of these same ones.

    So, yea, why don't you actually study a little and try to learn from your mistakes?

    Soma

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    Ok, thanks. But you know, instead of being a douche about it you could have just referred me there. I'm trying to learn from my mistakes, sorry I'm not fantastic as programming as the people on here. Thanks though.

    Oh, and I didn't realize that I had already posted the car one.
    Last edited by adam.morin; 03-10-2011 at 08:18 PM.

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,370
    True, I could have done that, but if you can't bother to search for your own bloody posts and remember what the regulars here took the time to teach you I see no reason that I should bother not being a douche.

    Soma

  5. #5
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Code:
    //Debug 6-3
    #include<iostream>
    #include<conio.h>
    using namespace std;
    
    class ClubMember
    {
      public:
        static int count;
        char name[20];
        ClubMember(char* name);
        ~ClubMember();
     };
    int ClubMember::count = 0;
    ClubMember::ClubMember(char* name)
     {
        ++count;
        strcpy(name,name);
        cout<<"ClubMember #"<<count<<" created: ";
        cout<<name<<" has joined the club"<<endl;
     }
    ClubMember::~ClubMember()
      {
        --count;
        cout<<name<<" has left the club"<<endl;
        cout<<count<<" ClubMembers left"<<endl;
      }
    void main()
    {
      {
        cout<<"This program shows five club members"<<endl<<"joining, then leaving, the club"<<endl;
    	
        ClubMember club[5] = {ClubMember("Jim"),ClubMember("Tom"),ClubMember("Julie"),ClubMember("Jane"),ClubMember("Kate")};
      }
      std::cin.clear();
      std::cin.ignore();
      getchar();
    }
    1. Write count++ and count--, instead of ++count and --count. I hope you know the difference between post and pre increment.....
    2. In strcpy(name,name), use strcpy(this.name,name).
    Code:
    //Debug 6-2
    #include<iostream>
    #include<conio.h>
    using namespace std;
    
    class BirthdayCake
    {
      public:
    	  //made entire class public
        int orderNumber;
        char cakeFlavor[20];
        char frostingFlavor[20];
        int numCandles;
        BirthdayCake(int num, char cake[], char frost[], int numCandles);
    	//got rid of intialization of cake, frost and numCandles. Void added.
        void displayBirthdayCake();
     };
    BirthdayCake::BirthdayCake(int num, char cake[] = "white", char frost[] = "vanilla", int candles = 1)
     {
        orderNumber = num;
        cake = cakeFlavor;
        frost = frostingFlavor;
        numCandles = candles;
     }
    void BirthdayCake::displayBirthdayCake()
     {
        cout<<"Order #"<<orderNumber<<" "<<cakeFlavor<<" cake with "<<
        	frostingFlavor<<" frosting and "<<numCandles;
        if(numCandles == 1)
          cout<<" candle";
        else cout<<" candles";
        cout<<endl;
     }
    void main()
    {
      cout<<"Cakes are white with vanilla frosting and 1 candle unless otherwise indicated. "<<endl;
      BirthdayCake a(111,"chocolate","chocolate",8);
      BirthdayCake b(222,"yellow", "chocolate");
      BirthdayCake c(333,"banana");
      BirthdayCake d(444);
      a.displayBirthdayCake();
      b.displayBirthdayCake();
      c.displayBirthdayCake();
      d.displayBirthdayCake(); 
    
      std::cin.clear();
      std::cin.ignore();
      getchar();
    }
    1. In your parametrized constructor, write cakeFlavour=cake and FrostingFlavour=Frost..
    So, in the third program, clear your memory buffer with cin.ignore...
    And for the fourth program, must define default constructor as you didn't......

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,370
    Listen Lucky, that guy has literally had help with these four exercises before. I don't know why he doesn't remember or didn't keep the fixes the internet gave him, but you are probably wasting your time in trying to help him.

    Oh, in this case, using prefix or postfix operators is entirely a matter of programmer preference; here there is no difference between them. (I'd be surprised if the compiler didn't generate the same binary.)

    Soma

  7. #7
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Quote Originally Posted by phantomotap View Post
    Oh, in this case, using prefix or postfix operators is entirely a matter of programmer preference; here there is no difference between them.
    Yup... You are right... As it's standalone statement... I didn't concentrate over that line too much...

  8. #8
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    What does "this." change? When I try it, it's just creating more problems.
    I've tried changing it to cakeFlavor = cake etc., and it causes an error, saying cakeFlavor must be modifiable.
    I noticed on the other board with the car program that's here, I didn't quite understand what was going on. I get that the function is calling for 3 parameters, but how would I write it in the main function so that the default appears (short of actually filling in the default). The following code was what I had originally.
    Code:
    // Debug 6-4
    #include<iostream>
    #include<conio.h>
    using namespace std;
    
    class Dress
     {
      private:
       char material[20];
       int size;
       char style[10];
       double price;
    
      public:
        Dress(char mtrl[], int sz, char stl[]);
        void displayDress();
      };
    Dress::Dress(char mtrl[] = "cotton", int sz, char stl[] = "daytime")
    {
      strcpy(material,mtrl);
      sz = size;
      strcpy(style,stl);
      price = 29.99;
      if(strcmp(material,"silk") == 0)
        price += 20.00;
      if(strcmp(style, "evening")==0)
        price += 40.00;
     }
    
    void Dress::displayDress()
     {
       cout<<"A size "<<size<<" dress made of "<<material<<" and suitable for "<<
       	style<<" wear costs $"<<price<<endl;
      }
    
    void main()
      {
         cout<<"Dress sale! $29.99. Only $20 more for"
    		 <<"sil. Only $40 more for evening designs."<<endl;
         Dress dressA(), dressB("wool"), dressC("silk",14,"evening"),
         	 dressD("denim",4,"casual"),dressE("silk",8,"business");
         dressA.displayDress();
         dressB.displayDress();
         dressC.displayDress();
         dressD.displayDress();
         dressE.displayDress();
         getch();
      }
    I do know the difference between pre and post increment. I do appreciate your help on this, I'm trying to learn this, it's just not sinking in as well as I would hope.

    Oh wait, that's not the one I wanted to post.
    Code:
    #include<iostream>
    #include<conio.h>
    using namespace std;
    
    class Auto
    {
      public:
        int autoId;
        char mechanicName[20];
        double fee;
        Auto(char name[], int id, double amt);
        //switched above line with Auto1 function, above line initialized variables and added void
    	//also switched so char name[] was first
        void showAuto();
     };
    
    Auto::Auto(char name[], int id = 999, double amt = 25.00)  // Valid, name (no default) comes before all others with default 
     {
       autoId = id;
       strcpy(mechanicName, name);		//this block switches the names passing to and from the functions
       fee = amt;
     }
    
    void Auto::showAuto()
     {
       cout<<"Auto #"<<autoId<<" worked on by "<<mechanicName<<" Amount due $"<<fee<<endl;	//this function shows the information for each car
     }
    void main()
    {
      cout<<"Mike's Service "<<endl<<"Autos Worked on Today"<<endl<<endl;
      cout<<"Mike works on most cars. Occasionally he assigns a job to another mechanic"<<endl;			
      cout<<"Minimum charge $25"<<endl<<endl;
      //the above 3 lines give the introduction to the program
      Auto car1(), car2(321), car3("Amy", 456), car4("Jeremy", 567, 149);		//provides the data for the function
      car1.showAuto();
      car2.showAuto();
      car3.showAuto();
      car4.showAuto();
      //the above 4 lines call the showAuto() function for each car
      std::cin.clear();
      std::cin.ignore();
      getchar();
    Sorry.
    Last edited by adam.morin; 03-11-2011 at 12:33 AM.

  9. #9
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    1. Make a default constructor in your class as you are doing Dress dressA().
    2. Dress dressB("wool") shouldn't work too.... as second parameter has no default values...
    3. Default values should be into the extreme right of the parameter list as far as i know..
    4.
    Code:
    sz = size;
    I don't understand why are you doing this?
    You need to initialize the size with the value sz, but in this case you are assigning sz with size which is garbage.....
    Also, remind the assignment operator (=) rule. It always, puts the value at it's right to the left....
    5. And previously i used this operator because you were passing the variable of same name as it was in your private member list... So this points to the private variable of the class...
    6. And don't be afraid of errors, but try to understand, what errors are telling you to do..

    Check for the same above mentioned mistakes in your re-posted code....

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Mr. 777, do you understand what pointers are? This is the second time you've made a mistake involving pointers. First modf, then this, which also is a pointer.
    But before you go any further adam.morin, there are things you must do.
    Change void main to int main.
    Change all char to std::string, replace strcpy with normal assignment.
    Make it work with standard C++, and not C!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Mr. 777, do you understand what pointers are? This is the second time you've made a mistake involving pointers. First modf, then this, which also is a pointer.
    Yes, "this" is a pointer... So?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ten Errors
    By AverageSoftware in forum Contests Board
    Replies: 0
    Last Post: 07-20-2007, 11:50 AM
  2. Header File Errors...
    By Junior89 in forum C++ Programming
    Replies: 5
    Last Post: 07-08-2007, 01:28 AM
  3. Help with a couple compiler errors
    By s_ny33 in forum C Programming
    Replies: 1
    Last Post: 09-15-2005, 09:46 PM
  4. Errors when including winsock2.h
    By skiingwiz in forum Windows Programming
    Replies: 2
    Last Post: 12-27-2002, 07:32 PM
  5. Replies: 5
    Last Post: 11-13-2001, 04:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21