I have an array that I need to update after using a function. So I need to know how to empty it before I call on the function that reads everything into the array. Simple enough to solve?
I have an array that I need to update after using a function. So I need to know how to empty it before I call on the function that reads everything into the array. Simple enough to solve?
Unless it's a char array, which by convention uses \0 to mark the end, then there is no concept of an 'empty' value for all the other numeric types. Pointers would of course use NULL to indicate emptyness.
The easiest thing to do is
a) pass a parameter indicating the max size of the array
b) return a result indicating the number of array entries actually used.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
The Array contains String, Float and int. And Im kind of a newbie at c++ so I don't exactly know what your talking about..
An array cannot contain different types at the same time. Do you mean that you have more than one array? Or maybe it isn't a normal array?
Try posting the code that declares the array(s) or explain in much more detail what you're doing.
Since this is C++, I might recommend using vectors (search that in google), because they're much easier to use. But everyone else's way would work too.
I want to empty the Register[] after AntalVaror=AntalVaror+1 in the NyVara function and then call the function again so it updates itself.Code:void ReadIntoArray(void){ varuread.open("varulager.txt"); if(!varuread){ cout<<"Filen kunde inte öppnas"; exit(1); } AntalVaror=0; getline(varuread, tmp); while (!varuread.eof()){ AntalVaror=AntalVaror+1; Register[AntalVaror] = parseRow(tmp); getline(varuread, tmp); } varuread.close(); // cout << AntalVaror << " 1: " << Register[1].aname << "\n" << endl; } void NyVara(int artnr, string artname, float artpris){ string s1,s2; ofstream outFile; outFile.open("varulager.txt", ios::app); if (!outFile) { cerr << "Can't open output file " << "varulager.txt" << endl; exit(1); } outFile << artnr << " " << artname << " " << artpris << endl; outFile.close(); AntalVaror=AntalVaror+1; }
I think your question has already been answered.
a) Use a std::vector and clear it with clear().
b) (More work) Keep a variable that shows how many items the array actually contains. To clear, set this variable to 0. Simply don't access items beyond and including that index.
In addition, this is how you should read from a file:
Code:while (getline(varuread, tmp)){ //eof does not work as you expect, see FAQ Register[AntalVaror] = parseRow(tmp); AntalVaror=AntalVaror+1; //indexing starts from 0, increase after accessing }
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
And this is how you should indent.Code:void ReadIntoArray(void){ varuread.open("varulager.txt"); if(!varuread){ cout<<"Filen kunde inte öppnas"; exit(1); } AntalVaror=0; getline(varuread, tmp); while (!varuread.eof()){ AntalVaror=AntalVaror+1; Register[AntalVaror] = parseRow(tmp); getline(varuread, tmp); } varuread.close(); // cout << AntalVaror << " 1: " << Register[1].aname << "\n" << endl; } void NyVara(int artnr, string artname, float artpris){ string s1,s2; ofstream outFile; outFile.open("varulager.txt", ios::app); if (!outFile) { cerr << "Can't open output file " << "varulager.txt" << endl; exit(1); } outFile << artnr << " " << artname << " " << artpris << endl; outFile.close(); AntalVaror=AntalVaror+1; }
Further,
Is the same asCode:AntalVaror=AntalVaror+1;
AndCode:AntalVaror++;
This is generally considered bad practice. Your function should return whether it failed or not, and preferably an error value to say what went wrong and the caller (main usually) should handle the error.Code:if (!outFile) { cerr << "Can't open output file " << "varulager.txt" << endl; exit(1); }