Problem with Functions!

This is a discussion on Problem with Functions! within the C++ Programming forums, part of the General Programming Boards category; Why wont this work?? It shows the right answer in the input function but some wierd number in the main... ...

  1. #1
    dizz
    Guest

    Problem with Functions!

    Why wont this work?? It shows the right answer in the input function but some wierd number in the main... any ideas?

    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();
            cout<<endl;
            /******************************************
                          BUT NOT HERE
            ******************************************/
            cout<<vars.number;
            getch();
    }
    I also tried declaring the structure globally and still it works in the input function but gives me a zero in the main. Any help would be greatly appreciated.

    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;
    };
    
    info vars;
    
    void input(info vars)
    {
            cout<<"Enter a number: ";
            cin>>vars.number;
            /******************************************
                          IT WORKS HERE
            ******************************************/
            cout<<vars.number;
    }
    
    main()
    {
            input(vars);
            cout<<endl;
            /******************************************
                          BUT NOT HERE
            ******************************************/
            cout<<vars.number;
            getch();
    }

  2. #2
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    It works if you take out the arguements...also, why not put input()'s code in main()?
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Simple. you output the number twice when you dont need to.

    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; //YOU ONLY OUTPUT NUMBERHERE IN FUNCTION
    }
     main()
    {       info vars;
            input();
            cout<<endl;
            /******************************************
                          BUT NOT HERE
            ******************************************/
            getch();
            //cout<<vars.number; //YOU DONT NEED THIS LINE
    
    }
    When you put cout<<vars.number in main you are reprinting the numebr you already printed in your input function.

    HOPE THAT HELPS!

  4. #4
    Registered User dizz's Avatar
    Join Date
    Nov 2002
    Posts
    41
    first of all which one works?
    second becuase the project im working on must be slpit up into functions.

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    THIS SHOULD BE YOUR FINAL CODE
    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;
            cout<<vars.number;
    }
     main()
    {       info vars;
            input();
            cout<<endl;
            getch();
           
    }
    Check it for yourself.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  6. #6
    Registered User dizz's Avatar
    Join Date
    Nov 2002
    Posts
    41
    Originally posted by gamer4life687
    Simple. you output the number twice when you dont need to.

    When you put cout<<vars.number in main you are reprinting the numebr you already printed in your input function.

    HOPE THAT HELPS!
    Actually i only want to display it in the main which still does not work if i take out the cout<<vars.number in the input function

    keep the possible sollutions coming!

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Look your are printing it main when you call the input function. That function does it for you. and that code does work i tried it myself.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  8. #8
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    As a matter of fact you can even delete this line

    Code:
    main()
    {       info vars;//DONT NEED THIS IN MAIN
            input();
            cout<<endl;
            getch();
           
    }
    becomes
    Code:
    main()
    {       
            input();
            cout<<endl;
            getch();
           
    }
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  9. #9
    Registered User dizz's Avatar
    Join Date
    Nov 2002
    Posts
    41
    yeah i know what you are saying but it is not that tho.
    i must call the variable from the structure into main.

    im damn sure ya can do it since i my teacher did it in class today.

  10. #10
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Do you need to use the function input() then because its purpuse is to print the line. I can help you if you dont need to use input().
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  11. #11
    Registered User dizz's Avatar
    Join Date
    Nov 2002
    Posts
    41
    hmm okay what i need to do is write an app that asks the comp to guess a random number. the app has to be split up into 3 functions and the main and the variables must be stored in the structure.

    it has to be set up like this

    struct

    inputFunction()

    randomFunction()

    outputFunction()

    main()

    i dont know if you understand what im talking about but thats pretty much what i am trying to acomplish, but first i am just trying to figure out how to send variables to the structure and then display them in a different function.

  12. #12
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    here is the reason why it does not print the right answer in main to the screen.

    when you call input, it reserves memory for its own info.

    however, when you are to call it in main, it will not alter main's 'info vars' because in input, 'info vars' is a local variable for that function.

    for example...

    Code:
    void func()
    {
            int x;
    
            cin >> x;
            cout << x;
    }
    
    //if you enter 10 for x here, it will output 10.
    
    int main()
    {
           int x;
    
           cout << x;
    }
    //it will NOT output 10 here because x in main is local to main.  they are two different memory locations, not the same memory.
    
    //I'm sure you already know this and just forgot...

  13. #13
    gamer4life688
    Guest
    alpha i sort of already established that i know thats its different output and i know how to output it. The only prob is i dont know how he wanted to and know i do but i have to get off right now.
    Sorry hope someone else will help you!!

  14. #14
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Oops i put my wrong user name lol

  15. #15
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    gamers...I know, but with his initial code, thats the way it was setup

Page 1 of 2 12 LastLast
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, 10:36 PM
  2. Problem with system(), execl(), etc. functions
    By nickkrym in forum C Programming
    Replies: 5
    Last Post: 05-12-2007, 09:04 AM
  3. Problem: Functions
    By Dmitri in forum C Programming
    Replies: 21
    Last Post: 11-06-2005, 10:40 AM
  4. Problem with pointers and functions
    By Kheila in forum C++ Programming
    Replies: 5
    Last Post: 10-13-2005, 01:40 PM
  5. Problem with functions
    By lizardking3 in forum C++ Programming
    Replies: 4
    Last Post: 09-22-2003, 05:34 PM

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