Quick Question

This is a discussion on Quick Question within the C++ Programming forums, part of the General Programming Boards category; Just a quick question, If I wanted to define a function in a seperate file (which would be better right?) ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    99

    Lots of Questions now!

    Just a quick question, If I wanted to define a function in a seperate file (which would be better right?) would I do it in a header file or a cpp file?
    Last edited by Da-Spit; 04-28-2002 at 05:30 AM.

  2. #2
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    i prefer header files.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    99
    k, I was just wondering if you HAD to use one of the two or if it didn't matter. I think I'll make an extra cpp file that will
    #Include all my header files, then include the cpp in my main file.

  4. #4
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    sure. do what you are comfertable with.

    Though it'll be nice to know wether there were some adv/disadv of using either one.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    99

    one more q

    Ok, thx. Just one more question. How would I define a function in a seperate file to where I was using it? I've read the section on functions in the tutorial, but would I do it any different if I was defining in a seperate file? or would there be an easier way? or would I have to do it the same way I would normally?. Well I guess that's kinda 4 questions, but oh well.

  6. #6
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    The only thing that is different for headers is that you must do

    #ifndef _MYHEADER_H
    #define _MYHEADER_H

    Or something like that on the top.

    And then

    #endif

    at the bottom.
    Other than that there's no differnce except that you cant acces variables from the cpp files so you need to have them in the header aswell.

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    99
    Ok, got it. Wahoo! I can almost purchase a doughnut from my canteen! I'm gonna link it up to my Bank Account program once I implement the i/o to it. It's gonna be sweeeet. lol

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    99

    ok 1 more!

    Lol, ok. I hope this is the last one... I have to Linker errors, I know what the first one is, but don't know how to fix it, and I think I know what the other is, but still can't fix it.

    Here they are...

    c:\my documents\doughnut.o(.text+0x2c):doughnut.cpp:
    multiple definition of `PurchaseDoughnut(void)'

    c:\my documents\canteen-main.o(.text+0x2c):canteen-main.c: first defined here

    with the first error, is it supposed to replace () with (void)? cause in my program it's PurchaseDoughnut();

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    99

    ?!

    and why does it say canteen-main.c? Did it just cut off the pp cause the filename was too long or what? I can't see any .c files in the folder except some other progs.

  10. #10
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    Well you have defined stuff more than you should. Code?

  11. #11
    Registered User
    Join Date
    Apr 2002
    Posts
    99

    Code:

    Here is the main file:

    Code:
    #include <doughnut.cpp>
    #include <iostream.h>
    #include <stdio.h>
    
    int PurchaseDoughnut(); //Is this where I put the prototype or should it be in doughnut.cpp?
    
    int main()
    {
    int change;
    float price = 0.0;
    float money_entered;
    float money = price;
    char menu[20];
    char wdyh[20] = "What do you have?";
    
    cout<<"Welcome to the Canteen! How may I serve you?"<<endl<<endl;
    cout<<"Type 'What do you have?' to look at our Menu."<<endl;
    cin.getline(menu, 20);
    cout<<endl;
    if (menu == wdyh)
    {
    cout<<"We currently have in stock: Doughnut."<<endl;
    cin.getline(menu, 20);
    };
    
    if (menu == "Doughnut")
    {
    PurchaseDoughnut();
    };
    
      return 0;
    }
    And here is the file that defines PurchaseDoughnut()

    Code:
    #include <iostream.h>
    
    int PurchaseDoughnut(); //Is this where I put the prototype or should it be in my main file?
    
    int PurchaseDoughnut()
    {
    float change;
    float price = 0.0;
    float money_entered;
    float money = price;
    
    cout<<"Please enter the ammount you want to enter."<<endl;
    cin>>money_entered;
    change = money_entered - price;
    
    return 0;
    }
    Last edited by Da-Spit; 04-28-2002 at 05:33 AM.

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > #include <doughnut.cpp>
    But this is a bad idea - you're always compiling this file

    You should use your compiler's project to list all the .cpp files. By creating what is in effect a single source file, your compile times will grow as your code grows.

    By listing all your .cpp files, the compiler will compile only those files which need to be compiled.


    Code:
    main.cpp
    --------
    #include <iostream.h>
    #include <stdio.h>
    #include "doughnut.h"
    int main()
    {
      // your code
      return 0;
    }
    
    doughnut.cpp
    ------------
    #include <iostream.h>
    #include "doughnut.h"
    int PurchaseDoughnut()
    {
    float change;
    float price = 0.0;
    float money_entered;
    float money = price;
    
    cout<<"Please enter the ammount you want to enter."<<endl;
    cin>>money_entered;
    change = money_entered - price;
    
    return 0;
    }
    
    
    doughnut.h
    ----------
    #ifndef INCLUDED_DOUGHNUT_H
    #define INCLUDED_DOUGHNUT_H
    // this prevents problems, should you include the same file twice
    
    // prototype for the function
    int PurchaseDoughnut();
    #endif

  13. #13
    Registered User
    Join Date
    Apr 2002
    Posts
    99

    Cool

    Thanks, I was having a hard time figuring out where the prototype should go, I'll try that now.

  14. #14
    Registered User
    Join Date
    Apr 2002
    Posts
    99

    Thx Heaps!

    It compiled! Woohoo! It doesn't seem to execute PurchaseDoughnut() but I'll see if I can fix it myself before I ask for more help.

  15. #15
    Registered User
    Join Date
    Apr 2002
    Posts
    99

    Got it!

    Ok I got it, it works perfectly. I know now what I was doing wrong. I was using
    Code:
    int wdyh[20] = "What do you have?"; 
    cin.getline(menu, 20);
    
    if (menu == wdyh)
    {
    PurchaseDoughnut()
    };
    Instead of using strcmp, silly me!

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21