| | |
Help in terminating a program
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
If you're in main and not some nested function call you can simply return and the program will end. Otherwise, there's a function called exit in stdlib.h that does the same thing from anywhere in the program. Do you know how to take a string as input and compare it with another string?
I'm here to prove you wrong.
•
•
Join Date: Feb 2007
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
If you're in main and not some nested function call you can simply return and the program will end. Otherwise, there's a function called exit in stdlib.h that does the same thing from anywhere in the program. Do you know how to take a string as input and compare it with another string?
•
•
Join Date: Feb 2007
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
Post your code so far so that I know what language and language features you're using for your strings and I'll be happy to show you.
C++ Syntax (Toggle Plain Text)
#include <sys/ipc.h> #include <sys/msg.h> #include <iostream> using namespace std; int main( ){ char buffer[50]; int qid = msgget(ftok(".",'u'), 0); // declare my message buffer struct buf { long mtype; // required char greeting[50]; // mesg content }; buf msg; int size = sizeof(msg)-sizeof(long); // prepare my message to send //strcpy(msg.greeting, "Hello there"); cout<<"Enter your message: "<<endl; cin.getline(buffer,50); strcpy(msg.greeting,buffer); cout << getpid( ) << ": sending message" << endl; msg.mtype = 117; // set message type mtype = 117 msgsnd(qid, (struct msgbuf *)&msg, size, 0); // sending msgrcv(qid, (struct msgbuf *)&msg, size, 314, 0); // reading cout << getpid( ) << ": gets reply" << endl; cout << "reply: " << msg.greeting << endl; // cout << getpid( ) << ": now exits" << endl; exit(0); }
C++ Syntax (Toggle Plain Text)
#include <sys/ipc.h> #include <sys/msg.h> #include <iostream> using namespace std; int main( ) { // create my msgQ with key value from ftok() int qid = msgget(ftok(".",'u'), IPC_EXCL|IPC_CREAT|0600); // declare my message buffer struct buf { long mtype; // required char greeting[50]; // mesg content }; buf msg; int size = sizeof(msg)-sizeof(long); msgrcv(qid, (struct msgbuf *)&msg, size, 117, 0); // read real mesg cout << getpid( ) << ": gets message" << endl; cout << "message: " << msg.greeting << endl; //strcat(msg.greeting, " and Adios."); //cout << getpid( ) << ": sends reply" << endl; msg.mtype = 314; // only reading mesg with type mtype = 314 msgsnd(qid, (struct msgbuf *)&msg, size, 0); // cout << getpid( ) << ": now exits" << endl; msgrcv(qid, (struct msgbuf *)&msg, size, -112, 0); msgrcv(qid, (struct msgbuf *)&msg, size, 0, 0); //now safe to delete mesg queue msgctl(qid, IPC_RMID, NULL); exit(0); }
Last edited by Narue; Feb 25th, 2007 at 3:38 pm. Reason: Added code tags
Hmm, interesting that your problem is comparing strings. In this case you would use strcmp:
c Syntax (Toggle Plain Text)
if ( strcmp ( buffer, "terminate" ) == 0 ) { // Notify the other program of termination return 0; }
Last edited by Narue; Feb 25th, 2007 at 4:19 pm. Reason: Brain fart :p
I'm here to prove you wrong.
![]() |
Similar Threads
- 500 ways to print [1..10]! (IT Professionals' Lounge)
- Finding length (Java)
- C++ (C++)
- Pop3 Mail Watcher (Part 1) (Visual Basic 4 / 5 / 6)
- How can I get Highest, Lowest and Average ? (C++)
- trouble with c++ code (C++)
Other Threads in the C++ Forum
- Previous Thread: help with encryption! asap
- Next Thread: word count
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






