Archived

This topic is now archived and is closed to further replies.

Dealing with strings

This topic is 5825 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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[].

Share this post


Link to post
Share on other sites
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 function

private:
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

Share this post


Link to post
Share on other sites
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

};

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
just always use namespace std and it will work. When using string don''t use any of those C functions like strcpy, strlen, etc... use the methods of the class. Anything that you don''t find as part of the class will be in algorithm. Oh and due to a mistake fstreams do take char* instead of strings in their constructors. Fortunately the string class has a method c_str() which will return one.

Share this post


Link to post
Share on other sites