Dealing with strings

Started by
9 comments, last by pdstatha 22 years, 3 months ago
Ok I'm mainly from a Java background, so at the moment I converting some of my Java programs into C++. I was unaware that C++ dealt with Strings in a different way, i.e. using character arrays. I have the following header which converted directly from java. Due to my unfamiliarity in dealing with strings in c++ there appear to be quite a few errors(27) which I can't iron out. Also I think I should have some pointers in there somewhere, but since Java hides pointers I don't yet understand them and quite frankly they scare me.
  
#include < iostream.h >
#include < string.h >

class Bank {
public:
	Bank();
	~Bank() {}
	int addCred(int addCred) {
		currCred += addCred;
        totalCred += addCred;
        return currCred;
	}
	int giveChange(int deductCred){
        currCred -= deductCred;
        totalCred -= deductCred;
        return currCred;
    }
	void updateCred(int cred) { currCred = cred; }
	int getCred() { return currCred; }
private:
	int currCred;
	int totalCred;
};

Bank::Bank() {
	currCred = 0;
    totalCred = 2000;
}

//Bank::~Bank() {}


class Display {
public:
	Display();
	~Display() {}
	void showDisplay(char displayText[50]) {
        text = displayText;
        cout << "DISPLAY:: " << text;
    }
	void clear() {
       text = "READY";
       showDisplay(text);
    }
private:
	char text[50];
};

Display::Display() {}

~Display::~Display() {}

class Keypad {
public:
	Keypad();
	~Keypad() {}
	char[] getCode() {
		cin.getline(code, 10, '\n');
		strupr(code);
		return code;
	}
private:
	char code[10];
};

Keypad::Keypad() {}

Keypad::~Keypad() {}

class Product {
public:
	Product(char itsName[50], int itsPrice);
	~Product() {}
	char[] getName() { return name; }
	int getPrice() { return price; }
private:
	char name[50];
	int price;
};

Product :: Product(char itsName[50], int itsPrice) {
	name = itsName;
    price = itsPrice;
}

Product::~Product() {}

class Tray {
public:
	Tray(char itsCode[10],Product trayItem);
	~Tray();
	char[] getCode() { return code;  }
	Product getProduct(){ return prod; }
	void dispense() { quantity--; }
	int getQuant() { return quantity;  }
private:
	int const limit = 20;
	int quantity;
	char code[10];
	Product prod;
};

Tray::Tray(char itsCode[10],Product trayItem) {
	code = itsCode;
    prod = trayItem;
    quantity = 20;
}

Tray::~Tray() {}    
Edited by - pdstatha on January 1, 2002 8:48:09 AM BTW the classes are for a vending machine Edited by - pdstatha on January 1, 2002 8:51:55 AM
Advertisement
Don''t use <string.h>. Use <string>. It provides a class that is far closer to what Java has for strings.

Also, don''t use <iostream.h>. Use <iostream>. The former is typically deprecated and regularly unmaintained. The latter is the standard name for the header and typically the most up-to-date.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
Oh right I didn''t know that there was a difference, still means I have errors tho, the most common one being

cannot convert from ''char []'' to ''char [50]''
There are no conversions to array types, although there are conversions to references or pointers to arrays

Anyone got any ideas?
Are you having error here?

Product :: Product(char itsName[50], int itsPrice)
{
name = itsName;
price = itsPrice;
}

In C++ ''name'' and ''itsName'' is a pointer, they have their own memeory allocated. so I think you can try

strcpy(name, itsName);

Hope Help.
get those char* and char[] out of your program and use the string class. It is very similar to the one in java. The differences are: it is named "string" not "String", it is passed by value, not pointer. You can change a string. It also has some other differences, such as while you can do this s3=s1+s2 you can''t do this s2=s1+i1 (where s means a string and i means an int). Most importantly stop using char* and char[].
Ok assuming that I'm going to stick with using char[], how the hell do I make it return an array, have a look at the following code.

    class Keypad {public:	Keypad();	~Keypad() {}	char getCode[]();//array element type cannot be functionprivate:	char code[10];};Keypad::Keypad() {}//Keypad::~Keypad() {}Keypad::getCode[]() {  //array element type cannot be function	cin.getline(code, 10, '\n');	strupr(code);	return code;}    


Edited by - pdstatha on January 1, 2002 1:14:29 PM
Return a pointer to the array.
Ok now i''m using < string >, so can someone please explain how to fix the following errors.

  class Display {public:	Display();	~Display() {}	void showDisplay(string displayText) {//syntax error : identifier ''string''		strcpy(text, displayText);              cout << "DISPLAY:: " << text;    }	void clear() {	   strcpy(text, "READY");       //text = "READY";       showDisplay(text);    }private:	string text;//syntax error : missing '';'' before identifier ''text''// ''string'' : missing storage-class or type specifiers};  
Right after your #includes, you''ll need to have the statement

using namespace std;

A namespace is like a package in Java, so this just says that you''re using the standard namespace.

strcpy works on character arrays only. When you use the string class, you should just use the assignment operator. So that line should read

text = displayText;

Hope that helps.
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
Yes, thankyou thankyou thankyou. Another question tho, if I were to use < iostream > instead of < iostream.h > what would be the equivalent of cout, because it''s not recognizing it.

This topic is closed to new replies.

Advertisement