Hi,

I'm trying to find the resultant of a given data using fstream. I made the program and everything and I believe its all correct however, when I try to run it, I'm getting a linker error saying "undefined reference to Resultant(const double...)" which I can't figure out how to fix it. Can you please help me spot the problem?


Thank you in advance.


This is the program:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>
#include <cmath>

using namespace std;

void Resultant(const double[], const double[], const double[], const double[], const int,
                double&, double&, double&);

const double PI=acos(-1.0);
int main()
{
    const int size = 200 ;
    int moves = 0;
    double Magnitude[size], X_axis[size], Y_axis[size], Z_axis[size];
    double X_Resultant=0, Y_Resultant=0, Z_Resultant=0;
    string choice, answer;
    
    cout << "This program will calculate the Resultant Forces from a "
         << "selected data sheet.";
         
        do 
        {      
    cout << "\n\nPlease type in the data sheet you want to be calculated?"
         << "\nEnter Force1 or Force2 : " ;
    cin >> choice;
                
    if (choice == "Force1" || "force1")
    {
                        ifstream Data("Forces1.DAT");
 
                                  if (Data.fail())
                                     cout <<"\nFail To Open\nCheck File" << endl;
 
           for ( int i=0; !Data.eof();i++)
               {
                  Data >> Magnitude[i] >> X_axis[i] >> Y_axis[i] >> Z_axis[i];
                   moves++;
                                   }     
                     Data.close();   break;
     }
    
    else if (choice == "Force2" || "force1")
    {
                        ifstream Data("Forces2.DAT");
 
                                  if (Data.fail())
                                     cout <<"\nFail To Open!\nCheck File" << endl;
 
                  for ( int i=0; !Data.eof();i++)
                  {
        Data >> Magnitude[i] >> X_axis[i] >> Y_axis[i] >> Z_axis[i];
        moves++;
                                   }     
        Data.close(); break;
     }
     
            else {
                     cout << "\nERROR! Invalid Input! \nWould you like to"
                          << " try again? Enter y or n : " ;
                      cin >> answer;
                          }
}
     while (answer == "y" || answer == "Y");                   
     
     Resultant(Magnitude, X_axis, Y_axis, Z_axis, moves, X_Resultant, Y_Resultant, Z_Resultant);
     
     cout << setw(6) << X_Resultant << setw(6) << Y_Resultant << setw(6) << Z_Resultant;
     
system("pause");
return 0;
}

void Resultant(const double M[], const double X[], const double Y[], double Z[],
const int moves, double& Rx, double& Ry, double& Rz)
 {
      double Fx[moves], Fy[moves],Fz[moves];
        for (int i=0; i<3; i++) // using 3 to test the program
        {
        Fx[i]= M[i]*cos(X[i]*180/PI);
        Fy[i]= M[i]*cos(Y[i]*180/PI);
        Fz[i]= M[i]*cos(Z[i]*180/PI);
        }

        for (int i=0; i<3; i++)
        {
            Rx += Fx[i];
            Ry += Fy[i];
            Rz += Fz[i];
        }
        
 }

Declaration

void Resultant(const double[], const double[], const double[], [B]const[/B] double[], const int, double&, double&, double&);

does not match the definition

void Resultant(const double M[], const double X[], const double Y[], [B]/* ??? */[/B] double Z[], const int moves, double& Rx, double& Ry, double& Rz)
{
   // ...

Not a great idea, writing a function with eight(!) parameters.

Edited 5 Years Ago by vijayan121: n/a

check the implementation of your Resultant function. You forgot the const on Z[].

vij beat me to it!

Edited 5 Years Ago by fibbo: n/a

wow you guys are right. silly me. haha. what is the best way to pass 8 variables to a function? just curious.

Well on the top of my head I have 2 "solutions" for passing less variables.

1 defining the variables global
or passing a vector of doubles and a vector of arrays of doubles.

Take this with a big grain of salt and wait for a competent coder with his opinion. I just try to get some opinions as well (because on another project I work on I pass alot of variables as well).

i see but this project is recording numbers from a file thats why im passing a lot of variables. hehe. your right though it's unsafe.

i have a problem with my program :( . i don't know how or what happen but suddenly it doesn't go to "else" statement saying "try again" if i type in random words, it just goes to the first "if". my "else if" and "if" statements records the right file when i type in the correct word phrase. any idea why this is happening?

this is the do while loop:

do{      
                        
    cout << "\n\nPlease type in the data sheet you want to be calculated?"
         << "\nEnter Forces1 or Forces2 : " ;
    cin >> file;
    
      
    if (file == "Forces1" || "forces1")
    {
                        ifstream Data("Forces1.DAT");
 
                                  if (Data.fail())
                                     cout <<"\nFail To Open! Check File If Created" << endl; 
                 
           for ( int i=0; !Data.eof();i++)
               {
                  Data >> Magnitude[i] >> X_axis[i] >> Y_axis[i] >> Z_axis[i];
                   moves++;
                                   }     
                     Data.close(); 
     }
    
    else if (file == "Forces2" || "forces1")
    {
                        ifstream Data("Forces2.DAT");
 
                                  if (Data.fail())
                                     cout <<"\nFail To Open! Check File If Created" << endl; 
 
                  for ( int i=0; !Data.eof();i++)
                  {
        Data >> Magnitude[i] >> X_axis[i] >> Y_axis[i] >> Z_axis[i];
        moves++;
                                   }     
        Data.close(); 
     }
     
            else {
                     cout << "\nERROR! Invalid Input! \nWould you like to"
                          << " try again? Enter y or n : " ;
                      cin >> answer;
                          }
}
     while (answer == "y" || answer == "Y");
if (file == "Forces1" || "forces1")

must be

if(file == "Forces1" || file == "forces1")

otherwise, in the first instance it's evaluating "forces1" as true since it is not null, so your statement's always true.

Same thing for the else if condition, too.

Edited 5 Years Ago by jonsca: n/a

thank you for pointing that out. i got it to work because of you guys.

This article has been dead for over six months. Start a new discussion instead.