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.
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;
}
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.
Re: Need of Assistance #2
Quote:
Originally Posted by
CoriNEIT
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.
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!! :confused: