Thread: Cant use as function...?

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    114

    Cant use as function...?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <conio.h>
    #include <ctype.h>
    #include <windows.h>
    #include <iostream>
    using namespace std;
    extern int gold;
    extern int donated;
    int teif()
    {
        int teif;
        char choice;
        int size;
        int x;
        char sent5[]="\"Maby next time you will donate\"";
        char sent0[]="\"You would like to give teif?";
        char sent1[]="\"How much teif would you like to give?\"";
        char sent3[]="\"Thank you for your gift\"";
        char sent2[]="The priest smiles \"Thanks for your generous gift\"";
        char sent4[]="\"Holy god himself thanks for the generous amount of gold\"";
        char sent6[]="\"You dont have that much gold\"";
        
        system("cls");  //ASKS IF YOUR SURE U WANT TO GIVE TEIFS
        size=strlen(sent0);
        for(x=0;x>size;x++)
        {
           Sleep(30);
           printf("%c", sent0[x]);
        }
        printf("<Y>es\n<N>o\n");
        choice=toupper(getch());
        for(;;)
        {
           if(choice=='Y')
           {
              system("cls"); //How much would u give
              size=strlen(sent1);
              for(x=0;x>size;x++)
              {
                  Sleep(30);
                  printf("%c", sent1[x]);
              }
              cin >> teif;
              if(teif <= gold)
              {
                 if(teif >= 1)
                 {
                    if(teif >= 10)
                    {
                       if(teif >= 100)
                       {
                          system("cls"); //Generous gift
                          size=strlen(sent4);
                          for(x=0;x>size;x++)
                          {
                             Sleep(30);
                             printf("%c", sent4[x]);
                          }
                                 
                       } 
                       else
                       {
                          system("cls"); //Semi generous Gift 
                          size=strlen(sent2);
                          for(x=0;x>size;x++)
                          {
                             Sleep(30);
                             printf("%c", sent2[x]);
                          } 
                       }     
                    }
                    else
                    {
                       system("cls"); //Thanks for little
                       size=strlen(sent3);
                       for(x=0;x>size;x++)
                       {
                          Sleep(30);
                          printf("%c", sent3[x]);
                       } 
                    }
                 }
                 else if(teif==0)
                 {
                    system("cls"); //No gold offer
                    size=strlen(sent5);
                    for(x=0;x>size;x++)
                    {
                       Sleep(30);
                       printf("%c", sent5[x]);
                    }
                 }
                 else
                 { 
                     cin >> teif; 
                 }
                  
              }              
              else
              { 
                 system("cls"); //Not enough gold
                 size=strlen(sent6);
                 for(x=0;x>size;x++)
                 {
                    Sleep(30);
                    printf("%c", sent6[x]);
                 }
              }              
           }    
           
        }
    donated = donated + teif;
    return(0);
    }
    This is one of my functions for a game im making and for some reason it says i cant use this as a function not sure why? My Main file just says cant be used as function it worked with the title.cpp and main file why not main file and this file :9

    Code:
    #include "teif.cpp"
    int game()
    {
          teif();
    return 0;
    }
    Last edited by Nathan the noob; 02-17-2009 at 02:29 AM.

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Are you learning C or C++?

    You include <iostream> and the namespace when 95% of your code is C. So you are including the namespace librarys when you really do not need to.

    How is the function defined? What are you calling it with? What is the exact error message you get. and can you post the code for main that calls it and any forwarding declaorations.
    Double Helix STL

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    Hmmm il do that one second but i found out that the code for the above is jumbled up a bit tho maby thats it

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    K i added a basic program of how it is and im learning c++ i wasnt aware this is also c lol i used iostream i beleive im not sure cause of cin
    if u could show me a way to get the user input for more than 1 character with out using library or iostream then il try that but i dont no another way
    Last edited by Nathan the noob; 02-17-2009 at 02:34 AM.

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Im going on the aspect you are learning C, although this is the wrong forum... C uses scanf() to read basic integral input, if you want to read strings you can use fgets() in C

    Code:
    puts("Enter a string: ");
    fgets( theString, sizeof( theString ), stdin );
    theString[ strlen( theString ) -1 ] = 0; // get rid of newline character in fgets()
    If you are using C++, then use std::getline()

    Code:
    std::cout << "Enter a string: ";
    std::getline( std::cin, theString );
    Double Helix STL

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And don't forget
    std::string theString;
    first.

    Also, do not include .cpp files.
    .cpp files are compiled; header files (.h) are included.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    Hey thanks but it still says i cin is undeclared if i remove io stream and std

    and i changed the cpp files to h files still says cannot be used

    im just going to add the 2 files for you to see cause maby im not explaining enough
    Attachment 8738
    Attachment 8739
    Last edited by Nathan the noob; 02-17-2009 at 11:28 AM.

  8. #8
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    ok.. hard to know where to start...

    #1 Header files ( thats files ending with a .h extention should "never" contain any active working C++/C code. They are for purley definition purposes.

    The first thing you need to do is
    organise all that code into logical functions. A class would be one idea, and get rid of the global
    variables.
    Double Helix STL

  9. #9
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    Can u give me a short example of a definition cause i dont no wat u mean no working c++ code ?

  10. #10
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    if you had a class for example:

    In your header file lets call it foo.h

    Code:
    #ifndef FOO_H
    #define FOO_H
    
    // this is a "definition"
    // it tells the compiler what the class is called
    // and what the functions are called
    class Foo {
       public:
       Foo();
       ~Foo();
    };
    
    #endif
    Now I have that, I can make another .cpp file called foo.cpp

    Code:
    #include "foo.h" // we include the definition file
    #include <iostream>
    
    Foo::Foo() {
    }
    
    Foo::~Foo() {
    }
    Now, in "main" I can use the files I have created

    Code:
    #include "foo.h" // we need it in main too
    int main() {
    Foo fo; 
    return 0;
    }
    The above is correct as all definitions are in place at compile time. In the case of your program, you only need a header file if you are planning on placing all the player attributes into a class later on ( good idea btw ). You can use a header file for independat function definitions if you wish, but they are generally used for class/struct and other definitions.
    Double Helix STL

  11. #11
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    hmmm some confusing stuff wat does Foo::~Foo mean? is it just a function?

    And i agree classes would make stuff easier just hard to know how to make em
    Code:
    //Class//
    #ifndef DAMAGE_H
    #define DAMAGE_H
    #include <time.h>
    class Damage {
          int x;
          int y;
          srand(time(NULL));
          public:
                 void damage(int, int);
                 void max(int * int);
                 void random(rand() % (max - int);
    };
    
    #endif
    Is that a class

    //Using the class but getting alot of errors!!!
    Code:
    #include "Damage.h"
    #include <iostream>
    #include <time.h>
    void Damage::damage(int a, int b) {
         x = a;
         y = b;
    }
         
    int main () {
      srand(time(NULL));
      Damage sword;
      int c;
      sword.damage (1,3);
      c = (rand() % (a - b);
      std::cout << c;
      system("PAUSE");
      return 0;
    }
    Last edited by Nathan the noob; 02-17-2009 at 01:38 PM.

  12. #12
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Foo is the name of a class its just an odd name you use for an example. it has no special meaning in the language.

    When you write your #define #ifndef ensure they are capital letters.

    The class ends with a semi-colon after the closing brace - you dont neec stdio.h in there.

    Your edited example is wrong.

    Damage and damage are not equal. The names must match between the unary scope operator. So:

    Code:
    Damage::Damage
    would work.

    You do not place the above in the same function as main. As I showed previous, the implementation and definition are two independant files.
    Last edited by swgh; 02-17-2009 at 01:29 PM.
    Double Helix STL

  13. #13
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    Hmmm i fixed the semi collin thing but when i go to use this program

    But how would i get int, int in damage and int, int in random?

  14. #14
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Rand could be called in a class function.

    For example:

    Code:
    int Foo::returnRandomValue ( int num ) {
        num = ( 1 + rand() ) % 10;
    
        return num;
    }
    You really should read up on classes on the site's tutorial to get a better understanding. Basically a class is a blue print for an object. It is the "definition" of what the object can do. Public data is acessable by all functions that call it within scope, private data is acessable to the
    class and friends of the class, and protected data can be shared between classes using inherietence.

    The class can deal with more or less all of your players actions if you code it correctly and structure it right. Remember to think about what parts of the class you want different functions to have acess to, you control this through the public/private labels.
    Double Helix STL

  15. #15
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    Damage::damage(int a, int b);
    arrg why wouldnt this work it says it expects something before int and int.... i dont under stand.

    And if their were

    void example(int, int)

    and in the main file i used

    ex.example(?, ?) how would i fill that in so it used a saved interger value like a = 3 cause it wont let me
    Last edited by Nathan the noob; 02-17-2009 at 01:55 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM