dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Run-Time Check Failure #2 and Linker Tools Error LNK1168

  1. #1
    Join Date
    Feb 2018
    Posts
    2

    Run-Time Check Failure #2 and Linker Tools Error LNK1168

    Hello, I am writing a program that reads 2 input files, converts them into 2 arrays, and then merges the 2 arrays into 1 array in sequential order without any duplicates. I seem to be getting the message after asking the user what it's input files and output files are called. "Run-Time Check Failure #2 - Stack around the variable 'array1' was corrupted."
    I'm also getting an error that it cannot open my .exe file (Linker Tools Error LNK1168)

    Does anyone see what is wrong with my code?

    Thanks!

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <ctime>
    #include <fstream>
    #include <string>
    using namespace std;
    
    void getInput(string prompt, ifstream& fileVariable);
    void getOutput(string prompt, ofstream& fileVariable);
    
    
    
    
    
    int main()
    {
    	ifstream inputFile1;
    	ifstream inputFile2;
    	ofstream outputFile;
    
    	int array1[20] = {};
    	int array2[20] = {};
    	int finalArray[40] = {};
    	int array1elements = -1;
    	int array2elements = -1;
    	int array1index = 0;
    	int array2index = 0;
    
    
    
    	getInput("What is the name of the first input file? ", inputFile1);
    
    	do
    	{
    		inputFile1 >> array1[array1elements];
    		array1elements++;
    	} while (!inputFile1.eof());
    	getInput("What is the name of the second input file? ", inputFile2);
    
    	do
    	{
    		inputFile2 >> array2[array2elements];
    		array2elements++;
    	} while (!inputFile2.eof());
    	getOutput("What is the name of the output file? ", outputFile); // the error occurs somewhere below this point
    
    
    		do
    		{
    			if (array1elements = 0)
    			{
    				outputFile << array2[array2index] << " ";
    				array2index++;
    				array2elements--;
    			}
    			else if (array2elements = 0)
    			{
    				outputFile << array1[array1index] << " ";
    				array1index++;
    				array1elements--;
    			}
    			else if (array1[array1index] == array2[array2index])
    			{
    				outputFile << array1[array1index] << " ";
    				array1index++;
    				array2index++;
    				array1elements--;
    				array2elements--;
    			}
    			else if ((array1[array1index]) > (array2[array2index]))
    			{ 
    				outputFile << array2[array2index] << " ";
    				array2index++;
    				array2elements--;
    			}
    			else
    			{
    				outputFile << array1[array1index] << " ";
    				array1index++;
    				array1elements--;
    			}
    		} while ((array1elements > 0) || (array2elements > 0));
    
    
    
    
    	inputFile1.close();
    	inputFile2.close();
    	outputFile.close();
        return 0;
    }
    
    void getInput(string prompt, ifstream& fileVariable)
    {
    	string fileName(" ");
    	do
    	{
    		cout << prompt;
    		cin >> fileName;
    
    		fileVariable.open(fileName);
    
    		if (fileVariable.fail())
    		{
    			cout << "Error, incorrect file name. Please try again..." << endl;
    		}
    	} while (fileVariable.fail());
    }
    
    void getOutput(string prompt, ofstream& fileVariable)
    {
    	string fileName(" ");
    
    	do
    	{
    		cout << prompt;
    		cin >> fileName;
    
    		fileVariable.open(fileName);
    
    		if (fileVariable.fail())
    		{
    			cout << "Error, please try again..." << endl;
    		}
    	} while (fileVariable.fail());
    }

  2. #2
    Join Date
    Feb 2017
    Posts
    370

    Re: Run-Time Check Failure #2 and Linker Tools Error LNK1168

    Quote Originally Posted by Fudgezilla View Post
    if (array1elements = 0)
    Did you mean "if (array1elements == 0)" ?

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,511

    Re: Run-Time Check Failure #2 and Linker Tools Error LNK1168

    Code:
    int array1elements = -1;
    ...
    inputFile1 >> array1[array1elements];
    The first time data is read from inputFile1, array1elements is set to -1. As array indexing starts at 0......... The same for inputFile2.

    Is this a homework exercise to use arrays - as there's a much simpler solution to produce the required sorted unique file.

    A couple if points.

    When passing type string to a function, it is usually as type const string& (ie pass by reference rather than by value so that the contents of the passed string is not copied).

    Code:
    string fileName(" ");
    Why initialise fileName to a space when the contents of fileName are going to be overwritten with the stream extraction?

    What happens if there are more than 20 items in a file?
    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++17 Compiler: Microsoft VS2017 (15.9.3)

  4. #4
    Join Date
    Feb 2018
    Posts
    2

    Re: Run-Time Check Failure #2 and Linker Tools Error LNK1168

    Quote Originally Posted by wolle View Post
    Did you mean "if (array1elements == 0)" ?
    Yes! It was also the -1 mentioned by 2kaud.
    Thank you! My program is running well after fixing what both of you mentioned.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,511

    Re: Run-Time Check Failure #2 and Linker Tools Error LNK1168

    Quote Originally Posted by Fudgezilla View Post
    Yes! It was also the -1 mentioned by 2kaud.
    Thank you! My program is running well after fixing what both of you mentioned.
    Great!

    there's a much simpler solution to produce the required sorted unique file.
    For info, consider using a set

    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <set>
    using namespace std;
    
    void getInput(const string& prompt, ifstream& fileVariable)
    {
    	do
    	{
    		string fileName;
    
    		cout << prompt;
    		cin >> fileName;
    
    		fileVariable.open(fileName);
    
    	} while (!fileVariable.is_open() && (cout << "Error, incorrect file name. Please try again..." << endl));
    }
    
    void getOutput(const string& prompt, ofstream& fileVariable)
    {
    	do
    	{
    		string fileName;
    
    		cout << prompt;
    		cin >> fileName;
    
    		fileVariable.open(fileName);
    
    	} while (!fileVariable.is_open() && (cout << "Error, incorrect file name. Please try again..." << endl));
    }
    
    int main()
    {
    	set<int> si;
    	ifstream inputFile1;
    	size_t ic1 = 1;
    
    	getInput("What is the name of the first input file? ", inputFile1);
    
    	for (int inp; inputFile1 >> inp; ++ic1)
    		si.insert(inp);
    
    	if (!inputFile1.eof())
    		cout << "Problem reading data from first input file at entry " << ic1 << endl << endl;
    
    	ifstream inputFile2;
    	size_t ic2 = 1;
    
    	getInput("What is the name of the second input file? ", inputFile2);
    
    	for (int inp; inputFile2 >> inp; ++ic2)
    		si.insert(inp);
    
    	if (!inputFile2.eof())
    		cout << "Problem reading data from second input file at entry " << ic2 << endl << endl;
    
    	ofstream outputFile;
    
    	getOutput("What is the name of the output file? ", outputFile);
    
    	for (const auto& i : si)
    		outputFile << i << " ";
    
    	inputFile1.close();
    	inputFile2.close();
    	outputFile.close();
    }
    Basically, a set stores unique values in sorted order - exactly what is required! For more info see http://www.cplusplus.com/reference/set/set/
    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++17 Compiler: Microsoft VS2017 (15.9.3)

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)