CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2014
    Posts
    14

    Need of Assistance #3

    I am writing a program that grabs information from a .dat file. I have the code all structured up, but I get these 2 errors that does not make any sense to me on how to fix the program. Am I missing something?

    errors (2):
    Code:
    Error	1	error LNK2019: unresolved external symbol "void __cdecl swap(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > *,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > *)" (?swap@@YAXPAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0@Z) referenced in function "void __cdecl sort(int)" (?sort@@YAXH@Z)	C:\Users\Mom\Desktop\Drive\Quarter 2\C++\Projects\DynamicDeptPayroll\DynamicDeptPayroll\Source.obj	DynamicDeptPayroll
    ______________________________________________________________________________
    
    Error	2	error LNK1120: 1 unresolved externals	C:\Users\Mom\Desktop\Drive\Quarter 2\C++\Projects\DynamicDeptPayroll\Debug\DynamicDeptPayroll.exe	DynamicDeptPayroll
    .dat file contents:
    Code:
    Fogarty Bob 1 40 10.25
    Smith John 2 38 8.72
    Jones Mary 2 28 6.25
    Arrmen William 1 15 8.22
    Lavey Betty 1 32 15.00
    source contents:
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    
    //PROTOTYPES
    void sort(int n);
    void swap(string *p1, string *p2);
    //GLOBAL VARIABLES
    string *dept, *name, *grosspay;
    
    int main(){
    	string d, n, gp;
    	int count = 0;
    	double dept1, gpay, totalgpay = 0;
    	string name1;
    	
    	//DECLARE FILE OBJECT
    	ifstream fin("c:\\data\\dept.dat");
    
    	while (!fin.eof()){
    		fin >> d >> n >> gp;
    		count++;
    	}
    	fin.close();
    	//SIZE ARRAYS
    	dept = new string[count];
    	name = new string[count];
    	grosspay = new string[count];
    
    	//LOAD ARRAYS
    	fin.open("c:\\data\\dept.dat");
    
    	for (int i = 0; i < count; i++){
    		fin >> dept[i] >> name[i] >> grosspay[i];
    	}
    	fin.close();
    
    	//SORT CALL
    	sort(count);
    
    	for (int i = 0; i < count; i++){
    		
    		gpay = atof(grosspay[i].c_str());
    		name1 = atof(name[i].c_str());
    		totalgpay = gpay / 3;
    
    		cout << dept[i] << "\t" << name[i] << "\t" << grosspay[i] << "\n";
    		cout << "TOTAL GROSS: " << totalgpay;
    	}
    	delete[] dept;
    	delete[] name;
    	delete[] grosspay;
    
    	system("pause");
    	return 0;
    }
    
    void sort(int n){
    
    	int i, j, low;
    	for (i = 0; i < n - 1; i++){
    		low = i;
    		for (j = i + 1; j < n; j++)
    			if (dept[j] < dept[low])
    				low = j;
    
    		if (i != low){
    			swap(&dept[i], &dept[low]);
    			swap(&name[i], &name[low]);
    			swap(&grosspay[i], &grosspay[low]);
    		}
    	}
    }

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Need of Assistance #3

    Code:
    swap(&dept[i], &dept[low]);
    swap(&name[i], &name[low]);
    swap(&grosspay[i], &grosspay[low]);
    Why are you using & to obtain an address?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Need of Assistance #3

    When you get this program to compile, you will find that you also have issues re reading of the data and of calculating total gross. Using the debugger should help you find the problem re calulation.

    With regard to the issue reading the data. When you find eof(), the file is put into a 'fail' state which means that no further data can be read until this state is cleared using the .clear() function. There is also no need to close and re-open the file. Just position the file to the beginning using .seekg() after clearing the error.

    You are using .eof() to determine when the end of file has been reached. Another way of doing this is
    Code:
    while (fin >> d >> n >> gp) count++;
    as extracting from a file causes a fail when data cannot be read which means that the extraction returns a 0 as opposed to a reference to the stream if the extraction worked ok.

    Also note that the format of the data you are extracting from the file does not match the .dat file contents shown in post #1.

    See http://www.cplusplus.com/reference/istream/istream/ for further details of the file stream functions.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured