2 Replies - 132 Views - Last Post: 15 July 2012 - 06:23 AM Rate Topic: -----

#1 adityasingh95  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 14-July 12

error in output

Posted 14 July 2012 - 11:08 PM

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

Is This A Good Question/Topic? 0
  • +

Replies To: error in output

#2 healix  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 29-May 11

Re: error in output

Posted 14 July 2012 - 11:28 PM

comment out the cout << type <<endl; and see what happens.
Was This Post Helpful? 0
  • +
  • -

#3 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 844
  • View blog
  • Posts: 2,334
  • Joined: 20-August 07

Re: error in output

Posted 15 July 2012 - 06:23 AM

string data in C and C++ is terminated with a '\0' nul character, so the string you're inputting "53e10" is 6 characters long (Yes, six characters, not five!), but your buffer is only big enough to support only 5 characters
     char Code[5];



Your program is suffering from undefined behaviour because your input overuns the end of your char[] buffer, So pretty much anything could happen; That nul character is being dumped into a bit of memory which doesn't belong to the buffer. its most likely the reason that you're seeing output which you don't expect
(It could also reasonably cause your program to crash, or if your'e really unlucky it could cause your program to work as you expect and you might never see the error).

If your book is teaching you to write C++ using strcpy/strcmp (which are part of the C language), and teaching you to treat strings as arrays-of-char then I'm afraid it's not a good omen for the book; problems like this are one of the reasons that C++ programmers generally do not use char[] for strings.
C++ has got a built-in string type which resides in the <string> header, and effectively replaces the C string.h library. You should prefer to use that instead of using old C functions.

You'll almost certainly have an easier time if you replace your char[] with string, since a string resizes itself at will and also allows you to use the ordinary operators such as = for copy-assignment and == for comparisons

I created a thread a while ago which discusses the basics of C++ strings; have a look here:
http://www.dreaminco...2209-c-strings/



Edit: Also, void main() is illegal in C++ - the main() function must return an int. Read here for more info: http://users.aber.ac...uj/voidmain.cgi

This post has been edited by Bench: 15 July 2012 - 06:48 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1