Jump to content

C++ Char Help


Recommended Posts

So i seem to be terrible with char and strings... can anyone lend me a hand?

 


#include <iostream>
#include <string>

using namespace std;

char repeatString(char what[10], int times);

int main(){

cout << repeatString('Ok',10);

return(0);

}//end main

char repeatString(char what[10], int times){

for(int i=0; i<times; i++){

	cout << what << " ";

	}//end for

}//end repeatString

 

should be out putting: "Ok Ok etc"; ten times

 

instead I get:

 

Joshs-Mac-2:week7 MacBook$ g++ mid.cpp -o mid.out

mid.cpp:10:23: warning: multi-character character constant

mid.cpp: In function ‘int main()’:

mid.cpp:10: error: invalid conversion from ‘int’ to ‘char*’

mid.cpp:10: error:  initializing argument 1 of ‘char repeatString(char*, int)’

 

 

Thoughts?

Link to comment
https://forums.phpfreaks.com/topic/177207-c-char-help/
Share on other sites

In C++ (and C) ' and " are not the same.

 

 

'' means a character constant and "" is a string.

 

 

A string is basically an array of null terminated characters, and a character is a 1 byte chunk of memory.

 

In other words, in hex each thing would look as follows:

 

'a' 61

"a" 61 0

 

 

 

So, you should be using "Ok".

 

Also, you might want to make it a pointer so you're not limited to 9 characters.  (The 10th is the null byte.)

Link to comment
https://forums.phpfreaks.com/topic/177207-c-char-help/#findComment-934446
Share on other sites

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.