i have this program trying to read from a file but i keep getting this error message what am i doing i cant figure it out.

#include<iostream>
#include<cstdlib>
#include<cstring>
#include"assignment9.h"
#include<fstream>
using namespace std;

const int MAX_LEN = 30;

int numofRecords()
{

  int numRecords;

  cout <<"number of courses to process: ";
  cin >> numRecords;

  cin.ignore(999, '\n');

  return numRecords;
}

int getdata(std::fstream& fin)
{
  //  ifstream fin;
   char inputfilename[MAX_LEN + 1];

   cout << "Input filename(up to"<<MAX_LEN<<" characters): ";
   cin.get(inputfilename, MAX_LEN + 1, '\n');
   cin.ignore(999, '\n');


   fin.open(inputfilename, ios::in);
   if(fin.fail())

   {
     cout<<"Error opening input file"<< inputfilename;
     cout<<"press enter or return to continue...."<<endl;

     cin.get();

     exit(EXIT_FAILURE);
   }

    int numofRecords = 0;
    inventory records;

    fin >> ws;

    while(!fin.eof())
   {
     fin >> records.id;
     if(fin.eof())

   {
     cerr<<"Input file contains an incomplete record"<<endl;
     break;
   }

   fin.ignore(999,'\n');

   fin>>ws;

   fin.get(records.description, MAX_LEN +1,'\n');
   if(fin.eof())
   {

    cerr<<"inputfIle contains an incomplete record"<<endl;
    break;
   }

   fin.ignore(999,'\n');

   fin >>ws;

   fin >>records.wholesalePrice;

   if(fin.eof())
   {
    cerr<<"inputfile contains an incomplete record"<<endl;
    break;
   }
   fin.ignore(999,'\n');


   fin >> ws;
   fin >>records.retailPrice;
   if(fin.eof())
   {
    cerr << "inputfile contains an incomplete record"<<endl;
    break;
    }
   fin.ignore(999,'\n');


   fin >> ws;
   fin>>records.avgWeeklySales;
   if(fin.eof())
   {
    cerr << "inputfile contains an incomplete record"<<endl;
    break;
    }

   fin >> ws;
   fin >>records.quantityInStock;

   numofRecords ++;

    cout << records.id<< endl;
      cout << records.description;
      cout << records.wholesalePrice<< endl;
      cout << records.retailPrice<< endl;
      cout << records.avgWeeklySales<< endl;
      cout << records.quantityInStock << endl;
      cout << endl;
      cout << "# of courses scanned: " << numofRecords << endl;
      cout << endl;

  }

   fin.seekg(0);
   fin.clear();

   return numofRecords;
this the error i keep getting 
}

<< moderator edit: added [co[u][/u]de][/co[u][/u]de] tags >>

/usr/lib/gcc/i386-redhat-linux/3.4.4/../../../crt1.o(.text+0x18): In function `_start':
: undefined reference to `main'
collect2: ld returned 1 exit status

this the header file

#include<iostream>
#include<fstream>
#include<cstring>
#include<cstdlib>

struct inventory
{
   int id;
   char description[41];
   double wholesalePrice;
   double retailPrice;
   double avgWeeklySales;
   double quantityInStock;
};

int numofRecords();

int getdata(std::ifstream& fin);

#endif

<< moderator edit: fixed [co[u][/u]de][/co[u][/u]de] tags >>

Dani AI

Generated

Short answer: the linker is telling you it cannot find a main() function. That is the immediate reason for the error shown in the first post. As already noted, either there is no main() defined anywhere in the files you compiled, or you compiled only the file with your helpers and did not include the translation unit that contains main() when linking. Compile and link every .cpp file that is part of the program.

Do the following checklist (fast, in order):

  • Make sure there is a main() in one of your source files and that it is being compiled/linked.
  • Fix the header: remove the stray #endif and add a proper include guard so the header is valid and safe to include in multiple files.
  • Make the function declaration in the header match the definition in the .cpp exactly (stream types must match). If you want a simpler design, have getdata take a filename or a std::istream&.
  • Recompile everything together and use warnings to reveal problems: g++ -Wall -Wextra -o assignment9 main.cpp assignment9.cpp

Example header skeleton (use this pattern, not a verbatim copy of your posted header):

#ifndef ASSIGNMENT9_H
#define ASSIGNMENT9_H

#include <fstream>
#include <string>

struct inventory { /* fields */ };

int numofRecords();
int getdata(std::ifstream &fin);   // prototype must match the definition

#endif

If you prefer getdata to open the file itself, change its prototype to accept a filename (or std::string) so callers don't have to pass an un-opened stream. Also reconcile any buffer-size inconsistencies (your MAX_LEN vs. the description array size) or switch description to std::string to avoid overflow risks.

If problems persist, re-run the compile with full command lines here and post the exact compiler/linker output and the file list used for compilation; that will make it easy to spot missing objects or mismatched signatures.

Recommended Answers

All 2 Replies

i have this program trying to read from a file but i keep getting this error message what am i doing i cant figure it out.

It might be beneficial to post the error message. It might also be beneficial to post the header file.

You have a stray #endif in your header file. Perhaps you meant to add this onto the beginning:

#ifndef HEADER_H
#define HEADER_H 1
ad/usr/lib/gcc/i386-redhat-linux/3.4.4/../../../crt1.o(.text+0x18): In function `_start':
: undefined reference to `main'

Well, there you go. main() isn't defined.

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.