Functions, what am I doing wrong here?

This is a discussion on Functions, what am I doing wrong here? within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream.h> #include <stdio.h> int main() { int name() { int nam; cout<<"Enter your name: "; cin>>nam; } int ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    11

    Functions, what am I doing wrong here?

    Code:
    #include <iostream.h>
    #include <stdio.h>
    int main()
    {
    int name()
    {
    int nam;
    cout<<"Enter your name: ";
    cin>>nam;
    }
    int phn()
    {
    int phone;
    cout<<"Enter the Phone number";
    cin>>phone;
    }
    cout<<nam;
    cout<<phone;
    }
    I just want it to compile and go into the other functions

  2. #2
    1479
    Join Date
    Aug 2003
    Posts
    253
    Firt off you are declaring your functions in main. Which is no good. Fucntion declarations must be made outside of main. Secondly you are not calling your fucntions.
    Knowledge is power and I want it all

    -0RealityFusion0-

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    You can't define functions within another function, i.e. a local function. You will also need return the value, or use a global variable since the local variable you are declaring inside the finctions goes out of scope when the function exits.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    11
    Sorry that was a bit too fast paced for me could you simple it out?

  5. #5
    1479
    Join Date
    Aug 2003
    Posts
    253
    Code:
    #include <iostream.h>
    #include <stdio.h>
    int main()
    {
    int name()
    {
    int nam;
    cout<<"Enter your name: ";
    cin>>nam;
    }
    int phn()
    {
    int phone;
    cout<<"Enter the Phone number";
    cin>>phone;
    }
    cout<<nam;
    cout<<phone;
    }
    should be.

    Code:
    
    #include <iostream>
    #include <stdio>
    using namespace std;
    
    
    int name()
    {
    int nam;
    cout<<"Enter your name: ";
    cin>>nam;
    
    return nam;
    
    }
    int phn()
    {
    int phone;
    cout<<"Enter the Phone number";
    cin>>phone;
    
    return phone;
    
    }
    
    int main()
    
    {
    cout<<nam;
    cout<<phone;
    }
    Also you declared name and phone as being intergers and they should be chars.
    Knowledge is power and I want it all

    -0RealityFusion0-

  6. #6
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Originally posted by mattbbx
    Sorry that was a bit too fast paced for me could you simple it out?
    I used to do that same thing. You can't define function within other functions.

    Code:
    int main()
    {
    void myFunction() // Can't do this.
    {
    cout<<"Hello";
    }
    return 0;
    }
    is an example of the improper way to do it. Instead:

    Code:
    void myFunction() {
    cout<<"Hello";
    }
    
    int main()
    {
    myFunction();
    return 0;
    }
    would be okay, though.

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    11
    ahhhhhhhhhh i see, even a small thing matters
    Thank you

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    11
    RealityFusion, thanks but for some reason it wont compile it complains about the variables in main()

    Ok i changed int to char but now the window flashes up real quick and I dont see anything

  9. #9
    1479
    Join Date
    Aug 2003
    Posts
    253
    I know. I basically just plotted out the problems you are going to have even after you do what the other guy suggested though. .
    You also forgot to put
    Code:
     return 0;
    at the end of our main fucntion.
    Knowledge is power and I want it all

    -0RealityFusion0-

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    "Ok i changed int to char but now the window flashes up real quick and I dont see anything"

    That means your program ran, finished, and the dos window closed. To get the dos window to pause so you can see the output read this:

    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

  11. #11
    Registered User
    Join Date
    Aug 2003
    Posts
    44
    My favorite way to get a pause is to use:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        cin.ignore();         //this waits for a newline char
        return 0;
    }
    This is nice because it completely ignores whatever gets inputted.

    Edit: Oh yeah. Code is sooo hard to read when it's not indented.
    Last edited by Wick; 08-14-2003 at 06:39 PM.
    Check out all my dimensions:
    Height, width, and for a limited time only... Depth!
    -sb

  12. #12
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Originally posted by RealityFusion
    I know. I basically just plotted out the problems you are going to have even after you do what the other guy suggested though. .
    You also forgot to put
    Code:
     return 0;
    at the end of our main fucntion.
    Any ANSI-C++ 98 compiler does not require a return 0; at the end of main. In '98, not explicitly returning anything is an implied "return 0;". If your compiler complains, it's not following the C++ standard.

  13. #13
    1479
    Join Date
    Aug 2003
    Posts
    253

    Thumbs up

    Wow! I didn't know that. Thanks for the info!
    Knowledge is power and I want it all

    -0RealityFusion0-

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is it legal to have functions within functions?
    By Programmer_P in forum C++ Programming
    Replies: 13
    Last Post: 05-26-2009, 12:21 AM
  2. An array of macro functions?
    By someprogr in forum C Programming
    Replies: 6
    Last Post: 01-28-2009, 07:05 PM
  3. help with functions and array please
    By rebel in forum C++ Programming
    Replies: 4
    Last Post: 11-24-2005, 01:09 PM
  4. functions - please help!!!!
    By linkies in forum C Programming
    Replies: 1
    Last Post: 08-21-2002, 08:53 AM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM

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