J. Flores
J. Flores

Reputation: 11

Reading file data into a struct and then sorting said data

My assignment is to read data in from a txt file and then sort it into an array. I've the basis of the code setup and I'm getting issues from the command line that don't necessarily recognize or are even for.
Here is my code:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

struct info{
  int number;
  string name;
  double salary;
};
int main() {
    ifstream file("input.txt");
    string st;
    int i = 0, j, swaps = 1,x;
    info items[10];
    file.open("C:/Users/user/Desktop/Prog-1/input.txt");
    if(!file){
      cerr << "Unable to open file datafile.txt" << endl;
      exit(1);
    }
    while (file >> x){
      //  echo input
        for(j = 0; j < i; j++){
          cout << items[i].number << items[i].name << items[i].salary << endl;
        }
        int swaps = 1;
        while(swaps == 1){
          swaps = 0;
          for(j = 0; j < i-1; j++){
            if(items[j].name > items[j+1].name)
              swap(items[j], items[j+1]);
              swaps = 1;
          }
        }
        //  echo input
        for(j = 0; j < i; j++){
          cout << items[i].number << items[i].name << items[i].salary << endl;
        }
        while(swaps = -1){
          swaps = 0;
          for(j = 0; j < i-1; j++){
            if(items[j].salary < items[j+1].salary)
              swap(items[j], items[j+1]);
              swaps = 1;
          }
        }
    }
    file.close();
  }

Then here is what is showing up in my console:

c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\javie\AppData\Local\Temp\ccIDnsGB.o:hw7.cpp:(.text+0x33): undefined reference to `std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(char const*, std::_Ios_Openmode)'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\javie\AppData\Local\Temp\ccIDnsGB.o:hw7.cpp:(.text+0x43): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string()'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\javie\AppData\Local\Temp\ccIDnsGB.o:hw7.cpp:(.text+0x8d): undefined reference to `std::basic_ifstream<char, std::char_traits<char> >::open(char const*, std::_Ios_Openmode)'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\javie\AppData\Local\Temp\ccIDnsGB.o:hw7.cpp:(.text+0xa0): undefined reference to `std::basic_ios<char, std::char_traits<char> >::operator!() const'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\javie\AppData\Local\Temp\ccIDnsGB.o:hw7.cpp:(.text+0xb3): undefined reference to `std::cerr'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\javie\AppData\Local\Temp\ccIDnsGB.o:hw7.cpp:(.text+0xb8): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\javie\AppData\Local\Temp\ccIDnsGB.o:hw7.cpp:(.text+0xbf): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\javie\AppData\Local\Temp\ccIDnsGB.o:hw7.cpp:(.text+0xc6): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\javie\AppData\Local\Temp\ccIDnsGB.o:hw7.cpp:(.text+0xeb): undefined reference to `std::istream::operator>>(int&)'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\javie\AppData\Local\Temp\ccIDnsGB.o:hw7.cpp:(.text+0xfe): undefined reference to `std::basic_ios<char, std::char_traits<char> >::operator bool() const'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\javie\AppData\Local\Temp\ccIDnsGB.o:hw7.cpp:(.text+0x13a): undefined reference to `std::cout'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\javie\AppData\Local\Temp\ccIDnsGB.o:hw7.cpp:(.text+0x13f): undefined reference to `std::ostream::operator<<(int)'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\javie\AppData\Local\Temp\ccIDnsGB.o:hw7.cpp:(.text+0x168): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\javie\AppData\Local\Temp\ccIDnsGB.o:hw7.cpp:(.text+0x18b): undefined reference to `std::ostream::operator<<(double)'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\javie\AppData\Local\Temp\ccIDnsGB.o:hw7.cpp:(.text+0x195): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\javie\AppData\Local\Temp\ccIDnsGB.o:hw7.cpp:(.text+0x19c): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\javie\AppData\Local\Temp\ccIDnsGB.o:hw7.cpp:(.text+0x292): undefined reference to `std::cout'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\javie\AppData\Local\Temp\ccIDnsGB.o:hw7.cpp:(.text+0x297): undefined reference to `std::ostream::operator<<(int)'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\javie\AppData\Local\Temp\ccIDnsGB.o:hw7.cpp:(.text+0x2c0): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\javie\AppData\Local\Temp\ccIDnsGB.o:hw7.cpp:(.text+0x2e3): undefined reference to `std::ostream::operator<<(double)'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\javie\AppData\Local\Temp\ccIDnsGB.o:hw7.cpp:(.text+0x2ed): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\javie\AppData\Local\Temp\ccIDnsGB.o:hw7.cpp:(.text+0x2f4): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\javie\AppData\Local\Temp\ccIDnsGB.o:hw7.cpp:(.text+0x3bf): undefined reference to `std::basic_ifstream<char, std::char_traits<char> >::close()'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\javie\AppData\Local\Temp\ccIDnsGB.o:hw7.cpp:(.text+0x3ee): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\javie\AppData\Local\Temp\ccIDnsGB.o:hw7.cpp:(.text+0x3fb): undefined reference to `std::basic_ifstream<char, std::char_traits<char> >::~basic_ifstream()'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\javie\AppData\Local\Temp\ccIDnsGB.o:hw7.cpp:(.text+0x435): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\javie\AppData\Local\Temp\ccIDnsGB.o:hw7.cpp:(.text+0x442): undefined reference to `std::basic_ifstream<char, std::char_traits<char> >::~basic_ifstream()'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\javie\AppData\Local\Temp\ccIDnsGB.o:hw7.cpp:(.text+0x467): undefined reference to `std::ios_base::Init::~Init()'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\javie\AppData\Local\Temp\ccIDnsGB.o:hw7.cpp:(.text+0x488): undefined reference to `std::ios_base::Init::Init()'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\javie\AppData\Local\Temp\ccIDnsGB.o:hw7.cpp:(.text$_ZN4infoC1Ev[__ZN4infoC1Ev]+0x12): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string()'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\javie\AppData\Local\Temp\ccIDnsGB.o:hw7.cpp:(.text$_ZN4infoD1Ev[__ZN4infoD1Ev]+0x12): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\javie\AppData\Local\Temp\ccIDnsGB.o:hw7.cpp:(.text$_ZStgtIcSt11char_traitsIcESaIcEEbRKNSt7__cxx1112basic_stringIT_T0_T1_EESA_[__ZStgtIcSt11char_traitsIcESaIcEEbRKNSt7__cxx1112basic_stringIT_T0_T1_EESA_]+0x10): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::compare(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\javie\AppData\Local\Temp\ccIDnsGB.o:hw7.cpp:(.text$_ZN4infoC1EOS_[__ZN4infoC1EOS_]+0x25): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&&)'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\javie\AppData\Local\Temp\ccIDnsGB.o:hw7.cpp:(.text$_ZN4infoaSEOS_[__ZN4infoaSEOS_]+0x25): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator=(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&&)'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\javie\AppData\Local\Temp\ccIDnsGB.o:hw7.cpp:(.eh_frame+0x13): undefined reference to `__gxx_personality_v0'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\javie\AppData\Local\Temp\ccIDnsGB.o:hw7.cpp:(.eh_frame$_ZStgtIcSt11char_traitsIcESaIcEEbRKNSt7__cxx1112basic_stringIT_T0_T1_EESA_+0x13): undefined reference to `__gxx_personality_v0'
collect2.exe: error: ld returned 1 exit status

I'm not sure what I am doing wrong, what I'm not including or what I'm not implementing.

Upvotes: 0

Views: 50

Answers (1)

Moe
Moe

Reputation: 553

You're compiling this code using a C-language compiler. Not a C++ one. That's why you can't find a reference to all the C++ functions you used. The code is correct but your project setup is wrong. Create a new C++ project on the IDE you're using and paste your code and it will work fine.

EDIT

Ted Lyngmo mentioned in the comments that if it was compiled using gcc it would fail at the compilation stage not the linking stage. But I was still able to reproduce the error after compiling the code with gcc and it turned out that the reason why gcc could compile the code and fail to link it is because of the file extension. It was .cpp. The answer is the same. Compile your code using the proper compiler, which is g++.

Upvotes: 1

Related Questions