newbie question about parameters

I have a C program with a #define as follows :

#define MAIN_MSG "Testing"

I want to not have it hard coded but pass it to the routine as parameter and inside the routine assign it to a variable .
how can I do that?
Something like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

#define MAIN_MSG "Testing" 

void some_func (const char * msg) 
{   cout << msg << endl;
}

int main ()
{   some_func (MAIN_MSG);
    return 0;
}

but I don't want to hard code the msg , since I will be sending many msgs ..
Do you want to pass it in from the command line?

C++ is a compiled language. Either you hard code it, pass it in from the command line, read it from a file, or prompt the user for it. Those are about your only choices.
Last edited on
hi this is what I want :

int main ()
{
send_msg ("Message1");
..
send_msg ("Message2");
}

That's no different that the snippet I showed you above.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

void send_msg (const char * msg) 
{   cout << msg << endl;
}

int main ()
{   send_msg ("Message1");
    send_msg ("Message2");
    return 0;
}

oh I see , yes i understand

thanks
ok I am stuck again .
I have this declaration in program of a constant which I want to replace.
can I do that ? I tried strcpy but its not working.


#define MAIL_MESSAGE "Messaeg1"

void send_email(const char * message)
{
strcpy(message,MAIN_EMAIL_MSG );
}

void main()
{

send_email("Message2");
send_emial("Message3");

}
That shouldn't even compile:

1) You haven't defined MAIN_EMAIL_MSG.

2) You're attempting to copy something into a const char *.

It doesn't look like you're thinking clearly about what your variables are for. What is the message parameter supposed to be for? What is MAIL_MESSAGE supposed to be for? What is MAIN_EMAIL_MSG supposed to be for?

Also, please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/
Last edited on
You should probably review Macros and Functions for a good bit before giving this another go. Also do not use void main, use int main instead.
Topic archived. No new replies allowed.