Help please

Pages: 123
Thank you Yay295 for the help. :)

I believe I have added/deleted everything the community has mentioned but still i am getting an error message.
Last edited on
you can't do this


 
char filename [30] = { 0 };


what is the error?

Edit. My bad. Was not in front of my IDE.
Last edited on
"

DEBUG ERROR!

Program:
program/project/practice problem

invalid allocation size: 2347384702 bytes


Please retry to debug application
"
@Bdanielz
Yes, you can. That's how you initialize arrays. If you don't initialize it to something, it will have random values, but if you give it values to use (and not too many) it will use those values and set the rest of the array to 0.

@mousecrazy
Can you post the contents of scores.dat?


45
1
34.3
12
0.0
78.53
3
7
46
86.3
50.0
78.53
3
7
46
86.3
5
Last edited on
It looks like size is still uninitialized. That can happen if read from file fails. Something makes me think that file is not open.

Add following between lines 18 and 19:
1
2
3
4
if(not file.is_open()) {
    std::cout << "Cannot open file";
    exit(-1);
}
And try running again
Last edited on
@MiiNiPaa - I added the code.
1
2
3
4
   if(file.fail()) {
   cout << "Cannot open file";
    
}


I get "cannot open file" and the same error message simultaneously


"
DEBUG ERROR!

Program:
program/project/practice problem

invalid allocation size: 2347384702 bytes


Please retry to debug application
"
Last edited on
Wait. it should exit before allocation! Are you sure that you insertet it exactly as I posted?
At least we know that program cannot open file. I suppose it is not placed in your project working directory (If you are running from IDE, it is usually directory when your .cpp files is.)
The program did close before allocation I had removed the exit(-1); so I could see the words "could not open file"

Last edited on
Hint: to prevent console from closing run project using CTRL+F5 instead of just F5. But you lose benefits of debug mode that way (like readable error messages).

What about file? Where is it located?

I added the .dat file to the source files. I believe this is correct.
Nope that is not correct. At least you did not make it to actually compile.

You should place file in project working directory. It is either where actual executable is located, or where all source files are.
Last edited on
Whoza!!! It no longer gives me an error message nor the "could not open file". Thank you :)

So the problem asks me to have my program read the remaining numbers into the array. Once the array has been filled.

Last edited on
closed account (2UD8vCM9)
@mousecrazy


Something about what you're saying is really confusing me.

You're saying that in this text file, the very first number you read in should be the size of your number array.

However, for this to be true, your text file should have that many numbers after the first number.

Are you sure that the text file you have contains correct information?

For example:

Instead of looking like this
1
2
3
4
5
6
7
8
9
10
11
45
1
34.3
12
0.0
78.53
3
7
46
86.3
5 



It should look like this
1
2
3
4
5
6
7
8
9
10
11
10
1
34.3
12
0.0
78.53
3
7
46
86.3
5 


The reason I changed the first number to 10, was because there are 10 numbers after the first number, and from what you said earlier it makes it sound like the first number is a representation of how many numbers follow after that number.
yes you are right, 10 is the first number.


Last edited on
closed account (2UD8vCM9)
I made some changes to your code due to your valiant efforts.
Effort is rewarded.
Hopefully I did this right.

Let me know if this is what you were looking for.
PLEASE ASK ABOUT ANYTHING YOU DON'T GET HOMIE. I TRIED TO PUT LOTS OF COMMENTS N SHIT YOU KNOW WHAT IM SAYING SO YOU WOULDNT JUST BE LOOKING AT CODE WITHOUT KNOWING WHAT WAS GOING ON BUT I REALIZE SOMETIMES MY COMMENTS MIGHT NOT BE THAT GOOD SO LET ME KNOW IF YOU DON'T GET IT

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <fstream>
#include <iostream>
#include <string>


using namespace std;

int main ( )
{
	string filename;


	cout << ( "Please enter the file name: " );

	getline(cin, filename); //Takes the input from whatever user typed it and puts it into filename string
	cin.clear(); //Good habit to call cin.clear() after getline

	while (filename.size() == 0) //This while loop will only run if the user doesn't enter anything
	{
		cout << "Please enter a valid file name: ";
		getline(cin,filename);
		cin.clear();
	}

	
	ifstream file; //Create input file stream object called file
	file.open(filename); //Attempt to open file

	while (!file.is_open()) //If our file was not successfully opened, loop until we can properly open it
	{
		cout << "File could not be found. Please re-enter a valid file name: ";
		getline(cin,filename);
		cin.clear();
		file.open(filename);
	}

	int size;
	file >> size ;
	double *array = new double[size];

	for (int i=0; i<size; i++) //For loop to copy all the numbers from file into our array
	{
		file >> array[i]; //Move number from file into our double array
	}

	//Next we need to sort the array. We'll use a linear sort (I think it's called that, I forget)
	for (int i=0; i<(size); i++) //This is the loop to make sure we go through as many times as might be necessary
	{

		for (int a=0; a<(size-1); a++) //This is the loop to swap each number at least once
		{
			if ( array[a] > array[a+1] ) //If a number touching on the left side is greater than a number on the right
			{
				swap(array[a], array[a+1]); //Swap the numbers
			}

		} //Closing curly brace for first for loop (nested loop)

	} //Closing curly brace for second for loop

	//At this point our array called array has been sorted
	cout << "Your sorted array is listed below." << endl;
	for (int i=0;i<size; i++) //For loop to display each character seperated by a space
	{
		cout << array[i] << " ";
	} 

	// this deletes the dynamically created array
	delete [] array; 


	
	//console controls
	cout<<"\nPress 'enter' to exit: ";
	system("pause");

    return 0;
}
Last edited on
Wow, Thank you @Pindrought ! lol, I like how you can write this code out in a few minutes or so with no problem. :)

And thank you to everyone who pitched in, this is one of the best programming community I have been to, I really appreciate the support.


closed account (2UD8vCM9)
No problem buddy. Just do me one favor.

Please click the "My Question Was Resolved" checkmark button, so that way people will know it's answered.

Last edited on
How would I output the information to a new file? I changed the

ifstream file; //Create input file stream object called file
to
ofstream file; //Create input file stream object called file

but this symbol >> gets a red underline on lines 38 and 43
Last edited on
closed account (2UD8vCM9)
Think of it like this.

When we used ifstream, if we wanted to read the data from the line into an integer, we would do something like...

1
2
int x=0;
file >> x;


So if we want to write to a file, we'll use

1
2
int x;
file << x;


If you want to save all of the data to a file after you've copied it into your array, modify your code like so.

Tell me what you don't understand.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <fstream>
#include <iostream>
#include <string>


using namespace std;

int main ( )
{
	string filename;


	cout << ( "Please enter the file name: " );

	getline(cin, filename); //Takes the input from whatever user typed it and puts it into filename string
	cin.clear(); //Good habit to call cin.clear() after getline

	while (filename.size() == 0) //This while loop will only run if the user doesn't enter anything
	{
		cout << "Please enter a valid file name: ";
		getline(cin,filename);
		cin.clear();
	}

	
	ifstream file; //Create input file stream object called file
	file.open(filename); //Attempt to open file
	while (!file.is_open()) //If our file was not successfully opened, loop until we can properly open it
	{
		cout << "File could not be found. Please re-enter a valid file name: ";
		getline(cin,filename);
		cin.clear();
		file.open(filename);
	}

        int size;
        file >> size;
	double *array = new double[size];

	for (int i=0; i<size; i++) //For loop to copy all the numbers from file into our array
	{
		file >> array[i]; //Move number from file into our double array
	}
        file.close(); //close the read file
	//Next we need to sort the array. We'll use a linear sort (I think it's called that, I forget)
	for (int i=0; i<(size); i++) //This is the loop to make sure we go through as many times as might be necessary
	{

		for (int a=0; a<(size-1); a++) //This is the loop to swap each number at least once
		{
			if ( array[a] > array[a+1] ) //If a number touching on the left side is greater than a number on the right
			{
				swap(array[a], array[a+1]); //Swap the numbers
			}

		} //Closing curly brace for first for loop (nested loop)

	} //Closing curly brace for second for loop

	//At this point our array called array has been sorted
	cout << "Your sorted array is listed below." << endl;
	for (int i=0;i<size; i++) //For loop to display each character seperated by a space
	{
		cout << array[i] << " ";
	} 

	cout << endl;

	cout << "Enter the name of the file to save this information to:";
	string NameOfFileToSaveInfoTo;
	getline(cin, NameOfFileToSaveInfoTo);

	ofstream FileToSaveTo;
	FileToSaveTo.open(NameOfFileToSaveInfoTo);

	FileToSaveTo << size << endl; //First we copy the size since our original file worked like that
	for (int i=0; i<size; i++) //Then we'll loop through each element in our dynamically created array of doubles and copy them
	{
		FileToSaveTo << array[i] << endl;
	}
	//When we're done, we close it
	FileToSaveTo.close();


	// this deletes the dynamically created array
	delete [] array; 


	
	//console controls
	cout<<"\nPress 'enter' to exit: ";
	system("pause");

    return 0;
}


Sorry I didn't reply to your message earlier, I've been sick today.
Last edited on
closed account (2UD8vCM9)
Oops accidentally removed the int size; file >> size;

Just added it back in though
Disregard this post, I figured it out.

I hope you feel better soon :)

Last edited on
Pages: 123