Question:
My exe keeps crashing. I am using Dev- C++.?
2012-08-06 19:14:24 UTC
Here is my code...Thank you!
#include
#include
#include
#include
#include

using namespace std;

void readData(ifstream& inputFile, int list[], int size);
void print(int list[], int size);

int main()
{
int scores[8] = {0};

ifstream infile;

infile.open("C:\\TestScoresData.txt");


if (!infile)
{
cout << "Cannot open the input file. Program terminates!"
<< endl;
return 1;

}

readData(infile, scores, 8);
print(scores, 8);
cout << endl;

infile.close();

return 0;


}

void readData(ifstream& inputFile, int list[], int size)
{
int score;
int index;

inputFile >> score;

while (inputFile)
{
index = score / 25;
if (index == size)
index--;
if (index < size)
list[index]++;
inputFile >> score;

}

}

void print(int list[], int size)
{
int range;
int lowRange = 0;
int upperRange = 24;

cout << " Range # of Students" << endl;

for (range = 0; range < size; range++)
{
cout << setw(3) << lowRange << " - " << setw(3)
<< upperRange << setw(15)
<< list[range] << endl;
lowRange = upperRange + 1;
upperRange = upperRange + 25;
if (range == size - 2)
upperRange++;
}
cout << endl;

}
Four answers:
Bob
2012-08-06 20:12:28 UTC
I don't think your program is crashing, its just executing and closing too fast for you to tell. To debug this, I would first check that the text file "TestScoresData.txt" exists in the C drive. Next, add two system("pause") statements as shown below. Your main function should look like this:



int main()

{

int scores[8] = {0};



ifstream infile;



infile.open("C:\\TestScoresData.txt");



if (!infile)

{

cout << "Cannot open the input file. Program terminates!"

<< endl;



system("pause");



return 1;

}



readData(infile, scores, 8);

print(scores, 8);

cout << endl;



infile.close();



system("pause");



return 0;

}
James Bond
2012-08-07 02:57:18 UTC
Are you sure that data is valid. You said scores is an element array. Its valid elements indexes are 0 to 7. You are reading score from file and dividing with 5 and that you are assigning to index and incrementing the scores array count.

Also you have declared scores in the main as

int scores[8] = {0};

This does not initialize all the values to 0s. Only first element is made as 0. So write a for loop to initialize scores array to 0s like

for(int i=0;i<8;i++) scores[i]=0;



void readData(ifstream& inputFile, int list[], int size)

{

int score;

int index;



inputFile >> score;



while (!inputFile.eof())

{

index = score / 25;

if (index == size)

index--;

if (index < size)

list[index]++;

inputFile >> score;



}



}
Wertle Woo
2012-08-07 02:58:48 UTC
1. Don't use Dev C++.

2. Use pastebin.

3. After correcting #1 and #2, try asking again.



Now on to the code issues:

1. On line 21, you're checking if infile is NULL. That's always going to be false, because infile is an object on the stack that you created on line 16. Instead, you want to check if infile is not open:

if(!infile.is_open())

However, this is backwards logic. Instead of checking if something is NOT bad in order to do something, you should check if it is good. So instead of checking if it's not open and exiting, check if it IS open and then execute the code for that scenario.



2. On line 25, you're returning from an if statement. This is retarded. Do not use multiple return statements, especially for returning from main. Use the stack to move your execution where it belongs, and terminate execution at the same spot for all paths of executions.



3. On line 47, you're running an infinite while loop, because your condition only checks if your inputFile is not NULL. What you want is to check that it's not at the end of file:

while(!inputFile.eof())

However, this can lead to other issues depending on what's inside the while loop, because you have to attempt to read the eof bit before the eof flag will get activated, which means if your last statement stopped reading the file just before the EOF, then inputFile.eof will return false and your loop will run again.



4. I'm not sure what you're trying to accomplish with the code inside your while loop. From what I can tell, your text file is a list of scores. You're taking in each score and dividing it by 25 to get an index. Then on line 50, if the index is the same as the size of your array, you reduce the index by 1. Then on line 52, if the index is less than the size of the array, you increment the score that was already in that index. Then you read in a new score on line 54. It almost looks like it started to be a hash function, but then it got all stupid. You're not saving any of the actual scores anywhere, which brings me to...



5. You're just doing some math on the score to get an index, and then incrementing the value at that index, which is going to cause problems because you never initialized the elements in your array, so they're storing garbage and you're incrementing that garbage.



6. Okay, there are more issues, and I haven't even begun to look at your print function. I had to stop before I lost more IQ points. Your code is horrid. You have no clue what you're doing. You clearly lack basic logic skills. Yes, new programmers make mistakes, but of all the new programmers I've tutored, these are NOT the types of mistakes future *good* programmers make. These are the types of mistakes made by people who are not cut out to be programmers and eventually fail out of the degree program in school.



Click this link: http://www.kent.ac.uk/careers/tests/computer-test.htm

It is a logic aptitude test. If you can't ace it within the time limit, you have poor logic skills. Logic is EVERYTHING in programming, so if you have bad logic, then you need to get out of programming and find something that will lend itself to your particular skillset, whatever that may be.
2012-08-07 03:22:45 UTC
idk, you can attach your testscores.txt to an email and i will try it out on your data. I made a quick file with several test scores all under 100 and nothing crashed. I took out the setw() out of the cout. I might be missing where that is in the proper #include. I never used cout and cin all that much. I went from stdio c to windows programming in c/c++.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...