Char String Arrays

I have to write a program that has a string stored as "Vacation is near" in an array named message. I have to use a function that accepts the message in a parameter and then displays the message. I have no idea how to use strings or chars and was wondering if i could get some help. I basically Dont know how to declare them at all. Please help me out.
Last edited on
You haven't even tried to set out a scheme for main()... C'mon, don't let anxiety overcome you. Just start writing, then the code will come.
Here's the basic:
1
2
3
4
5
6
7
8
9
10
// "...a string stored as "Vacation is near" in an array named message..."
char message[] = ... ;


// "... a function that accepts the message in a parameter..."
void displayMessage(char mess[])
{
   // "...and then displays the message..."
   std::cout << ... 
}

Last edited on
Hello TyDaddyFlex,

Unless you are stuck using the old C-style char arrays the std::string would be a better choice. Here is Enoizat's example using std::string:

1
2
3
4
5
6
7
8
9
10
11
12
#include <string>

// "...a string stored as "Vacation is near" in an array named message..."
std::string message = ... ;


// "... a function that accepts the message in a parameter..."
void displayMessage(std::string mess)
{
   // "...and then displays the message..."
   std::cout << mess << "\n";
}


The C-style array will work, but the string has more functionality and a bit easier to use with C++.

Hope that helps,

Andy
Topic archived. No new replies allowed.