strcpy string to char array

I want to change a string into an array of characters. After looking around online strcpy came up the most, but I can't get it to work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class PlayerColors: public Key{
public:
   //PlayerColors(){};
   PlayerColors(){
      pGuess = new char[kSize];
   };
   ~PlayerColors(){
      delete pGuess;
   };
   
   virtual void getCode();
   void playersGuess();
   
private:
   char *pGuess;
   
   };

//member function

  void PlayerColors::playersGuess(){
   string guess;
   
   cout << "What is your guess?: ";
   cin >> guess;
   
   strcpy(pGuess,guess.c_str(), kSize);
   
   for(int i = 0; i<kSize; i++){
      cout << pGuess[i];
   }
}

}
Last edited on
A string is an array of characters. What's wrong with just using a string? It is not a very heavy object in memory:

1
2
3
4
5
6
7
class PlayerColors: public Key {
    ...

private:
    string guess;

    };

Now you don't have to worry about managing memory for an array of characters.

BTW, if you insist, strcpy() takes only two arguments. Your compile errors should be telling you this.

You might be thinking of strncpy(). However, strncpy() takes a little extra care to use -- it might not null-terminate your string.
http://www.cplusplus.com/reference/cstring/strncpy/

Good luck!
I need to use an array of characters because i'm comparing it to another array of characters. Anyway I tried out both strcpy and strncpy and they both added a bunch of stuff on to the end of the array. How do I stop this from happening?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void PlayerColors::playersGuess(){
   char guess[kSize];
   
   cout << "What is your guess?: ";
   cin >> guess;
   
   //strcpy(pGuess,guess);
   strncpy (pGuess,guess, kSize);
   pGuess[kSize+1] = '\0';
   
   for(int i = 0; i<kSize; i++){
      cout << pGuess[i];
   }
}
Last edited on
well now i get a couple hundred upside down question marks after the array is output, so all the correct characters are output, but are followed by the questions marks
closed account (2UD8vCM9)
use strings instead. You can still compare using strings in the same way you would a char array. Only difference is it's easier to use strings.

1
2
3
4
5
6
7
8
	string guess;
	cout << "What is your guess?: ";
	getline(cin,guess);
	cin.clear();
	for (int i=0;i< guess.size(); i++)
	{
		   cout << guess[i];
	}


Don't forget to #include <string>
Last edited on
it still outputs a question mark at the end
closed account (2UD8vCM9)
Then you must have not used the code I pasted? I tested it and no question marks..
I still need it to be an array of character to compare to another array of characters which is filled with random characters. Strings are so messy and difficult...
closed account (2UD8vCM9)
You do realize comparing individual characters for strings is exactly the same as a char array?

Ex.
1
2
3
string HelloWorld = "HelloWorld";
HelloWorld[0]='J';
cout << HelloWorld << endl; // Displays JelloWorld 
Last edited on
"Then you must have not used the code I pasted? I tested it and no question marks.."

I did use the code you posted… I wonder why mine is spitting out question marks.
closed account (2UD8vCM9)
Can you copy/paste the exact code you used so I can test it?
I suppose the string is a good way of doing it, theres just that stupid upside down question mark at the end. This is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include "Key.h"
#include "Colors.h"
#include "PlayerColors.h"
#include "functions.h"

using namespace std;

int main(){
string guess1;
	cout << "What is your guess?: ";
	getline(cin,guess1);
	cin.clear();
	for (int i=0;i<= guess1.size(); i++)
	{
      cout << guess1[i];
	}
   cout <<endl;
   
    return 0;
}



yet it gives me XXXX? (but the ? is upside down)
closed account (2UD8vCM9)
Sorry in the for loop change it from
i<=guess1.size() to
i<guess1.size()
ahhhhh the equal sign was the culprit!
well the code is broken once again. It won't output guess. At first the program wasn't waiting for user input so I added cin.ignore(), but then it wasn't running through the loop. I moved the cin.ignore() to before the user inputs things and now it runs through the loop, but still nothing is output to the screen

1
2
3
4
5
6
7
8
9
10
11
12
13
void PlayerColors::playersGuess(){
   string guess;
   
   cin.ignore();
   cout << "What is your guess?: ";
   getline(cin, guess);
   cin.clear();
 
   cout << guess;
   for(int i = 0; i<guess.size(); i++){
      cout << guess[i];
   }
}
closed account (2UD8vCM9)
Try changing the cin.ignore(); to a cin.clear();
without the cin.ignore() the program will not wait for user input :(
closed account (2UD8vCM9)
Only thing I can think of is you have a cin occurring before this function is called that is messing it up. Do you have a cin occurring before this function is called?
yup
closed account (2UD8vCM9)
post code please for the cin occurring right before this function is called
Last edited on
Topic archived. No new replies allowed.