Undefined Reference Help

Read data from a file into an array

Page 1 of 1

7 Replies - 2605 Views - Last Post: 05 August 2007 - 08:49 AM Rate Topic: -----

#1 trl10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-August 07

Undefined Reference Help

Post icon  Posted 04 August 2007 - 06:41 PM

Hi Everyone,
I'm writing my last program for class and I'm getting an error (actually several) that say
In function `main':
sample.cpp:53: undefined reference to `sumArray(double const*, double)'
sample.cpp:57: undefined reference to `avgArray(double const*, double)'
sample.cpp:61: undefined reference to `findHigh(double const*, double)'
sample.cpp:65: undefined reference to `findLow(double const*, double)'
collect2: ld returned 1 exit status
Here is what I have coded so far. Any suggestions would be greatly appreciated'
#include <iostream>
#include <fstream>

using namespace std;

//Constant array size declaration.
const int LOCATIONS = 20;

//Function Prototypes
void readData(double a[], int size, double& data);
double avgArray(const double a[], double data);
double sumArray(const double a[], double data);
double findHigh(const double a[], double data);
double findLow(const double a[], double data);

int main()
{ 
   //Variable declarations
   ifstream in; 
   char ans, in_file[256];
   double data;
   double avgSales, highSales, lowSales, totalSales, salesData[LOCATIONS];
   
  
   //Prompts the user for the input file name.
   cout << "Enter the input file name:\n";
   cin >> in_file;
   
   // Opens the stream and connects to the file.
   in.open(in_file);
   
   //Checks to see if the input file opened properly.
   //Displays an error message if  file not opened.
   if(in.fail( ))
   {
	  cout << "Input file opening failed.\n";
	  exit(1);
   }
   
   do
   //do-while: Tells what the loop body should be
   //and how many times the body should iterate.
   {	
	  cout << endl << "N or n quits the program, any other character repeats\n"
		   << "the process: ";
	  cin >> ans;
	}while ((ans != 'N') && (ans != 'n'));
	//while loop - Used to determine if the user wants to continue.
	//If true the user will continue.
	  cout << endl;
	   
   //Finds and prints the total sales.
   totalSales = sumArray(salesData, data);
   cout << endl << "The total sales are $" << totalSales << endl;

   //Finds and prints the average sales amount.
   avgSales = avgArray(salesData, data);
   cout << "The average sales amount is $" << avgSales <<endl;

   //Finds and prints the highest sales amount.
   highSales = findHigh(salesData, data);
   cout << "The highest sales amount is $" << highSales << endl;

   //Finds and prints the lowest sales amount.
   lowSales = findLow(salesData, data);
   cout << "The lowest sales amount is $" << lowSales << endl;

   return 0;
}

//Total sales function.
double sumArray(double sales[], double n)
{
   double sum = 0;

   for(int count = 0; count < n; count++)
  
   sum += sales[count];
  
   return(sum);
}

//Average sales function.
double avgArray(double sales[], double n)
{
   return((double)sumArray(sales, n)/n);
}

//Highest sales function.
double findHigh(double sales[], double n)
{
   double highest;
   
   highest = sales[0];
   
   for(int count = 1; count < n; count++)
   
   if(highest < sales[count])
	  highest = sales[count];
   
   return(highest);
}

//Lowest sales function.
double findLow(double sales[], double n)
{
   double lowest;
   
   lowest = sales[0];
   
   for(int count = 1; count < n; count++)
  
   if(lowest > sales[count])
	   lowest = sales[count];
   
   return(lowest);
}

//Read data function.
void readData(int a[], int size, double& data)
{
	 //Still in the process of figuring this out   
}

Thanks

This post has been edited by trl10: 05 August 2007 - 03:23 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Undefined Reference Help

#2 Xing  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • Posts: 725
  • Joined: 22-July 06

Re: Undefined Reference Help

Posted 04 August 2007 - 07:54 PM

There's no automatic conversion from const to non-const. You are missing const in arguments while defining the functions.
Was This Post Helpful? 0
  • +
  • -

#3 trl10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-August 07

Re: Undefined Reference Help

Posted 04 August 2007 - 09:51 PM

View PostXing, on 4 Aug, 2007 - 07:54 PM, said:

There's no automatic conversion from const to non-const. You are missing const in arguments while defining the functions.

When I put const in my functions, I get an exception error ( 89 [main] a 5028 handle_exceptions : Exception : STATUS_ACCESS_VIOLATION 1045 [main] a 5028 open_sackdumpfile: Dumping stack trace to a.exe.stackdump). I know that I'm probably going about this all wrong.
Is there anything else that you can see that I may need to change. Maybe go about writing my code differently, or do you think I'm on the right track? Thanks for the tips.
Was This Post Helpful? 0
  • +
  • -

#4 Xing  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • Posts: 725
  • Joined: 22-July 06

Re: Undefined Reference Help

Posted 04 August 2007 - 10:42 PM

Post your modified code.
Was This Post Helpful? 0
  • +
  • -

#5 trl10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-August 07

Re: Undefined Reference Help

Posted 05 August 2007 - 07:35 AM

I've been playing around with it. Here is what I have now. I can't get doubles to work and now I can't get anything to work.
#include <iostream>
#include <fstream>

using namespace std;

//Constant array size declaration.
const int LOCATIONS = 20;

//Function Prototypes
void readData(int a[], int size, int& data);
double avgArray(int a[], int data);
int sumArray(int a[], int data);
int findHigh(int a[], int data);
int findLow(int a[], int data);

int main()
{ 
   //Variable declarations
   ifstream in; 
   char ans, in_file[256];
   int data;
   double  avgSales;
   int highSales, lowSales, totalSales, salesData[LOCATIONS];
   
  
   //Prompts the user for the input file name.
   cout << "Enter the input file name:\n";
   cin >> in_file;
   
   // Opens the stream and connects to the file.
   in.open(in_file);
   
   //Checks to see if the input file opened properly.
   //Displays an error message if  file not opened.
   if(in.fail( ))
   {
	  cout << "Input file opening failed.\n";
	  exit(1);
   }
   
		  
   //Finds and prints the total sales.
   totalSales = sumArray(salesData, data);
   cout << endl << "The total sales are $" << totalSales << endl;

   //Finds and prints the average sales amount.
   avgSales = avgArray(salesData, data);
   cout << "The average sales amount is $" << avgSales <<endl;

   //Finds and prints the highest sales amount.
   highSales = findHigh(salesData, data);
   cout << "The highest sales amount is $" << highSales << endl;

   //Finds and prints the lowest sales amount.
   lowSales = findLow(salesData, data);
   cout << "The lowest sales amount is $" << lowSales << endl;

   return 0;
}

//Total sales function.
int sumArray(int sales[], int n)
{
   int sum = 0;

   for(int count = 0; count < n; count++)
  
   sum += sales[count];
  
   return(sum);
}

//Average sales function.
double avgArray(int sales[], int n)
{
   return((double)sumArray(sales, n)/n);
}

//Highest sales function.
int findHigh(int sales[], int n)
{
   int highest;
   
   highest = sales[0];
   
   for(int count = 1; count < n; count++)
   
   if(highest < sales[count])
	  highest = sales[count];
   
   return(highest);
}

//Lowest sales function.
int findLow(int sales[], int n)
{
   int lowest;
   
   lowest = sales[0];
   
   for(int count = 1; count < n; count++)
  
   if(lowest > sales[count])
	   lowest = sales[count];
   
   return(lowest);
}

//Read data function.
void readData(int a[], int size, int& data)
{
		
}

I'm using jGrasp with cygwin and when I run the code in the MSDOS window I get the exception error, but when I don't it gives me this
The total sales are $1984877205
The average sales amount is $-1.26907e+06
The highest sales amount is $2090320320
The lowest sales amount is $-1923614696
Was This Post Helpful? 0
  • +
  • -

#6 trl10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-August 07

Re: Undefined Reference Help

Posted 05 August 2007 - 07:44 AM

Now with this code the same thing happens when I run it in MSDOS window(exception and the stackdump)
#include <iostream>
#include <fstream>

using namespace std;

//Constant array size declaration.
const int LOCATIONS = 20;

//Function Prototypes
void readData(double a[], int size, double& data);
double avgArray(const double a[], double data);
double sumArray(const double a[], double data);
double findHigh(const double a[], double data);
double findLow(const double a[], double data);

int main()
{ 
//Variable declarations
ifstream in; 
char ans, in_file[256];
double data;
double avgSales, highSales, lowSales, totalSales, salesData[LOCATIONS];


//Prompts the user for the input file name.
cout << "Enter the input file name:\n";
cin >> in_file;

// Opens the stream and connects to the file.
in.open(in_file);

//Checks to see if the input file opened properly.
//Displays an error message if file not opened.
if(in.fail( ))
{
cout << "Input file opening failed.\n";
exit(1);
}


//Finds and prints the total sales.
totalSales = sumArray(salesData, data);
cout << endl << "The total sales are $" << totalSales << endl;

//Finds and prints the average sales amount.
avgSales = avgArray(salesData, data);
cout << "The average sales amount is $" << avgSales <<endl;

//Finds and prints the highest sales amount.
highSales = findHigh(salesData, data);
cout << "The highest sales amount is $" << highSales << endl;

//Finds and prints the lowest sales amount.
lowSales = findLow(salesData, data);
cout << "The lowest sales amount is $" << lowSales << endl;

return 0;
}

//Total sales function.
double sumArray(const double sales[], double n)
{
double sum = 0;

for(int count = 0; count < n; count++)

sum += sales[count];

return(sum);
}


//Average sales function.
double avgArray(const double sales[], double n)
{
return((double)sumArray(sales, n)/n);
}

//Highest sales function.
double findHigh(const double sales[], double n)
{
double highest;

highest = sales[0];

for(int count = 1; count < n; count++)

if(highest < sales[count])
highest = sales[count];

return(highest);
}

//Lowest sales function.
double findLow(const double sales[], double n)
{
double lowest;

lowest = sales[0];

for(int count = 1; count < n; count++)

if(lowest > sales[count])
lowest = sales[count];

return(lowest);
}

//Read data function.
void readData(int a[], int size, double& data)
{
//Still in the process of figuring this out 
}

This is the output when I don't run it in MSDOS
The total sales are $5.26382e-308
The average sales amount is $9577.59
The highest sales amount is $5.26382e-308
The lowest sales amount is $5.26382e-308


Which of the two is the better code or should I say which one am I on the right track with?
Was This Post Helpful? 0
  • +
  • -

#7 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Undefined Reference Help

Posted 05 August 2007 - 08:25 AM

You have not initialized any variables...where are you looking to get the values from? A file? The user?
Was This Post Helpful? 0
  • +
  • -

#8 trl10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-August 07

Re: Undefined Reference Help

Posted 05 August 2007 - 08:49 AM

View PostAmadeus, on 5 Aug, 2007 - 08:25 AM, said:

You have not initialized any variables...where are you looking to get the values from? A file? The user?

You said that I haven't initialized any variables, isn't this my initilized variables?
ifstream in; 
   char ans, in_file[256];
   int data;
   double  avgSales;
   int highSales, lowSales, totalSales, salesData[LOCATIONS];


We are to write a program that reads sales data from a file. The progam is suppose to output the total sales, average sales, highest sales, and lowest sales.
Outcomes:
1. Read data from a file into an array.
2. Process data in a partially filled array via functions.
3. Pass an array as a parameter to a function.
4. Use for and while loops when processing an array.

Specifications:
1. Use an array to store the sales data. Sales data should be stored as a double.

2. The company has at most 20 locations. (Max capacity of the array)

3. The input file contains a sales report that list the number of stores reporting data and the sales data.

4. Sample Input file format:
3
27648.92 234.83 23458.32
The 3 represent that this reports contains 3 locationís sales data. (size of the partially filled array)
The numbers 27648.92 234.83 23458.32 represent the sales for three location.

5. The main function should be modular so the main tasks must be handled by functions. You should notice that only an outline of the functionís requirements is provided. You will need to decide on function names, parameter list and return types on your own.

6. The tasks are:
a. Ask the user for the file name that store the sales data. Fill the array with the sales data from a file.
b. Total the sales data.
c. Average the sales data.
d. Determine the highest sales in the report.
e. Determine the lowest sales from the report

7. The main function should output the data to the user.

8. Sample output:
Enter file name: sales.dat
The total sales are $755260.00
The average sales amount is $ 75526.00
The highest sales amount is $124569.00
The lowest sales amount is $ 35678.00

9. Add code to the main function to allow the user to rerun the program.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1