im getting the following error on a for loop, what does this mean?Code:arning C4552: '<' : operator has no effect; expected operator with side-effect
im getting the following error on a for loop, what does this mean?Code:arning C4552: '<' : operator has no effect; expected operator with side-effect
When no one helps you out. Call google();
well here it is, Andy helped me figure out the reading part of the file but now im trying to get the average of the numbers in the array. so here is what i got so far.
Code:#include <iostream.h> #include <fstream> #include <stdlib.h> using namespace std; const int FILENAMELEN = 30, ARRAYLEN = 4; // The class array class array { public: // These functions are avaiable for use outside the class inline int getsize() {return(size);} int readarray(); float findaverage(); array(); private: //Private - available only to member functions int x[ARRAYLEN]; int size; int j; int numbers[20]; int count; float average; float sum; char filename[FILENAMELEN]; char inchar; }; //initialization constructor array::array(void) { size = 0; } int array::readarray() { ifstream infile; char filename[FILENAMELEN]; char inchar; cout<<"Enter filename->" <<'\t'; cin>> filename; //openfile infile.open(filename); //if you cant open the file, print error message if (!infile) { cerr<< "Cannot open " <<filename << endl; exit(1); } //Read the file character while (!infile.eof()) { inchar = infile.get(); //this reads in even whitespaces cout << inchar; //display it on screen } infile.close(); return(0); } float array::findaverage() { int j; float average; sum =0.0; for (j<0; j<count; j++) { sum+=numbers[j]; } average = sum/j; cout << "The average is "<< average <<endl; return 0; } int main() { array myarray; myarray.readarray(); myarray.findaverage(); return(0); }
When no one helps you out. Call google();
for (j<0; j<count; j++)
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
ah yes, jesus im blind
ok i corrected the for loop but now the result is ugly, no errors, no warnings, but an ugly wrong result
any ideas?Code:-1. #IND
Last edited by InvariantLoop; 02-12-2005 at 07:46 PM.
When no one helps you out. Call google();
This means the result was not a number/indeterminate. You get this result when the operation is not mathematically defined.Code:-1. #IND
http://stevehollasch.com/cgindex/coding/ieeefloat.html
http://www.google.com/search?q=%23INDeterminate+nan
You are getting this result because you are performing maths on an array of numbers that have not been initialised.
wait im confused, the numbers contained in the array have not been initialized? I am able to read the array and display the values that is holding, why cant i operate on those values since im able to read them? any ideas how i can go about it then so i can work on those numbers?
When no one helps you out. Call google();
>>I am able to read the array and display the values that is holding
Review readarray() again. I see where it reads the material in the file into a single char and displays that char on the screen. But...I don't see where it stores the material read in anywhere, let alone read in an int that could be stored in numbers[] such that the values in numbers[] could then be used in findaverage().
Read the file contents into an int and store the value of the int in numbers[], or better, in my opinion, use a loop to store the value read from the file directly into numbers[], and get rid of !infile.eof() as the conditional of the loop, which is likely to get you into trouble (likely to read in one more value than you had planned on).
You're only born perfect.
i searched on the forums can i use atoi something for this? to convert the char into int? someone give me a few hints please
When no one helps you out. Call google();
Once you read your file into an array, you can use atoi to convert it into a number. Here is an example prog to demonstrate this:
Code:#include <iostream> #include <ctype.h> #include <cstdlib> using namespace std; int main() { char myString[5] = {'1', ' ', '2', ' ', '3'}; int myInt[5] = {0}; int count = 0, sum = 0; for(int i = 0; i < 5; i++){ if(isdigit(myString[i])){ myInt[count++] = atoi(&myString[i]); } } for(int i = 0; i < count; i++){ sum += myInt[i]; } cout << endl << sum << endl; return 0; }
i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced
It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah
Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem
ok ill work on that. thanks man
When no one helps you out. Call google();
for the love of god i still cant figure this out, can someone take a look at my code please the reading is the only thing holding me back, i created another function similiar to findaverage() but if i cant read the numbers then i cant do any work on that either.
When no one helps you out. Call google();
Ok here is your while loop where you are reading from the file:
Notice you are not storing your information anywhere. Somewhere in that while loop you are going to need to store the char in inchar into an array or some form of container.Code://Read the file character while(!infile.eof()) { inchar = infile.get(); //this reads in even whitespaces cout << inchar; //display it on screen } infile.close();
So ask yourself, Do I know how large I need to make the array?(aka how many characters are going to be read from the file)
If you do simply create a char array as one of your private members and then add inchar to it like so:
Now if you don't know the size you can either size the array dynamically with the new operator or the easiest way of handling this in C++ is to use a container object such as a vector.Code://Defined in class char fileInput[fileSize]; //In your read function int currentChar = 0; //Read the file character while(!infile.eof()) { fileInput[currentChar] = infile.get(); //this reads in even whitespaces cout << fileInput[currentChar]; //display it on screen currentChar++; } infile.close();
I have a simple prog that loads a file into a vector. Let me know if you want to see it, I am assuming that you want to figure this out for yourself though.
i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced
It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah
Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem