User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 425,895 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,931 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 269 | Replies: 3 | Solved
Reply
Join Date: Jun 2008
Posts: 6
Reputation: Utter_iMADNESS is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Utter_iMADNESS Utter_iMADNESS is offline Offline
Newbie Poster

Including other C files into program

  #1  
Jun 18th, 2008
Hello,
I'm trying to make a code that acts as a simple menu for a number of programs. What I was wondering was how do I include the .ccp files and run them in the menu program. Here's what I have so far:
#include <stdio.h>
#include <stdlib.h>
#include "1.cpp"
#include "2.cpp"
#include "3.cpp"
#include "4.cpp"

int main()
{
    int select;
    
    printf("Assignments\n");
    printf("-----------\n\n");
    printf("1. Multiple of 5\n2. Calculator\n3. Rock, Paper, Scissors\n4. No Vowels (incomplete)\n7. Exit\n");
    scanf(" %d", &select);
    
    switch(select)
    {
                  case 1:
                          a1();
                          break;
                          
                  case 2: 
                          a2();
                          break;
                          
                  case 3:
                         a3();
                         break;
                         
                  case 4:
                         a4();
                         break;
                          
                  default:
                          printf("That's not a choice.\n");
                          break;
    }
    
    system("PAUSE");
    return 0;
}

I've placed all the files in the same folder and also created a project. I also tried putting the full file name for each file but I kept getting the same error: multiple definition of `a1()' or a2(), etc...

Any help will be greatly appreciated.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2004
Posts: 3,620
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 142
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Including other C files into program

  #2  
Jun 18th, 2008
In general, never #include source files. Learn about separate compilation and linking.
Reply With Quote  
Join Date: Dec 2005
Posts: 3,638
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 22
Solved Threads: 415
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: Including other C files into program

  #3  
Jun 19th, 2008
http://faq.cprogramming.com/cgi-bin/...&id=1043284392
Same principle for C
You separate the implementation (in a .c file) from the interface (in a .h).

Everything else includes just the interface, and the implementation is in just one place.


Also, stop calling your C programs with a .cpp extension. That will just lead you astray.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Reply With Quote  
Join Date: Jun 2008
Posts: 6
Reputation: Utter_iMADNESS is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Utter_iMADNESS Utter_iMADNESS is offline Offline
Newbie Poster

Re: Including other C files into program

  #4  
Jun 19th, 2008
Thanks for your help guys. I figured it out using this link: http://faq.cprogramming.com/cgi-bin/...&id=1043284392. Here's the code that I ended up with:
#include <stdio.h>
#include <stdlib.h>
#include <process.h>

int main(void)
{
    int select;
    
    printf("Assignments\n");
    printf("-----------\n\n");
    printf("1. Multiple of 5\n2. Calculator\n3. Rock, Paper, Scissors\n4. No Vowels (incomplete)\n7. Exit\n");
    scanf(" %d", &select);
    
    switch(select)
    {
                  case 1:
                          spawnl( P_OVERLAY, "1.exe",
                          "1.exe", "1.exe", "Arg1", "Arg2", NULL );
                          break;
                          
                  case 2: 
                          spawnl( P_OVERLAY, "2.exe",
                          "2.exe", "2.exe", "Arg1", "Arg2", NULL );
                          break;
                          
                  case 3:
                         spawnl( P_OVERLAY, "3.exe",
                         "3.exe", "3.exe", "Arg1", "Arg2", NULL );
                         break;
                         
                  case 4:
                         spawnl( P_OVERLAY, "4.exe",
                         "4.exe", "4.exe", "Arg1", "Arg2", NULL );
                         break;
                         
                  case 7:
                         break;
                          
                  default:
                          printf("That's not a choice.\n");
                          break;
    }
    
    return 0;
}
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 7:19 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC