Thread: New to C++; not sure how to solve error regarding my code w/ functions

  1. #1
    Registered User
    Join Date
    May 2019
    Posts
    17

    New to C++; not sure how to solve error regarding my code w/ functions

    Hi all,

    I'm taking an introduction to C++ right now and I've been scratching my head over these errors. I'm not sure why my function is not being defined?

    The errors are: undefined reference to 'totalfare(char)' and [error] ID returned 1 exit status.

    Not looking for straight up answers but any guidance would be most appreciated. Thanks!



    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    
    double totalfare(char code);
    void output(double);
    
    
    
    
    int main()
    {
    	string flname;
    	int mins, miles;
    	char code, surge;
    	double surgem, totalcost;
    	
    	cout << "What is your first and last name?";
    	cin >> flname;
    	cout << "How long is your ride in minutes?";
    	cin >> mins;
    	cout << "How many miles is your ride?";
    	cin >> miles;
    	cout << "What is the code of the ride?";
    	cin >> code;
    	cout << "Is there a surge?";
    	cin >> surge;
    	if (surge == 'Y')
    		{
    			
    			cout << "What is the surge multiplier?";
    			cin >> surgem;
    			exit(0);
    		}
    
    
    	totalcost = totalfare(code);
    	output(totalcost);	
    	return 0;
    	
    }
    
    
    double totalfare(char code, char surge, double surgem, int mins, int miles)
    {
    	double totalcost;
    	
    	if (code == 'X')
    	{
    		if (surge == 'Y' && totalcost >= 6.55)
    			{
    				totalcost = (surgem * 2.00) + (mins * 0.22) + (miles * 1.15); 
    				return totalcost;
    			}	
    		else	
    			{
    				totalcost = 6.55;
    				return totalcost;
    			}
    	}
    	
    	else if (code == 'S')
    	{
    		if (surge == 'Y' && totalcost >= 25.00)
    			{
    				totalcost = (surgem * 15.00) + (mins * 0.90) + (miles * 3.75);
    				return totalcost;
    			}
    		else
    			{
    				totalcost = 25.00;
    				return totalcost;
    			}
    	}
    	
    	else if (code == 'L')	
    	{
    		if (surge == 'Y' && totalcost >= 10.55)
    			{
    				totalcost = (surgem * 5.00) + (mins * 0.50) + (miles * 2.75);
    				return totalcost;	
    			}
    		else
    			{
    				totalcost = 10.55;
    				return totalcost;
    			}
    	}
    	
    }
    
    
    void output(double totalcost)
    {
    	cout << "Your total cost is: " << totalcost << endl;
    	return;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,880
    Notice that you forward declared totalfare as having one parameter, but you defined it with many more parameters, and then you called it with only one argument. The forward declaration matches the function call, which is good, but it also needs to match the function definition.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2019
    Posts
    17
    Nevermind; I realized I didn't add my variables to the prototype in line 6 as well as the calculation in main in line 38.

    A follow up questions; it doesn't seem to be calculating my totalcost correctly; it always defaults to the else statement for each character condition. Is this because I am comparing the variable totalcost before it is even calculated?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,880
    Yes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2019
    Posts
    17
    Thanks for the response -

    I am trying make a condition where if the totalcost variable is less or more than the minimum total for each different case ('X', 'S', 'L').

    So I assume I would have to check for the total cost after it has executed which means that if it is less than the minimum total, it would return minimum total; if it is more than the minimum total, it would return the actual calculated totalcost.

    I am guessing that I would insert another "if/else" statement nested in after the initial totalcost is calculated? I will give this a try but I would appreciate input on my thought process here

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help??How to solve this error??
    By i++ in forum C Programming
    Replies: 17
    Last Post: 08-08-2013, 11:06 PM
  2. How to solve this error
    By vin_pll in forum Windows Programming
    Replies: 3
    Last Post: 01-12-2010, 05:40 PM
  3. how to solve this error msg
    By rhythm in forum C++ Programming
    Replies: 11
    Last Post: 02-08-2008, 10:05 PM
  4. error cant solve
    By developer47 in forum C Programming
    Replies: 1
    Last Post: 04-30-2007, 05:10 AM
  5. Odd error i cant solve
    By c++.prog.newbie in forum C++ Programming
    Replies: 0
    Last Post: 09-15-2005, 09:08 AM

Tags for this Thread