Solved

Running two different programs from a single program.

Posted on 1998-09-10
5
202 Views
Last Modified: 2010-04-15
Hi,
I have two different C progams named factor.c and index.c
I want to write a 3rd program which when run will show two options: "Press F for Factor program"
         "Press I for Index program"
While I press "F" the factor.c progam should run, and while
I press "I" index.c should run.
Pls let me know how to write the 3rd progam say combine.c?
Thanks,
Saurav
0
Comment
Question by:saurav
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 1

Expert Comment

by:Bonev
ID: 1252632
There are two options:
1) to combine everything in one executable.
2) to create 3rd executable that will start either one of factor and index depending on the selection.

0
 
LVL 11

Expert Comment

by:alexo
ID: 1252633
saurav, a C program may consist of several source files.

As Bonev noted, you can combine all the files into one program thus:
  factor.c will contain a function called Factor()
  index.c will contain a function called Index()
  combine.c will contain your main() function that will invoke one of the other functions.
Don't forget to add an include file (say, combine.h) that will contain the prototypes for both functions.

If you really want to treat all three as different programs, you can use the system(), execl() or spawnl() functions to execute another program.
0
 
LVL 2

Accepted Solution

by:
duneram earned 50 total points
ID: 1252634
assumptions:  factor.c when compiled is factor.exe
                     index.c when compiled is index.exe

This example, won't exit until the user presses E to exit.

combine.c:

int main(int argc, char *argv[])
{
   char x;

   while (1)
   {
      printf("Press F for Factor program\nPress I for Index program\n");
      printf("Press E to exit the program\n\n");
      x = getc(stdin);
      if (x == 'E')
      {
            printf("Exiting the program.  Thanks for using me\n");
             break; /* get out */
      }
      if (x == 'F')
      {
         printf("Executing the factor program\n");
         system("factor.exe");  /* execute the factor program */
      }
      if (x == 'I')
      {
         printf("Executing the Index program\n");
         system("index.exe");  /* execute the index program */
      }
   } /* end while */
   

   return 0;
}
0
 
LVL 2

Expert Comment

by:duneram
ID: 1252635
at the top of combine.c you will most likely need

#include <stdio.h>
#include <stdlib.h>


Alexo:  When I was doing the code above, you hadn't posted so I never saw it.  one of those timing things...

But that's the way the ball bounces sometimes.
0
 

Author Comment

by:saurav
ID: 1252636
Hi all,
Thanks everybody for replying. The codes 'duneram' wrote for me worked perfectly. Thank you very very much.Saurav
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question