Frustrated. How do I use void display(char * str) in a header file?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bent
    New Member
    • May 2007
    • 5

    Frustrated. How do I use void display(char * str) in a header file?

    Ugh.

    I am taking a c++ foundations course, and all was well until the moment came where I had to break my prototypes out into .h files. Until this point, all my prototyping has been done with ints, doubles, etc. - char seems to be different and vexing.

    test.h

    void display(char* str)
    ---------------------------------------------
    int main()
    {
    #include "test.h"
    char str[] = "hello";
    display();
    }

    At this point I get an error on compile and nothing I have done, or none of the reference material I have at my disposal is fixing the prolem.

    HELP!
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by bent
    Ugh.

    I am taking a c++ foundations course, and all was well until the moment came where I had to break my prototypes out into .h files. Until this point, all my prototyping has been done with ints, doubles, etc. - char seems to be different and vexing.

    test.h

    void display(char* str)
    ---------------------------------------------
    int main()
    {
    #include "test.h"
    char str[] = "hello";
    display();
    }

    At this point I get an error on compile and nothing I have done, or none of the reference material I have at my disposal is fixing the prolem.

    HELP!
    Your include commands should be at the top of the file and don't forget to add a semicolon after your prototype. Also, your display function takes a char pointer but you aren't passing it any. Make sure that you actually have the body for the display function.
    [code=cpp]
    #include "test.h"

    int main()
    {
    char str[] = "Hello";
    display(str);
    return 0;
    }
    [/code]
    Don't get confused by characters, they are just another data type like ints or doubles.

    Comment

    • bent
      New Member
      • May 2007
      • 5

      #3
      Thanks! - that was the vital piece -

      I am now getting a return value of ''__Z5helloPc' - which is not right.

      The whole program looks like:

      Code:
      #include <iostream>
      using namespace std;
      #include <limits>
      int main()
      {
      #include "testing.h"
      	char str[] = "hello";
          hello(str );
      
      	
          cout << str[0]
              << str[1]
              << str[2]
              << str[3]
      		<< str[4]
              << endl;
      
          
          cout << str[4]
              << str[3]
              << str[2]
              << str[1]
      		<< str[0]
              << endl;
      		
          return 0;
      }
      I am not sure where the garbage is coming from.

      it should return "hello
      olleh"

      Which it does when the function is in main().
      Last edited by bent; May 19 '07, 07:25 PM. Reason: forgot code end tage

      Comment

      • Savage
        Recognized Expert Top Contributor
        • Feb 2007
        • 1759

        #4
        Originally posted by bent
        Thanks! - that was the vital piece -

        I am now getting a return value of ''__Z5helloPc' - which is not right.

        The whole program looks like:

        Code:
        #include <iostream>
        using namespace std;
        #include <limits>
        int main()
        {
        #include "testing.h"
        	char str[] = "hello";
            hello(str );
        
        	
            cout << str[0]
                << str[1]
                << str[2]
                << str[3]
        		<< str[4]
                << endl;
        
            
            cout << str[4]
                << str[3]
                << str[2]
                << str[1]
        		<< str[0]
                << endl;
        		
            return 0;
        }
        I am not sure where the garbage is coming from.

        it should return "hello
        olleh"

        Which it does when the function is in main().
        It's probably coming from function.

        And what do u mean by:

        "Which it does when the function is in main()."?

        Ur hello function is currently in main.

        Please can u give us more info..

        Savage

        Comment

        • bent
          New Member
          • May 2007
          • 5

          #5
          Originally posted by Savage
          It's probably coming from function.

          And what do u mean by:

          "Which it does when the function is in main()."?

          Ur hello function is currently in main.

          Please can u give us more info..

          Savage
          my testing.h header file contains:

          Code:
          void hello(char* str);
          if I put

          Code:
          char * str = new char[5];
          str = "hello";
          in the main body of my program, it returns my expected results. It is when I include the header file, I get the garbage return value.

          Comment

          • Savage
            Recognized Expert Top Contributor
            • Feb 2007
            • 1759

            #6
            Originally posted by bent
            my testing.h header file contains:

            Code:
            void hello(char* str);
            if I put

            Code:
            char * str = new char[5];
            str = "hello";
            in the main body of my program, it returns my expected results. It is when I include the header file, I get the garbage return value.
            Can we see function hello definition?

            Savage

            Comment

            • bent
              New Member
              • May 2007
              • 5

              #7
              Originally posted by Savage
              Can we see function hello definition?

              Savage
              Savage,

              This is where I am getting ignorant. Function hello only exists as a prototype in the header file. everything else I have posted is the entirety of the program.
              I am a rank beginner at OOP, so please forgive me if I am missing something or mis-understanding something.

              The goal of the program is to use pointer arithmatic to display the string "hello" forwards and bachwards . As far as coding it without using the header file, I am successful.
              So far, I have been unable to include the header in any meaningful way. Hence my frustration.

              Comment

              • Savage
                Recognized Expert Top Contributor
                • Feb 2007
                • 1759

                #8
                Originally posted by bent
                Savage,

                This is where I am getting ignorant. Function hello only exists as a prototype in the header file. everything else I have posted is the entirety of the program.
                I am a rank beginner at OOP, so please forgive me if I am missing something or mis-understanding something.

                The goal of the program is to use pointer arithmatic to display the string "hello" forwards and bachwards . As far as coding it without using the header file, I am successful.
                So far, I have been unable to include the header in any meaningful way. Hence my frustration.
                I allready know all that and this is probably ur problem:

                Code:
                int main()
                {
                #include "testing.h"
                    char str[] = "hello";
                    hello(str );
                As ilikepython said ur include commands should be at the top of the code.

                (Sorry,didn't noticed it when I was looking before few minutes)

                Savage

                Comment

                • bent
                  New Member
                  • May 2007
                  • 5

                  #9
                  Code:
                  int main()
                  {
                  #include "testing.h"
                      char str[] = "hello";
                      hello(str );
                  The problem, it appeared was on the final line above, with referenced the prototype, but didn't have any play with the variables. Removing that line fixed the output.

                  Thanks!

                  Comment

                  • Savage
                    Recognized Expert Top Contributor
                    • Feb 2007
                    • 1759

                    #10
                    Originally posted by bent
                    Code:
                    int main()
                    {
                    #include "testing.h"
                        char str[] = "hello";
                        hello(str );
                    The problem, it appeared was on the final line above, with referenced the prototype, but didn't have any play with the variables. Removing that line fixed the output.

                    Thanks!
                    Allways a pleasure..

                    Savage

                    Comment

                    Working...