We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,956 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

string to char problem

Hi, im pretty new to c++, and im stuck on this problem, i need to assign a letter from and array of stings to an array of chars and i just cannot figure it out.
any help is appreciated.
I tried both and both give me errors:
strcpy (newChars[j],alphabet[i]);
newChars[j]=alphabet[i];

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

using namespace std;

int main()
{

    char* newChars = new char[100];

    string alphabet[52] = { "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};

    int i=0;
    for (int j=0;j<52;j++){
        cout << j << "======" <<  alphabet[j]<<"\n"<< endl;
        //std::strcpy (newChars[j],alphabet[i]);
        newChars[j]=alphabet[i];
    }
}
3
Contributors
7
Replies
1 Day
Discussion Span
7 Months Ago
Last Updated
8
Views
Unidennn
Light Poster
27 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

You could use the c_str() member function to get the character array represented by the string. Remember to delete the newChars array before exiting the program, and also to use the C++ style header <string> , not the C-style version <string.h> .

    newChars[j]= *alphabet[i].c_str();
WolfPack
Postaholic
Moderator
2,062 posts since Jun 2005
Reputation Points: 572
Solved Threads: 119
Skill Endorsements: 11

Just noticed, there seems to be a small typo.
It should be
newChars[j]= *alphabet[j].c_str();

WolfPack
Postaholic
Moderator
2,062 posts since Jun 2005
Reputation Points: 572
Solved Threads: 119
Skill Endorsements: 11

strcpy() is wrong because it takes two character arrays as arguments, not two single characters

Your second example on line 17 is correct way to do it. But you didn't really say what the expected output should be so I can't say whether its right or wrong.

Ancient Dragon
Achieved Level 70
Team Colleague
32,124 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,575
Skill Endorsements: 69

thank you very much, that helped alot :)

Unidennn
Light Poster
27 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

newChars[j]= *alphabet[j].c_str();

Too complicated, you don't need to use c_str()

Ancient Dragon
Achieved Level 70
Team Colleague
32,124 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,575
Skill Endorsements: 69

Yeah I guess using c_str() is overkill. Just posted what I thought of at first.
This should be simpler.

        newChars[j]=alphabet[j][0];
WolfPack
Postaholic
Moderator
2,062 posts since Jun 2005
Reputation Points: 572
Solved Threads: 119
Skill Endorsements: 11

why the op is using an array of std::strings is beyond me, why not just a single string and index it as normal

std::string alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 

newChars[j]=alphabet[j];
Ancient Dragon
Achieved Level 70
Team Colleague
32,124 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,575
Skill Endorsements: 69

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0823 seconds using 2.71MB