0

i wrote this program as an answer to a question given in my book but the output always displays incorrectly

#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <stdio.h>

class Clothing
    {
    //private data members
    char Code[5];
    char Type[20];
    int Size;
    char Material[20];
    float Price;

    public:
    //constructor

    Clothing()
        {
        strcpy(Code,"NOT ASSIGNED");
        strcpy(Type,"NOT ASSIGNED");
        strcpy(Material,"NOT ASSIGNED");
        Size=0;
        Price=0;
        }
    //price calculation
    void Calc_Price()
        {
        if (strcmp(Material,"COTTON")==0 || strcmp(Material,"cotton")==0 ||strcmp(Material,"Cotton")==0)
            {
            if (strcmp(Type,"TROUSER")==0 || strcmp(Type,"trouser")==0 ||strcmp(Type,"Trouser")==0)
                {Price=1500;}
            else if (strcmp(Type,"SHIRT")==0 || strcmp(Type,"shirt")==0 ||strcmp(Type,"Shirt")==0)
                {Price=1200;}
            }
        else if (strcmp(Material,"COTTON")!=0 || strcmp(Material,"cotton")!=0 ||strcmp(Material,"Cotton")!=0)
            {if (strcmp(Type,"TROUSER")==0 || strcmp(Type,"trouser")==0 ||strcmp(Type,"Trouser")==0)
                {Price=1125;}
            else if (strcmp(Type,"SHIRT")==0 || strcmp(Type,"shirt")==0 ||strcmp(Type,"Shirt")==0)
                {Price=900;}
            }
        }

    //inputing values
    void Enter()
        {
        cin>>Code>>Type>>Size>>Material;
        Calc_Price();
        }
    //displaying values
    void Show()
        {clrscr();
        cout<<Code<<endl;
        cout<<Type<<endl;
        cout<<Size<<endl;
        cout<<Material<<endl;
        cout<<"Rs."<<Price;
        }
};

void main()
    {
    clrscr();
    Clothing c;   //object created
    c.Enter();
    clrscr();
    c.Show();
    getch();
    }

say the input is

code:53e10
type:trouser
size:42
material:cotton

the output should display

53e10
trouser
42
cotton
Rs.1500

but instead it displays

53e10trouser
trouser
42
cotton
Rs.1500

3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by rubberman
0

Look up again how to use a character array as a string. You are missing a couple key pieces of information about a string's length and termination character.

0

As WaltP said. In your Clothing constructor, you are copying too much data into the Code data member, corrupting the object. IE, this is part of the problem:

    Clothing()
    {
        strcpy(Code,"NOT ASSIGNED"); // This will corrupt the data structure.
        strcpy(Type,"NOT ASSIGNED");
        strcpy(Material,"NOT ASSIGNED");
        Size=0;
        Price=0;
    }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.