Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a C++ Expert!

Join 244,308 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 821 people online right now. Registration is fast and FREE... Join Now!




char to string

 
Reply to this topicStart new topic

char to string

Silent Ace
8 Jan, 2009 - 10:06 AM
Post #1

New D.I.C Head
*

Joined: 7 Jan, 2009
Posts: 19


My Contributions
I would like to know how can i type in a char from a keyboard converting the char to string in an Object Oriented programming. I have a few questions down below.

CODE

#include "stdafx.h"
#include "iostream"
using namespace std;


class char_to_string{
private:
    char name[20];
public:

    char_to_string(){
        name[0];
    }
    
    //Should i declare the name like in the User-defined constructor
    // i know that i can enter a name down in the main()but that is not what i want
    char_to_string(char * name[50]){
    }
    
    //is this a correct method for setting a name???
       // do i need to copy this in a new object or simply convert it somehow
    char * setName(char * n[50]){

        name = new char[strlen(n[50])+1];
        strcpy(name, n);
    }
    
    //a method for entering a name from keyboard
    void print(){
               //0 arguments ????????
        cin >> setName(something in here right :));
        cout << "The name is: " << endl;
    }
};


void main(){
    
    char_to_string * p = new char_to_string();
    p->print();
    system("pause");
}





And of course the errors:

1>Char to string.cpp
1>c:\documents and settings\admin\my documents\visual studio 2008\projects\char to string\char to string\char to string.cpp(24) : error C2440: '=' : cannot convert from 'char *' to 'char [20]'
1> There are no conversions to array types, although there are conversions to references or pointers to arrays
1>c:\documents and settings\admin\my documents\visual studio 2008\projects\char to string\char to string\char to string.cpp(25) : error C2664: 'strcpy' : cannot convert parameter 2 from 'char *[]' to 'const char *'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\documents and settings\admin\my documents\visual studio 2008\projects\char to string\char to string\char to string.cpp(31) : error C2109: subscript requires array or pointer type


Can some explain to me the meaning of cannot convert parameter 2 from 'char *[]' to 'const char *'????



Confused ohmy.gif !!!!!!!

User is offlineProfile CardPM
+Quote Post


Bench
RE: Char To String
8 Jan, 2009 - 11:43 AM
Post #2

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 827



Thanked: 57 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
char* [] is an array of character pointers. Perhaps you intended to simply use char* or char[] instead? (I can't see any reason why you would need an array of pointers)

Arrays in C++ will perform automatically cast when assigned to a pointer variable, which allows pointers to mix with arrays easily.

However, being that this is C++, I'd strongly suggest you use the standard string type instead.

This post has been edited by Bench: 8 Jan, 2009 - 11:45 AM
User is offlineProfile CardPM
+Quote Post

matthew180
RE: Char To String
8 Jan, 2009 - 12:05 PM
Post #3

D.I.C Head
Group Icon

Joined: 7 Jan, 2009
Posts: 191



Thanked: 31 times
Dream Kudos: 25
My Contributions
Is there some reason you don't just use scanf?

CODE

#define MAX_INPUT_LEN 50

char user_input[MAX_INPUT_LEN + 1];

printf("Input: ");
scanf("%50s", user_input);

printf("\n%s\n", user_input);



Matthew

User is offlineProfile CardPM
+Quote Post

kapax
RE: Char To String
8 Jan, 2009 - 01:01 PM
Post #4

D.I.C Head
**

Joined: 2 Jul, 2008
Posts: 54



Thanked: 3 times
My Contributions
Look, man, I am really sorry, but I did not like your code, so I wrote one short OOP style code myself:

CODE

#include <iostream>
#include <string>

using namespace std;


class charToStringConverter {
private:
    char *name;
    string *str;

public:

    charToStringConverter() {
        name = new char[20];
    }
  
    void getInput() {
        cout << "Write your name here: ";
        cin >> name;
    }
    
    void convertInput() {
        str = new string(name);
    }
    
    void print() {
        cout << "The name you entered is: " << *str;
    }
};


int main(){
  
    charToStringConverter * p = new charToStringConverter();
    p->getInput();
    p->convertInput();
    p->print();
    
    return 0;
}



Try to understand this and you can always ask if you need anything else.

As for the naming, I would advise you to write class names as objects because classes represent objects. I really like Java naming convention, so I use it almost everywhere.

This post has been edited by kapax: 8 Jan, 2009 - 01:04 PM
User is offlineProfile CardPM
+Quote Post

Bench
RE: Char To String
9 Jan, 2009 - 02:47 PM
Post #5

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 827



Thanked: 57 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
QUOTE(matthew180 @ 8 Jan, 2009 - 08:05 PM) *

Is there some reason you don't just use scanf?

CODE

#define MAX_INPUT_LEN 50

char user_input[MAX_INPUT_LEN + 1];

printf("Input: ");
scanf("%50s", user_input);

printf("\n%s\n", user_input);



Matthew

The likely reason is that he is programming in C++, and not C. scanf is very poor style in C++, since all of its functionality is replaced by far more reliable and robust constructs from the standard stream libraries.
User is offlineProfile CardPM
+Quote Post

Hyper
RE: Char To String
9 Jan, 2009 - 11:17 PM
Post #6

Banned
*****

Joined: 15 Oct, 2008
Posts: 2,129



Thanked: 87 times
Dream Kudos: 425
My Contributions
QUOTE(Bench @ 8 Jan, 2009 - 11:43 AM) *
(I can't see any reason why you would need an array of pointers)


I'll assume you mean in general (not his specific case) - This is why:
Array of (char) Pointers
char *GameItem[] = {
"Broken Sword",
"Cracked Sword",
"Short Sword",
"Sword",
"Long Sword",

"Broken Bow",
"Cracked Bow",
"Short Bow",
"Bow",
"Long Bow"
};


Now I can simply go:
printf("You have found a %s", GameItem[ItemID]);
Or countless other examples.

This post has been edited by Hyper: 9 Jan, 2009 - 11:18 PM
User is offlineProfile CardPM
+Quote Post

Bench
RE: Char To String
10 Jan, 2009 - 03:38 AM
Post #7

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 827



Thanked: 57 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
QUOTE(Hyper @ 10 Jan, 2009 - 07:17 AM) *

QUOTE(Bench @ 8 Jan, 2009 - 11:43 AM) *
(I can't see any reason why you would need an array of pointers)


I'll assume you mean in general (not his specific case) - This is why:
No, my reply was very specifically directed to the problem posed by the OP. However, even if we're talking in more general terms I would have to disagree on your rationale behind using an array of pointer-to-char in C++ (The OP was asking about C++).
Use of char[] is considered "bad" - the standard string type is far safer, more idiomatic, and more flexible. The use of arrays of any type are also generally considered "bad", since STL or boost containers are usually better for largely the same reasons. There are situations when raw arrays and pointers are unavoidable, but those circumstances are generally exceptional or can have their impact strictly limited to a small part of a program where they can't do much damage.

Here's one situation where arrays are currently unavoidable - as an initialiser for an STL container object (At least until C++0x when array initialisers will be granted more power)
cpp
const char* init[] =     "Broken Sword",
"Cracked Sword",
"Short Sword",
"Sword",
"Long Sword" };
std::vector< std::string > GameItem( init, init + sizeof(init)/sizeof(init[0]) );
In general, it would be hoped that init would be ignored throughout the rest of the program, on the grounds that GameItem caters for its functionality, at no additional cost, but removes any concerns about how the data is represented internally. (Though depending on exactly what GameItem is to be used for, a different STL container might be better)


There are no "never" or "always" rules in C++, and there will inevitably be some situations where char* [] is the 'best' solution to a problem (such as backward compatibility to legacy code), but as a matter of course, its best to avoid whenever possible.
User is offlineProfile CardPM
+Quote Post

Hyper
RE: Char To String
10 Jan, 2009 - 04:00 AM
Post #8

Banned
*****

Joined: 15 Oct, 2008
Posts: 2,129



Thanked: 87 times
Dream Kudos: 425
My Contributions
If I cared that much I'd just code in Assembly all the time.
Can't get much more direct than that.

EDIT: Thanks for the thoughtful reply though. Appreciated *

This post has been edited by Hyper: 10 Jan, 2009 - 04:00 AM
User is offlineProfile CardPM
+Quote Post

rachel roofy
RE: Char To String
10 Jan, 2009 - 05:47 AM
Post #9

New D.I.C Head
*

Joined: 10 Jan, 2009
Posts: 1

QUOTE(kapax @ 8 Jan, 2009 - 01:01 PM) *

Look, man, I am really sorry, but I did not like your code, so I wrote one short OOP style code myself:

CODE

#include <iostream>
#include <string>

using namespace std;


class charToStringConverter {
private:
    char *name;
    string *str;

public:

    charToStringConverter() {
        name = new char[20];
    }
  
    void getInput() {
        cout << "Write your name here: ";
        cin >> name;
    }
    
    void convertInput() {
        str = new string(name);
    }
    
    void print() {
        cout << "The name you entered is: " << *str;
    }
};


int main(){
  
    charToStringConverter * p = new charToStringConverter();
    p->getInput();
    p->convertInput();
    p->print();
    
    return 0;
}



Try to understand this and you can always ask if you need anything else.

As for the naming, I would advise you to write class names as objects because classes represent objects. I really like Java naming convention, so I use it almost everywhere.


User is offlineProfile CardPM
+Quote Post

Silent Ace
RE: Char To String
11 Jan, 2009 - 12:10 AM
Post #10

New D.I.C Head
*

Joined: 7 Jan, 2009
Posts: 19


My Contributions
Wow, thanks for the replays it was helpful wink2.gif
User is offlineProfile CardPM
+Quote Post

Hyper
RE: Char To String
11 Jan, 2009 - 08:29 AM
Post #11

Banned
*****

Joined: 15 Oct, 2008
Posts: 2,129



Thanked: 87 times
Dream Kudos: 425
My Contributions
QUOTE(Silent Ace @ 11 Jan, 2009 - 12:10 AM) *

Wow, thanks for the replays it was helpful wink2.gif


Replies *, and welcome.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 7/4/09 07:01PM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month