C++ Help
OttoDestruct
Join Date: 2002-11-08 Member: 7790Members

Heres the code... problem is that I can't get it to correctly get the integer length by reading from the file. *EDIT* The part its messing up on is the arraylength fuction.
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
#include <fstream.h>
#include <stdlib.h>
int main (void)
{
// Local Declaration
ifstream fin;
ofstream fout;
int length;
// Prototype Declaration
void openfiles (ifstream&, ofstream&);
int arraylength (ifstream&);
void resetarray (int, ifstream&, float[ ]);
void writefile (int, ofstream&, float[ ]);
void displayarray (int, float[ ]);
void addtenpercent (int, float[ ]);
void rmlastprice (int, float[ ]);
void checkfulladd (int, float[ ]);
void findsmallest (int, float[ ]);
void findlargest (int, float[ ]);
void deltwentypercent (int, ofstream&, float[ ]);
void checkfulladd2 (int, float[ ]);
void descendselection (int, float[ ]);
void rmfirstprice (int, ofstream&, float[ ]);
void ascendbubble (int, float[ ]);
const int CAPACITY = 50;
float array[CAPACITY];
length = arraylength(fin);
resetarray (length, fin, array);
fout << "Program by Aaron Friedley MWF 1:00" << endl << endl
<< "Original List" << endl;
writefile (length, fout, array);
fout << endl << "Ten percent added" << endl;
addtenpercent (length, array);
writefile (length, fout, array);
resetarray (length, fin, array);
cout << endl << "Last price removed" << endl;
rmlastprice (length, array);
cout << endl << "Check if list is full, if not add 27.93 to list" << endl;
checkfulladd (length, array);
cout << endl << "Find smallest value and its position" << endl;
findsmallest (length, array);
cout << endl << "Find largest value and its position" << endl;
findsmallest (length, array);
fout << endl << "Delete last twenty percent of prices";
deltwentypercent (length, fout, array);
cout << endl << "Check if list is full, if not add 54.28 to list" << endl;
checkfulladd2 (length, array);
fout << endl << "Sort list descending with selection sort";
descendselection (length, array);
writefile (length, fout, array);
resetarray (length, fin, array);
cout << endl << "Last price removed" << endl;
rmfirstprice (length, fout, array);
fout << endl << "Sort list in ascending order with bubble sort";
ascendbubble (length, array);
writefile (length, fout, array);
fin.close ();
fout.close ();
} // main
// *** openfiles ***
// Function opens input file hwk02.in and output hwk02.out streams
// If file cannot be opened error is displayed and program exits
void openfiles (ifstream& fin, ofstream& fout)
{
fin.open("hwk02.in");
if (fin.fail())
{
cout << "File not found.\n";
exit (1);
} // if
fout.open("hwk02.out");
if (fout.fail())
{
cout << "File could not be opened.\n";
exit (2);
} // if
} // openfiles
// *** arraylength ***
// Reads file incrementing integer length each time new data is found,
// then returns the length to main for initilization of an array
int arraylength(ifstream& fin)
{
char temp;
int length = 0;
while (fin.get(temp))
{
length++;
}// while
return length;
} // arraylength
// *** resetarray ***
// Function reads values from file and writes those values into array
void resetarray (int length, ifstream& fin, float array[ ])
{
int walker;
float temp;
for (walker = 0; walker < length; walker++)
{
fin >> temp;
array[walker] = temp;
} //for
} // resetarray
// *** writefile ***
// Writes values stored in the array to hwk02.out
void writefile (int length, ofstream &fout, float array[ ])
{
int walker;
fout << "---------------" << endl;
for (walker = 0; walker < length; walker++)
fout << "\t" << array[length] << endl;// for
} // writefile
// *** displayarray ***
// Displays values stored in array to screen
void displayarray (int length, float array[ ])
{
int walker;
cout << "---------------" << endl;
for (walker = 0; walker < length; walker++)
cout << "\t" << array[length] << endl;// for
}
// *** addtenpercent ***
// Adds ten percent to array values
void addtenpercent (int length, float array[ ])
{
int walker;
int temp;
int temp2;
for (walker = 0; walker < length; walker++)
{
temp = array[walker];
temp2 = array[walker] * 0.10;
array[walker] = temp + temp2;
} // for
} // addtenpercent
// *** rmlastprice ***
// Remove last price in array
void rmlastprice (int length, float array[ ])
{
int walker;
cout << "---------------" << endl;
for (walker = 0; walker < length-1; walker++)
cout << "\t" << array[length] << endl; // for
} // rmlastprice
// *** checkfulladd ***
// Check if list < 50, if not add 27.93 to end of list
void checkfulladd (int length, float array[ ])
{
int walker;
if (length < 50)
{
array[length+1] = 27.93;
cout << "---------------" << endl;
for (walker = 0; walker < length+1; walker++)
cout << "\t" << array[length]; // for
} // if
} // checkfulladd
// *** findsmallest ***
// Find smallest number in array, and its position then print to screen
void findsmallest (int length, float array[ ])
{
int walker;
int temp;
int temp2;
temp = array[0];
for (walker = 1; walker < length; walker++)
{
if (temp > array[walker])
{
temp2 = array[walker];
temp = walker;
} // if
} // for
cout << "---------------" << endl;
cout << "\t" << temp2 << endl << "\t" << temp;
} // findsmallest
// *** findlargest ***
// Find largest number in array, and its position then print to screen
void findlargest (int length, float array[ ])
{
int walker;
int temp;
int temp2;
temp = array[0];
for (walker = 1; walker < length; walker++)
{
if (temp < array[walker])
{
temp2 = array[walker];
temp = walker;
} // if
} // for
cout << "---------------" << endl;
cout << "\t" << temp2 << endl << "\t" << temp;
} // findlargest
// *** deltwentypercent ***
// Delete last 20 percent of prices from the list and write to file
void deltwentypercent (int length, ofstream& fout, float array[ ])
{
int temp;
int walker;
temp = length * .20;
fout << "---------------" << endl;
for (walker = 0; walker < length-temp; walker++)
fout << "\t" << array[length]; // for
} // deltwentypercent
// *** checkfulladd2 ***
// Check if list < 50, if not add 54.28 to end of list
void checkfulladd2 (int length, float array[ ])
{
int walker;
const int length2 = length + 1;
if (length < 50)
{
array[length+1] = 54.28;
cout << "---------------" << endl;
for (walker = 0; walker < length+1; walker++)
cout << "\t" << array[length]; // for
} // if
} // checkfulladd2
// *** descendselection ***
// Arrange array in descending order using selection sort
void descendselection (int length, float array[ ])
{
int current;
void exchangeLargest (int, float[ ], int);
for (current = 0; current < length; current++)
exchangeLargest (length, array, current);
} // descendselection
// *** exchangeLargest ***
// Second half of descendselection function
// Arranges array in descending order using selection sort
void exchangeLargest (int length, float array[ ], int current)
{
int walker;
int largest;
int temp;
largest = current;
for (walker = current + 1; walker <=length; walker++)
{
if (array[walker] > array[largest])
largest = walker; // if
}// for
temp = array[current];
array[current] = array[largest];
array[largest] = temp;
} // exchangeLargest
// *** rmfirstprice ***
// Removes first price from array
void rmfirstprice (int length, ofstream& fout, float array[ ])
{
int walker;
fout << "---------------" << endl;
for (walker = 1; walker < length; walker++)
fout << "\t" << array[length]; // for
} // rmfirstprice
// *** ascendbubble ***
// Sort list in ascending order using bubble sort
void ascendbubble (int length, float array[ ])
{
int current;
void bubbleUp (int, float[ ], int);
for (current = 0; current < length; current++)
bubbleUp (length, array, current);
} // ascendbubble
// *** bubbleUp ***
// Second half of ascendbubble
// Sort list in ascending order using bubble sort
void bubbleUp (int length, float array[ ], int current)
{
int walker;
int temp;
for (walker = length; walker > current; walker--)
{
if (array[walker] < array[walker-1])
{
temp = array[walker];
array[walker] = array[walker-1];
array[walker-1] = temp;
} // if
} // for
} // bubbleUp
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
Yes I know its messy.
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
#include <fstream.h>
#include <stdlib.h>
int main (void)
{
// Local Declaration
ifstream fin;
ofstream fout;
int length;
// Prototype Declaration
void openfiles (ifstream&, ofstream&);
int arraylength (ifstream&);
void resetarray (int, ifstream&, float[ ]);
void writefile (int, ofstream&, float[ ]);
void displayarray (int, float[ ]);
void addtenpercent (int, float[ ]);
void rmlastprice (int, float[ ]);
void checkfulladd (int, float[ ]);
void findsmallest (int, float[ ]);
void findlargest (int, float[ ]);
void deltwentypercent (int, ofstream&, float[ ]);
void checkfulladd2 (int, float[ ]);
void descendselection (int, float[ ]);
void rmfirstprice (int, ofstream&, float[ ]);
void ascendbubble (int, float[ ]);
const int CAPACITY = 50;
float array[CAPACITY];
length = arraylength(fin);
resetarray (length, fin, array);
fout << "Program by Aaron Friedley MWF 1:00" << endl << endl
<< "Original List" << endl;
writefile (length, fout, array);
fout << endl << "Ten percent added" << endl;
addtenpercent (length, array);
writefile (length, fout, array);
resetarray (length, fin, array);
cout << endl << "Last price removed" << endl;
rmlastprice (length, array);
cout << endl << "Check if list is full, if not add 27.93 to list" << endl;
checkfulladd (length, array);
cout << endl << "Find smallest value and its position" << endl;
findsmallest (length, array);
cout << endl << "Find largest value and its position" << endl;
findsmallest (length, array);
fout << endl << "Delete last twenty percent of prices";
deltwentypercent (length, fout, array);
cout << endl << "Check if list is full, if not add 54.28 to list" << endl;
checkfulladd2 (length, array);
fout << endl << "Sort list descending with selection sort";
descendselection (length, array);
writefile (length, fout, array);
resetarray (length, fin, array);
cout << endl << "Last price removed" << endl;
rmfirstprice (length, fout, array);
fout << endl << "Sort list in ascending order with bubble sort";
ascendbubble (length, array);
writefile (length, fout, array);
fin.close ();
fout.close ();
} // main
// *** openfiles ***
// Function opens input file hwk02.in and output hwk02.out streams
// If file cannot be opened error is displayed and program exits
void openfiles (ifstream& fin, ofstream& fout)
{
fin.open("hwk02.in");
if (fin.fail())
{
cout << "File not found.\n";
exit (1);
} // if
fout.open("hwk02.out");
if (fout.fail())
{
cout << "File could not be opened.\n";
exit (2);
} // if
} // openfiles
// *** arraylength ***
// Reads file incrementing integer length each time new data is found,
// then returns the length to main for initilization of an array
int arraylength(ifstream& fin)
{
char temp;
int length = 0;
while (fin.get(temp))
{
length++;
}// while
return length;
} // arraylength
// *** resetarray ***
// Function reads values from file and writes those values into array
void resetarray (int length, ifstream& fin, float array[ ])
{
int walker;
float temp;
for (walker = 0; walker < length; walker++)
{
fin >> temp;
array[walker] = temp;
} //for
} // resetarray
// *** writefile ***
// Writes values stored in the array to hwk02.out
void writefile (int length, ofstream &fout, float array[ ])
{
int walker;
fout << "---------------" << endl;
for (walker = 0; walker < length; walker++)
fout << "\t" << array[length] << endl;// for
} // writefile
// *** displayarray ***
// Displays values stored in array to screen
void displayarray (int length, float array[ ])
{
int walker;
cout << "---------------" << endl;
for (walker = 0; walker < length; walker++)
cout << "\t" << array[length] << endl;// for
}
// *** addtenpercent ***
// Adds ten percent to array values
void addtenpercent (int length, float array[ ])
{
int walker;
int temp;
int temp2;
for (walker = 0; walker < length; walker++)
{
temp = array[walker];
temp2 = array[walker] * 0.10;
array[walker] = temp + temp2;
} // for
} // addtenpercent
// *** rmlastprice ***
// Remove last price in array
void rmlastprice (int length, float array[ ])
{
int walker;
cout << "---------------" << endl;
for (walker = 0; walker < length-1; walker++)
cout << "\t" << array[length] << endl; // for
} // rmlastprice
// *** checkfulladd ***
// Check if list < 50, if not add 27.93 to end of list
void checkfulladd (int length, float array[ ])
{
int walker;
if (length < 50)
{
array[length+1] = 27.93;
cout << "---------------" << endl;
for (walker = 0; walker < length+1; walker++)
cout << "\t" << array[length]; // for
} // if
} // checkfulladd
// *** findsmallest ***
// Find smallest number in array, and its position then print to screen
void findsmallest (int length, float array[ ])
{
int walker;
int temp;
int temp2;
temp = array[0];
for (walker = 1; walker < length; walker++)
{
if (temp > array[walker])
{
temp2 = array[walker];
temp = walker;
} // if
} // for
cout << "---------------" << endl;
cout << "\t" << temp2 << endl << "\t" << temp;
} // findsmallest
// *** findlargest ***
// Find largest number in array, and its position then print to screen
void findlargest (int length, float array[ ])
{
int walker;
int temp;
int temp2;
temp = array[0];
for (walker = 1; walker < length; walker++)
{
if (temp < array[walker])
{
temp2 = array[walker];
temp = walker;
} // if
} // for
cout << "---------------" << endl;
cout << "\t" << temp2 << endl << "\t" << temp;
} // findlargest
// *** deltwentypercent ***
// Delete last 20 percent of prices from the list and write to file
void deltwentypercent (int length, ofstream& fout, float array[ ])
{
int temp;
int walker;
temp = length * .20;
fout << "---------------" << endl;
for (walker = 0; walker < length-temp; walker++)
fout << "\t" << array[length]; // for
} // deltwentypercent
// *** checkfulladd2 ***
// Check if list < 50, if not add 54.28 to end of list
void checkfulladd2 (int length, float array[ ])
{
int walker;
const int length2 = length + 1;
if (length < 50)
{
array[length+1] = 54.28;
cout << "---------------" << endl;
for (walker = 0; walker < length+1; walker++)
cout << "\t" << array[length]; // for
} // if
} // checkfulladd2
// *** descendselection ***
// Arrange array in descending order using selection sort
void descendselection (int length, float array[ ])
{
int current;
void exchangeLargest (int, float[ ], int);
for (current = 0; current < length; current++)
exchangeLargest (length, array, current);
} // descendselection
// *** exchangeLargest ***
// Second half of descendselection function
// Arranges array in descending order using selection sort
void exchangeLargest (int length, float array[ ], int current)
{
int walker;
int largest;
int temp;
largest = current;
for (walker = current + 1; walker <=length; walker++)
{
if (array[walker] > array[largest])
largest = walker; // if
}// for
temp = array[current];
array[current] = array[largest];
array[largest] = temp;
} // exchangeLargest
// *** rmfirstprice ***
// Removes first price from array
void rmfirstprice (int length, ofstream& fout, float array[ ])
{
int walker;
fout << "---------------" << endl;
for (walker = 1; walker < length; walker++)
fout << "\t" << array[length]; // for
} // rmfirstprice
// *** ascendbubble ***
// Sort list in ascending order using bubble sort
void ascendbubble (int length, float array[ ])
{
int current;
void bubbleUp (int, float[ ], int);
for (current = 0; current < length; current++)
bubbleUp (length, array, current);
} // ascendbubble
// *** bubbleUp ***
// Second half of ascendbubble
// Sort list in ascending order using bubble sort
void bubbleUp (int length, float array[ ], int current)
{
int walker;
int temp;
for (walker = length; walker > current; walker--)
{
if (array[walker] < array[walker-1])
{
temp = array[walker];
array[walker] = array[walker-1];
array[walker-1] = temp;
} // if
} // for
} // bubbleUp
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
Yes I know its messy.
Comments
Although I can't offer constructive advice on this particular problem. I seriously suggest that in the future that you simply read the entire file into memory through fread() all at once and parse it manually byte by byte.
Anyway, I think the problem is that the return of the ifstream.get(char &c) method is not usefull for determining the end of file (it's been a while, but the reference seems to indicate this). You should probably use ifstream.eof() in your while loop, which will return true at end of file or false otherwise.
After a second once over, you seem to be attempting to use fin before you open the file. Am I missing something or do you never actually call openfiles?
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
char temp;
do
{
file >> char;
++count;
}while(!file.eof());
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
I've also had problems with too many chars being read, and this seems to solve it.
Anyway, I think the problem is that the return of the ifstream.get(char &c) method is not usefull for determining the end of file (it's been a while, but the reference seems to indicate this). You should probably use ifstream.eof() in your while loop, which will return true at end of file or false otherwise.
After a second once over, you seem to be attempting to use fin before you open the file. Am I missing something or do you never actually call openfiles? <!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->
Yep. I deserve a big 'You got served.' I never even opened it. Meh.... I feel stupid now <!--emo&:(--><img src='http://www.unknownworlds.com/forums/html//emoticons/sad.gif' border='0' style='vertical-align:middle' alt='sad.gif' /><!--endemo-->
*Edit*
Ok now I've got the problem that it won't compile.... giving me the error that arraylength doesn't return a value.... even though i have return length in it....
Either way, it's something obfuscating the return line. Not the lack of a return line.
EDIT: That, or the crazy function predeclarations. I've never actually seen the style of putting it inside another function. That's crazy talk.
Yes, you do. Whoops. Fixed.
I finally debugged the thing (I think) and it seems to compile alright, but when I try to run the executable, the command prompt window minimizes and opens up another copy of notepad with the program in it, and I dont get any output in the command prompt window either.
Any help would be <i>greatly</i> appreciated - I'm starting to tear my hair out. <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html//emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif' /><!--endemo-->
(I also realize there's some extra stuff not needed here - I'll trim that out once I get it workin)
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
/* proggy to use simpson's rule to approximate area under
? an arbitrary function between two x values */
#include <stdio.h>
#include <math.h>
typedef struct {
?float a1, a2;
} DATA;
typedef struct {
?float (*function)(float);
} FUNCTION;
int simpson(FUNCTION, float xmin, float xmax, float acc, int *jmax, float *area);
float func1(float);
float func2(float);
void tries(int);
int main(void) {
?DATA mydata1, mydata2;
?FUNCTION myfunc1, myfunc2;
?float x1min=1.0, x1max=2.0, x2min=0, x2max=3.1415926539, acc=.00001, area;
?void *parea, *prepeats;
?int ans1, ans2, repeats=200;
?parea=&area;
?prepeats=&repeats;
?mydata1.a1=x1min; ? ? ? ? mydata2.a1=x2min;
?mydata1.a2=x1max; ? ? ? ? mydata2.a2=x2max;
?myfunc1.function=&func1; ?myfunc2.function=&func2;
?ans1 = simpson(myfunc1, mydata1.a1, mydata1.a2, acc, prepeats, parea);
?printf("\nThe approx. area under function 1 from %f to %f is %f, and it took %f tries.\n",
? ? ? ? mydata1.a1, mydata1.a2, &area, &repeats);
?tries(ans1);
?ans2 = simpson(myfunc2, mydata2.a1, mydata2.a2, acc, prepeats, parea);
?printf("\nThe approx. area under function 2 from %f to %f is %f, and it took %f tries.\n",
? ? ? ? mydata2.a1, mydata2.a2, &area, &repeats);
?tries(ans2);
?return 0;
}
int simpson(FUNCTION fun, float a, float b, float tol, int *jmax, float *area) {
?int temp1=0, i=0, temp2=0;
?float h, new_area, old_area, base, N=2, test, c, d;
?h=(b-a)/N;
?base = (h/3) * ( fun.function(a) + fun.function(b) +
? ? ? ? 4 * fun.function(a+h) );
?old_area=base;
?new_area=0;
?while (test>tol) {
? ?N+=2;
? ?h=(b-a)/N;
? ?base = (h/3) * ( fun.function(a) + fun.function(b) +
? ? ? ? ? 4 * fun.function(a+h) );
? ?while (temp2<=N) {
? ? ?c=a+(2+temp1)*h;
? ? ?d=a+(3+temp1)*h;
? ? ?new_area += (h/3) * ( 2 * fun.function(c)+
? ? ? ? ? ? ? ? ?4 * fun.function(d) );
? ? ?temp1+=2;
? ? ?temp2=temp1+4;
? ?}
? ?new_area+=base;
? ?test=new_area-old_area;
? ?if (new_area-old_area<0) {
? ? ?test*=-1; }
? ?old_area=new_area;
? ?i++;
?}
?*area=new_area;
?if ((test<tol)&&(i<=*jmax)) {
? ?*jmax=i;
? ?return 1;
?}
?return 0;
}
float func1(float x) {
?float y, fx;
?y=x;
?fx=(3*x*y)-2*y+1;
?return fx;
}
float func2(float x) {
?float fx;
?fx=sin(x/2);
?return fx;
}
void tries(int ans) {
?printf("Desired accuracy was ");
?if (ans==0) {
? ?printf("not "); }
?printf("achieved within\n max number of iterations.\n");
}
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
EDIT: punctuation
Yes it is. But IIRC Otto's teacher is a moron and believes that this is some form of optimization.
Yes it is. But IIRC Otto's teacher is a moron and believes that this is some form of optimization. <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
Ive got a different professor now, but she's still convinced that unless the function is called a TON between different functions, nothing should be global.
Although, on second thoughts I can vaguely see why you would not make the function declarations global; it's a kind of crude encapsulation. I can grudgingly see a point to it. I still don't like it, though.
There's absolutely nothing wrong with global scope when tempered properly. It can be easily argued that a program that uses all globals and has no parameters to functions is faster in all respects. The only downside is potential clutter. But breaking it down to a line-by-line basis it's actually less clutter overall to declare a function in global scope than it is in local scope.
I friggin hate people that decide to follow self-imposed limitation when there's no benefit for the sake of it being "proper" (which it isn't).
These are the same people that re-use local function variables because it's "faster" than declaring more (which is LITERALLY a free operation right up until the moment that you overflow the stack).
GWARRHRHEHREHRHWHEWHRWMEERMERNNEESEREREH!!!!