@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?
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.)
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.
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.
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
#include <fstream>
#include <iostream>
#include <string>
usingnamespace 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 = newdouble[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;
}
#include <fstream>
#include <iostream>
#include <string>
usingnamespace 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 = newdouble[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.