Help in terminating a program

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2007
Posts: 6
Reputation: samernic is an unknown quantity at this point 
Solved Threads: 0
samernic samernic is offline Offline
Newbie Poster

Help in terminating a program

 
0
  #1
Feb 25th, 2007
How can you make a program ends when the user enters the string "terminate"?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,740
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 739
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Help in terminating a program

 
0
  #2
Feb 25th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 6
Reputation: samernic is an unknown quantity at this point 
Solved Threads: 0
samernic samernic is offline Offline
Newbie Poster

Re: Help in terminating a program

 
0
  #3
Feb 25th, 2007
Originally Posted by Narue View Post
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?
How can you compare input from user to a string terminate?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,740
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 739
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Help in terminating a program

 
0
  #4
Feb 25th, 2007
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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 6
Reputation: samernic is an unknown quantity at this point 
Solved Threads: 0
samernic samernic is offline Offline
Newbie Poster

Re: Help in terminating a program

 
0
  #5
Feb 25th, 2007
Originally Posted by Narue View Post
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.
I am using two programs this is the first one:
  1. #include <sys/ipc.h>
  2. #include <sys/msg.h>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. int main( ){
  7. char buffer[50];
  8.  
  9. int qid = msgget(ftok(".",'u'), 0);
  10. // declare my message buffer
  11. struct buf {
  12. long mtype; // required
  13. char greeting[50]; // mesg content
  14. };
  15. buf msg;
  16. int size = sizeof(msg)-sizeof(long);
  17. // prepare my message to send
  18. //strcpy(msg.greeting, "Hello there");
  19.  
  20. cout<<"Enter your message: "<<endl;
  21. cin.getline(buffer,50);
  22. strcpy(msg.greeting,buffer);
  23. cout << getpid( ) << ": sending message" << endl;
  24. msg.mtype = 117; // set message type mtype = 117
  25. msgsnd(qid, (struct msgbuf *)&msg, size, 0); // sending
  26. msgrcv(qid, (struct msgbuf *)&msg, size, 314, 0); // reading
  27. cout << getpid( ) << ": gets reply" << endl;
  28. cout << "reply: " << msg.greeting << endl;
  29. // cout << getpid( ) << ": now exits" << endl;
  30. exit(0);
  31. }
This is the second:
  1. #include <sys/ipc.h>
  2. #include <sys/msg.h>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. int main( ) {
  7. // create my msgQ with key value from ftok()
  8. int qid = msgget(ftok(".",'u'), IPC_EXCL|IPC_CREAT|0600);
  9. // declare my message buffer
  10. struct buf {
  11. long mtype; // required
  12. char greeting[50]; // mesg content
  13. }; buf msg;
  14. int size = sizeof(msg)-sizeof(long);
  15. msgrcv(qid, (struct msgbuf *)&msg, size, 117, 0); // read real mesg
  16. cout << getpid( ) << ": gets message" << endl;
  17. cout << "message: " << msg.greeting << endl;
  18.  
  19. //strcat(msg.greeting, " and Adios.");
  20. //cout << getpid( ) << ": sends reply" << endl;
  21. msg.mtype = 314; // only reading mesg with type mtype = 314
  22. msgsnd(qid, (struct msgbuf *)&msg, size, 0);
  23.  
  24. // cout << getpid( ) << ": now exits" << endl;
  25. msgrcv(qid, (struct msgbuf *)&msg, size, -112, 0);
  26. msgrcv(qid, (struct msgbuf *)&msg, size, 0, 0);
  27. //now safe to delete mesg queue
  28. msgctl(qid, IPC_RMID, NULL);
  29. exit(0);
  30. }
I need to end both programs when user enters terminate.
Last edited by Narue; Feb 25th, 2007 at 3:38 pm. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,740
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 739
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Help in terminating a program

 
1
  #6
Feb 25th, 2007
Hmm, interesting that your problem is comparing strings. In this case you would use strcmp:
  1. if ( strcmp ( buffer, "terminate" ) == 0 ) {
  2. // Notify the other program of termination
  3. return 0;
  4. }
Last edited by Narue; Feb 25th, 2007 at 4:19 pm. Reason: Brain fart :p
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC