-
March 5th, 2014, 10:00 AM
#1
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.
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], <ype[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], <ype[low]);
}
}
}
void swap(string *p1, string *p2) {
string temp = *p1;
*p1 = *p2;
*p2 = temp;
}
-
March 5th, 2014, 10:05 AM
#2
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.
-
March 5th, 2014, 10:13 AM
#3
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;
}
-
March 5th, 2014, 10:24 AM
#4
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.
-
March 5th, 2014, 10:33 AM
#5
Re: Need of Assistance #2
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.
Victor Nijegorodov
-
March 5th, 2014, 11:46 AM
#6
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. 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|