Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 08-29-2008, 09:48 PM   #1 (permalink)
The.Merridian
Recruit
 
Join Date: Aug 2008
Posts: 6
The.Merridian is on a distinguished road
Arrow *Confused* help please?

I literally just signed up because I'm rather tired of flipping through the stupid text book, so I'm going to admit I'm completely lost :/

I'm using Dev-C++ compiler and it's spitting back the " [Linker error] undefined reference to `getValue(int)' "

I can't figure out why or where my mistake is. It's an assignment for one of my classes. The object of the program is to use user defined functions and have the user input the current year, the current month, their birth year, birth month, and it will tell them how old they are in years and months.

Most of the code was written ahead of time and it's a fill in the blank sort of deal. I'm probably doing it completely wrong.

Code:
#include <iostream>
#include <string>
using namespace std;

// user-defined function prototypes
int getValue(int value = 0);
char getLetter(char letter = 'y');
int main()
{
    int thisYear, thisMonth, year, month, ageYear, ageMonth;
    char again = 'y';

   	cout<<"Enter the current year in 4 digit format: ";
   	cin>>thisYear;
        getValue(thisYear);	

	cout<<"Enter the number of the current month: ";
   	cin>>thisMonth;
        getValue(thisMonth);		

	while (again == 'y')
	{
        cout<<"Enter your birth year in 4 digit format: ";
   	cin>>year;
        getValue(year);
    	
	cout<<"Enter the number of your birth month: ";
   	cin>>month;
        getValue(month);

	ageYear = thisYear - year;
	ageMonth = thisMonth - month;

    if (thisMonth < month)
	{
	 	ageYear--;
	 	ageMonth += 12;
	}

	cout<<"\nYou are "<<ageYear<<" years and "<<ageMonth 
        <<"months old.\n";

        cout<<"Would you like to enter another birthday? (y/n)\t";
        cin>>again;
        getLetter(again);

	again = tolower(again);
    }
	
        system("pause");
	return 0;
}

int getValue(string message)
{
       int value;

       cout<<message;
	
	cin>>value;
	
        return value;
}

char getLetter(string message)
{
	char letter;

	cout<<message;
	
	cin>>letter;

	return letter;
}
The.Merridian is offline   Reply With Quote
Old 08-30-2008, 12:58 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
Look through your code very carefully focusing on your getValue() definition aswell as the function it self, then considder if theres a match between the two ie:
Will this give a hint
Code:
int getValue(int value = 0);
...
int getValue(string message)
{...
If not, keep on reading (requires marking the text in order to see)

Since your predefinition of the getValue() function is accepting an int as the argument, it isnt the same as your actual function deifinition which takes a string as the argument, when your main() function is parsed, the parser is knowing the existance of a getValue(int) function, but since theres no actual body of that function, it is never prevailed to teh linker what it actualy does, hence your error.

Since the getValue(int) is known uppon parse time, theres no problem in letting your main() require the
Code:
getValue(month);
since its argument is of type int, but once the linking is done it will either result in a segmentation fault, due to the body of the function relying on string type as argument, or a linkage error warning type mismatch.

You either have to redefine your predefinition of the functionint getValue(int); or change the body of the function, aswell as the reference in your main() function ie
Code:
thisYear = getValue("Enter the current year in 4 digit format: ")
But since your main() function has the
Code:
cout<<"Enter the current year in 4 digit format: ";
cin>>thisYear;
already, you dont need your getValue() function, so you need to decid if you want to change your design according to my suggestion, or get rid of the function itself.

Given that this probably is a lecture in use of calling functions I would suggest you take my advice and change the body of your function to fit your predefinition getValue(string), and change the main() accordingly.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 08-30-2008, 10:56 AM   #3 (permalink)
The.Merridian
Recruit
 
Join Date: Aug 2008
Posts: 6
The.Merridian is on a distinguished road
Thank you for your help.

I changed the function body like you suggested and now it's working
The.Merridian is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 05:42 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting