I know I'm not the best with functions in C++ and that's probally why I'm having problems, but anyways I keep getting these errors:
[linker error] undefined reference to 'Area(float,float)'
[linker error] undefined reference to 'Circumference(float,float)'
[linker error] undefined reference to 'Diameter(float,float)'

I also get a message that says: Id returned 1 exit status.
I have not the slightest idea what that means.

Here's the code.

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

 void Area(float radius, float answer); 
 void Circumference(float radius, float answer);
 void Diameter(float radius, float answer);

int main()
{
    
    void circle_vals(char type, float radius, float answer);
    
          string prompt;
          char type;
          float  radius;
          float answer;

          
          prompt= "Please enter the problem type or exit command." ;
          
   cout <<"Please enter the problem type you want then the radius." ;
  cout << "Enter A for area, D for diameter, C for circumference, or E 0 to exit.";
  cout << "For example you would have A 2.2 to find an area with a 2.2 radius" <<endl;
          cin>> type;
          cin >> radius;
          while (type != 'E')
                {
                      if (type != 'A' || type != 'C' || type != 'D')
                       {
                       cout << "That's not a valid problem type" <<endl;
                       }
                       if (type =='A')
                       {
                       Area(radius, answer);
                       cout << answer <<endl;      
                       }
                      if (type =='C')
                       {
                       Circumference(radius, answer);
                       cout << answer <<endl;       
                       }
                       if (type =='D')
                       {
                       Diameter(radius, answer);  
                    cout << answer <<endl;       
                     }
                cout << prompt <<endl;
                cin >> type;
                cin >> radius ;
                }
                
                    if (type == 'E' )
                    {
                    cout << "Thank you.";
                    }
                
          void Area(float radius, float answer);
         answer=0;
          {
          answer = (radius * radius* 3.14) ;
          }
          
          void Circumference(float radius, float answer);
          answer=0;
          {
          answer = (2.0 * radius* 3.14) ;
          }
          
          void Diameter(float radius, float answer);
          answer=0;
          {
          answer = (2 * radius) ;
          }
        
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Recommended Answers

All 6 Replies

You are getting those errors because you didn't write those functions yet. The three lines just before main() are not functions, they are just prototypes, which tell the compiler about the return value and parameters. You still have to write all the code that makes them do something.

You are getting those errors because you didn't write those functions yet. The three lines just before main() are not functions, they are just prototypes, which tell the compiler about the return value and parameters. You still have to write all the code that makes them do something.

Okay, I get the prototype thing but aren't they defined in my coding just after my while statement?

void Area(float radius, float answer);
         answer=0;
          {
          answer = (radius * radius* 3.14) ;
          }
          
          void Circumference(float radius, float answer);
          answer=0;
          {
          answer = (2.0 * radius* 3.14) ;
          }
          
          void Diameter(float radius, float answer);
          answer=0;
          {
          answer = (2 * radius) ;
          }

You don't want these functions inside your main function. Try designing your program like this:

// code to include libraries

using namespace std;

// function prototypes

int main ()
{
      // code for main
}

// Put functions here

I would agree with Vernon. AD helped me out quite a bit in this respect. What you can do is create a separate cpp file for your functions if you don't want to do it on the same "page" as your main. The way I have been doing it (yes it may be going too far) is to create a header file:

myprogramheader.h
in here are my includeguards
in here are my prototypes

#ifndef NAMEINP_H
#define NAMEINP_H
 
 char getlast( char lastName[] , int maxin);
 char getfirst( char firstName[] , int maxin);
 char getmid( char midName[] , int maxin);
 void punchmeinthehead();
 
 #endif

Then I create a cpp file to hold my functions
myprogramfunctions.cpp
in here are all the functions I want to call in main

(Toggle Plain Text) 
#include "nameinp.h"
#include <iostream>
#include <string.h>
using namespace std;

//Input last name
void getlast( char lastName[] , int maxin)
{
	cout << " Please enter your last name up to 15 characters " << endl;
	cin.getline(lastName, maxin, '\n' );
	cin.ignore (cin.rdbuf()->in_avail(), '\n');
}


//Input first name
void getfirst( char firstName[] , int maxin)
{
	cout << " Please enter your first name up to 15 characters " << endl;
	cin.getline(firstName, maxin, '\n' );
	cin.ignore (cin.rdbuf()->in_avail(), '\n');
}


//and so on with the other individual functions

Then I create a cpp file for my main
mymainprogram.cpp
function call
function call
function call

void main()
{
             //declarations
	const int maxin = 16;
	char lastName[maxin];
         char firstName[maxin]; 
	char midName[maxin];

              //function calls	
	getlast(lastName, maxin);
         getfirst(firstName, maxin);
         getmid(midName, maxin);
	punchmeinthehead();
}

I know it all seems a bit separated and more effort than necessary for what you are doing, but it works really well for a code a little compile a little mentality. Not to mention once your functions are correct and you know they are correct, all you have to deal with is your main.

My errors have really all been small ones that I've blown out of proportion with my own inexperience, but with the help of these folks (AD, Sarehu, Vernon, Vij) it's getting me thinking more about how the code actually works and the theory behind it. Separating all this helps me work with my functions, pass them around, and get used to doing things a little at a time. Hope it helps

Ah, okay. Thank you.

Creating separate *.cpp files will work, but I think its overkill in your project. And unless you were specifically instructed to create separate files I don't think you should do it. All you have to do is write those functions after the end of main()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.