Need of Assistance #2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Need of Assistance #2

  1. #1
    Join Date
    Feb 2014
    Posts
    14

    Need of Assistance #2

    Hello again, I am currently writing a program that grabs information from a .dat file. When I go to debut I get this error.Name:  Untitled.jpg
Views: 285
Size:  31.4 KB

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    
    // function step 1 - declare the prototypes
    void sort(int n); // catches an integer number from the call
    void swap(string *p1, string *p2); //catches the location of data in ram
    
    // array number 1, declare & size the array
    string *lname, *lyear, *ltype; // global because it needs to work with both main & sort
    
    int main() {
    	int i, count = 0;
    	string type, name, year;
    	// open data file - create the fin object, - attach to datafile
    	ifstream fin("c:\\data2\\club.dat");
    	//count the records
    	while (!fin.eof()){
    		fin >> name >> type >> year;
    		count++;
    	}
    	fin.close();
    
    	//array  size the arrays
    	ltype = new string[count];
    	lname = new string[count];
    	lyear = new string[count];
    
    	//load the arrays
    	fin.open("c:\\data2\\club.dat");
    
    	for (i = 0; i < count; i++) {
    		fin >> name[i] >> type[i] >> year[i];
    
    	}
    	fin.close();
    
    	sort(6);
    
    	cout << "Here is the Array - Sorted \n\n";
    
    	// output
    	for (i = 0; i < 6; i++)
    		cout << name[i] << "\t" << type[i] << "\t" << year[i] << "\n";
    		//delete the arrays
    		delete [] lname;
    		delete [] ltype;
    		delete [] lyear;
    
    		system("pause");
    		return 0;
    }
    
    //Function step 3 - The Function Definition
    void sort(int n) {
    	int i, j, low; //Local variable - only exists in the sort function
    	for (i = 0; i < n - 1; i++){
    		
    		low = i;
    
    		for (j = i + 1; j < n; j++)
    			if (lname[j] < ltype[low])
    				low = j;
    
    		if (i != low) { //Need a swap call for every array
    			swap(&lname[i], &ltype[low]); //call to the swap function - the address if a[i] will be caught by
    									    //the variable *p1 & the address of a[low] will be caught by *p2
    			swap(&lname[i], &ltype[low]);
    
    
    		}
    	}
    }
    
    void swap(string *p1, string *p2) {
    	string temp = *p1;
    	*p1 = *p2;
    	*p2 = temp;
    }

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,230

    Re: Need of Assistance #2

    You appear to be trying to use name, type and year as arrays, although they aren't declared that way.

    That image is really hard to read because of its size. Your debugger should be able to tell you what's wrong.

  3. #3
    Join Date
    Feb 2014
    Posts
    14

    Re: Need of Assistance #2

    Okay, then I'm going to have to do something different. I'm copying off a program that I made before with the same setup to help me sort of get it structured. The only difference is that is only uses 2 variables instead of 3....

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    
    // function step 1 - declare the prototypes
    void sort(int n); // catches an integer number from the call
    void swap(string *p1, string *p2); //catches the location of data in ram
    
    // array number 1, declare & size the array
    string *stooge, *rating; // global because it needs to work with both main & sort
    
    
    int main() {
    	int i, count = 0;
    	string name, rate;
    	// open data file - create the fin object, - attach to datafile
    	ifstream fin("c:\\data\\stooge.dat");
    	//count the records
    	while (!fin.eof()){
    		fin >> name >> rate;
    		count++;
    	}
    	fin.close();
    
    	//array  size the arrays
    	stooge = new string[count];
    	rating = new string[count];
    
    	// load the arrays
    	fin.open("c:\\data\\stooge.dat");
    
    	for (i = 0; i < count; i++){
    		fin >> stooge[i] >> rating[i];
    	}
    	fin.close();
    
    	sort(4); //n in the sort function is catching the 10 because that is the size of the a array
    
    	cout << "Here is the array - sorted \n\n";
    
    	// output
    	for (i = 0; i < 4; i++)
    		cout << stooge[i] << "\t" << rating[i] << "\n";
    	//delete the arrays
    	delete[] stooge;
    	delete[] rating;
    
    	system("pause");
    	return 0;
    }
    
    //function step 3 - the function definition
    void sort(int n){ // n catches the 10 from the call
    	int i, j, low; // local variables - only exists in the sort function
    	for (i = 0; i < n - 1; i++){
    
    		low = i;
    
    		for (j = i + 1; j < n; j++)
    			if (stooge[j] < stooge[low])
    				low = j;
    
    		if (i != low){ // Need a swap call for every array
    			swap(&stooge[i], &stooge[low]); //call to the swap function- the address if a[i] will be caught by
    			// the variale *p1 & the address of a[low] will be caught by *p2
    			swap(&rating[i], &rating[low]);
    		}
    	}
    }
    
    void swap(string *p1, string *p2) {
    	string temp = *p1;
    	*p1 = *p2;
    	*p2 = temp;
    }

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,230

    Re: Need of Assistance #2

    I'm not sure what point you're trying to make. There are lots of differences, including the first app is trying to use single variables as arrays and the second one isn't.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,610

    Re: Need of Assistance #2

    Quote Originally Posted by CoriNEIT View Post
    Hello again, I am currently writing a program that grabs information from a .dat file. When I go to debut I get this error.
    When you get the "Debug assertion failed" dialog then do what it suggests: press a Retry button to debug the application.
    Then you will probably see what was the reason of the failure and where in your code it happened.
    Victor Nijegorodov

  6. #6
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,854

    Re: Need of Assistance #2

    Unless you have a requirement to handle memory allocation etc yourself, why not use a vector of string? You wouldn't then need to read the file twice - once to find out how many records and then again to read the data into the arrays. You could just do it all in one pass using the .push_back() vector method. Even better, have a struct with members name, type and year and then have a vector of this struct type.

    Looking at your original code
    Code:
    ltype = new string[count];
    lname = new string[count];
    lyear = new string[count];
    ...
    fin >> name[i] >> type[i] >> year[i];
    Oh!!
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

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

This is a CodeGuru survey question.


Featured


HTML5 Development Center