Thread: Newbie needs advice please..

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    3

    Newbie needs advice please..

    Hi,

    I have just started learning C++ and while messing around with calling and returning I have unexpected results.

    As far as I understood it I should be able to use the values returned from other functions outside of 'main' and use them as I would any 'int' value as shown below.

    But it loops through the program twice for some reason and then doesn't display the user input information as I thought it should.

    Any pointers would be appreciated.

    Code:
    #include "stdafx.h"
    #include <stdlib.h>
    #include <iostream>
    
    using namespace std;
    
    int age() 
    {
    	int ageyouare;
    	cout << "Please enter your age: ";
    	cin >> ageyouare;
    	cin.ignore();
    	system("cls");
    	return ageyouare;
    }
    
    
    int yearofbirth() 
    {
    	int year;
    	cout << "Please enter the year you were born: ";
    	cin >> year;
    	cin.ignore();
    	system("cls");
    	return year;
    }
    
    int main()
    {
    	int y;
    	int a;
    	yearofbirth();
    	age();
    	y = yearofbirth();
    	a = age();
    	cout << "You are: " << a << "\n";
    	cout << "And you were born in the year: " << y << "\n";
    		
    	char f;
    	cin >> f;
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    It's not looping through the program twice. You are calling yearofbirth and age twice in main, but in the first call to each function you are not assigning the return value to anything. Delete those two useless lines and the program will work as you were expecting.

  3. #3
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    what do you mean it loops twice? You aren't using any loops?
    the reason it "loops" twice is because you called the functions twice

    age()
    yearofbirth()

    and then

    a= age()
    y=yearofbirth()

    You were nearly right.

    just delete the "age()" and "yearofbirth()" and you should the results you want.
    You ended that sentence with a preposition...Bastard!

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    3
    Thanks alot!

    I thought I had to seperately run the functions and THEN assign the values from those to y and a.

    Which I now see was not needed!


    cheers


  5. #5
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by Vortigon View Post
    Thanks alot!

    I thought I had to seperately run the functions and THEN assign the values from those to y and a.

    Which I now see was not needed!


    cheers

    nope, when you return a value from a function it goes like this

    Code:
    a= age() //also the same as 
    a= 19
    //if age() returns 19. that is why functions always push the return address to the stack
    //when they are called. to return to that line "address"(number) and possibly assign values
    You ended that sentence with a preposition...Bastard!

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    3
    Thanks for the replies that makes it much clearer now.

    I can see this becoming an addiciton.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie variable advice
    By 182 in forum C++ Programming
    Replies: 12
    Last Post: 02-15-2006, 06:08 PM
  2. Advice for newbie...
    By CompiledMonkey in forum C Programming
    Replies: 2
    Last Post: 06-12-2003, 06:18 PM
  3. newbie needs advice!
    By bluenoser in forum C Programming
    Replies: 24
    Last Post: 10-16-2002, 07:28 PM
  4. newbie needs some advice
    By werdy666 in forum C++ Programming
    Replies: 0
    Last Post: 09-15-2002, 10:56 PM
  5. Newbie: Seek advice
    By gogo in forum C++ Programming
    Replies: 4
    Last Post: 11-06-2001, 10:04 AM