Problem with Functions!

This is a discussion on Problem with Functions! within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream.h> int x; void input(); int main() { input(); cout << x; return 0; } void input() { ...

  1. #16
    Registered User
    Join Date
    Nov 2002
    Posts
    3
    Code:
    #include <iostream.h>
    
    int x;
    void input();
    int main()
    {
           input();
           cout << x;
           return 0;
    }
    
    void input()
    {
            cin >> x;
            cout << x;
    }
    Is this what you are trying to accomplish?
    If so, it works when compiled under visual C++...

    Code tags added by Kermi3

  2. #17
    Registered User dizz's Avatar
    Join Date
    Nov 2002
    Posts
    41
    Originally posted by dizz

    it has to be set up like this

    struct

    inputFunction()

    randomFunction()

    outputFunction()

    main()
    Every variable must be sent to the structure and then retrieved from it

  3. #18
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    Code Tags

    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happier about helping you

    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found on the code tag post at the top of every forum. I also suggest you take a look at the board guildlines if you have not done so already.

    This is a common first post mistake, just remember to use [code] tags in the future and you'll get much more help.

    If this is your first time posting here the welcome, and if there's anything I can do or any questions I can answer about these forums, or anything else, please feel free and welcome to PM me.


    Good Luck with your program,

    Kermi3
    Lead Moderator
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  4. #19
    Registered User dizz's Avatar
    Join Date
    Nov 2002
    Posts
    41
    that was very nice kermi3 but why not give my problem a go?

  5. #20
    Registered User
    Join Date
    Nov 2002
    Posts
    3
    Kermi: lol, haha, i forgot to type them in. I am new but I DID read your sticky...Thanks for doing it for me.

  6. #21
    Guest1
    Guest
    Is this what you're looking for?

    Code:
    #include<iostream.h>
    #include<iomanip.h>
    #include<conio.h>
    #include<string.h>
    #include<math.h>
    #include<time.h>
    #include<stdlib.h>
    
    struct info
    {
            int number;
    };
    
    
    void input(info& vars)
    {
            cout<<"Enter a number: ";
            cin>>vars.number;
            /******************************************
                          IT WORKS HERE
            ******************************************/
            cout<<vars.number;
    }
    
    main()
    {
            info vars;
            input(vars);
            cout<<endl;
            /******************************************
                          BUT NOT HERE
            ******************************************/
            cout<<vars.number;
            getch();
    }

  7. #22
    Guest1
    Guest
    Sorry for doing the code tags wrong. But the code above should work for you now. The change is the:

    Code:
    void input(info& vars)

  8. #23
    Registered User dizz's Avatar
    Join Date
    Nov 2002
    Posts
    41
    yes finally thank you!

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

Similar Threads

  1. problem with functions
    By saliya in forum C Programming
    Replies: 1
    Last Post: 11-05-2007, 09:36 PM
  2. Problem with system(), execl(), etc. functions
    By nickkrym in forum C Programming
    Replies: 5
    Last Post: 05-12-2007, 08:04 AM
  3. Problem: Functions
    By Dmitri in forum C Programming
    Replies: 21
    Last Post: 11-06-2005, 09:40 AM
  4. Problem with pointers and functions
    By Kheila in forum C++ Programming
    Replies: 5
    Last Post: 10-13-2005, 12:40 PM
  5. Problem with functions
    By lizardking3 in forum C++ Programming
    Replies: 4
    Last Post: 09-22-2003, 04:34 PM

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