pointers HELP

This is a discussion on pointers HELP within the C++ Programming forums, part of the General Programming Boards category; can someone help me to compile this program.It does have many errors.Thanks int careful(int *a, int b) { int tmp; ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    7

    Talking pointers HELP

    can someone help me to compile this program.It does have many errors.Thanks

    int careful(int *a, int b)
    {
    int tmp;

    tmp = *a;
    b = *a + 50/tmp;
    *a = tmp;
    return b;
    }

    void main()
    {
    int a = 10, b = 23;

    cout << “first = “ << a << “, second = “ << b << endl;
    cout << “careful = “ << careful(&b,a);
    cout << “first = “ << b << “, second = “ << a << endl;
    }

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Quote Originally Posted by eriaug
    can someone help me to compile this program.It does have many errors.Thanks
    Change



    To

    "

    They are different characters. Just delete all your quotes and re-type them as quotes.


    And next time:
    • Read the sticky about posting code... it says "Use code tags!"
    • void main is not correct, it should be int main to be valid C++ (see: http://www.research.att.com/~bs/bs_faq2.html#void-main)
    • Post a complete program. Yours is missing the #include directives.
    • List the errors you are getting.

    Good luck!
    Last edited by jlou; 11-09-2004 at 09:05 PM.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    7

    Wink

    this is the program.Thank you for replying

    #include <iostream.h>


    int careful(int *a, int b)
    {
    int tmp;

    tmp = *a;
    b = *a + 50/tmp;
    *a = tmp;
    return b;
    }

    int main()
    {
    int a = 10, b = 23;

    cout << "first = " << a << ", second = " << b << endl;
    cout << "careful = << careful(&b,a);
    cout << "first = " << b << ", second = " << a << endl;
    }
    .cpp(20) : error C2001: newline in constant
    .cpp(21) : error C2146: syntax error : missing ';' before identifier 'cout'
    .cpp(22) : warning C4508: 'main' : function should return a value; 'void' return type assumed
    Error executing cl.exe.

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    First of all read this post!

    Look here:
    Code:
    cout << "careful = << careful(&b,a);
    Close the comma like this
    Code:
    cout << "careful = "<< careful(&b,a);
    And at the end of main add
    Code:
    return 0;

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    7
    perfect-thanks a lot it did work

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    And you've got an outdated header.
    #include <iostream>
    using namespace std; // There are reasons not to do this at times (based on preferences), but for now, it works, and you'll get to namespaces later.

    ...instead of...
    #include <iostream.h>
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    63
    int main()

    That is your clue to put in a return value.

    Whenever I start up a program, I start myself with a program that does nothing:
    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
    
    (some blank spaces)
    
    int closer;
    cin >> closer;       // just so that results don't fly too fast for me to read!
    return 0;
    }
    That way I know I won't get any funky errors because I missed some of the basics. Just my little method of making sure I have it all

    After that I put in prototypes for functions and include any other files I may need. Then I define classes and jump into the code

  8. #8
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Quote Originally Posted by Philandrew
    int main()

    That is your clue to put in a return value.
    The main function is special in that you do not have to return a value. If you don't, then 0 will automatically be returned (on most compliant compilers). So unless you are trying to return something specific, it is fine to leave main without a return statement.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 06:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 11:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM

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