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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.