How can you make a program ends when the user enters the string "terminate"?

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?

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?

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.

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:

#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);
}

This is the second:

#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);
}

I need to end both programs when user enters terminate.

Hmm, interesting that your problem is comparing strings. In this case you would use strcmp:

if ( strcmp ( buffer, "terminate" ) == 0 ) {
  // Notify the other program of termination
  return 0;
}
commented: You'd think someone who managed IPC could have figured out strcmp() - Salem +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.