iccaros Posted January 15, 2006 Report Share Posted January 15, 2006 ok let start with what this is tring to be..I am writing a program to help me in my statistics classit shoudl take data from a file (numbers double) and group them into slots (example we have readings from 100 people and 8 groups split in to .4 segments starign at a number , 8 people showed group 1, 10 group 2 ect...)here is my first byte at the code..#include<iostream>#include <fstream>#include <string>using namespace std;int check_freq(double x);int main(){double Data;string display ("00000000");ofstream myfile ("test.txt");if (myfile.is_open() ){getline (myfile,Data);display (check_freq(Data)) += 1;}myfile.close();}else cout <<"Unable to open file" << endl;cout << display;return 0;}int check_freq (double x){int place = 0;if ( x =< 96.8 ){place = 0;return place;}else if (x > 96.8 & < 97.3){place =1;return place;}else if (x > 97.2 & < 97.7 ){place = 2;return place;}else if (x > 97.6 & < 98.1 ){place = 3;return place;}else if (x > 98 & < 98.5){place = 4;return place;}else if (x > 98.4 & < 98.9){place = 5;return place;}else if ( x > 98.8 & < 99.3){place = 6;return place;}else if ( x => 99.3){place = 7;return place;}cout << no match;return 0;}[code]here is the g++ errorsg++ prob2-10.cppprob2-10.cpp: In function `int main()':prob2-10.cpp:19: error: invalid conversion from `void*' to `char**'prob2-10.cpp:19: error: cannot convert `double' to `size_t*' for argument `2' to `__ssize_t getline(char**, size_t*, FILE*)'prob2-10.cpp:20: error: no match for call to `(std::string) (int)'prob2-10.cpp: At global scope:prob2-10.cpp:24: error: syntax error before `else'prob2-10.cpp:26: error: syntax error before `<<' tokenprob2-10.cpp: In function `int check_freq(double)':prob2-10.cpp:35: error: syntax error before `<' tokenprob2-10.cpp: At global scope:prob2-10.cpp:40: error: syntax error before `else'prob2-10.cpp:76: error: syntax error before `<<' tokenhere is my input file[code]uskeyw@huskeyw ~/projects/school/statistics $ cat test.txt98.698.698989998.498.498.498.498.698.698.898.6979798.897.697.798.8989898.398.597.398.797.498.998.699.597.597.397.698.299.698.799.498.29898.698.697.298.498.698.29897.89898.498.698.697.89996.597.69896.997.697.197.998.497.39897.597.698.298.598.898.797.89897.197.499.498.498.698.498.598.698.398.798.899.198.697.998.89898.798.598.998.498.697.197.998.898.797.698.299.297.89898.497.898.497.49897I have just started withthis but some pointer (maybe not tell me hay line 10 needs to say this.. but say you need to convert the string to a double or something..) or look at your if statments agian.. Quote Link to post Share on other sites
iccaros Posted January 15, 2006 Author Report Share Posted January 15, 2006 (edited) ok changed my first if (in main) to readif (myfile.is_open() ){ while (! myfile.eof() ){getline (myfile,Data);display (check_freq(Data)) += 1;}myfile.close();}else cout <<"Unable to open file" << endl;cout << display;return 0;}that should make that a loopmy errors are nowhuskeyw@huskeyw ~/projects/school/statistics $ g++ prob2-10.cppprob2-10.cpp: In function `int main()':prob2-10.cpp:21: error: invalid conversion from `void*' to `char**'prob2-10.cpp:21: error: cannot convert `double' to `size_t*' for argument `2' to `__ssize_t getline(char**, size_t*, FILE*)'prob2-10.cpp:22: error: no match for call to `(std::string) (int)'prob2-10.cpp: In function `int check_freq(double)':prob2-10.cpp:37: error: syntax error before `<' tokenprob2-10.cpp: At global scope:prob2-10.cpp:42: error: syntax error before `else'prob2-10.cpp:78: error: syntax error before `<<' token Edited January 15, 2006 by iccaros Quote Link to post Share on other sites
jcl Posted January 15, 2006 Report Share Posted January 15, 2006 (edited) You seem to have changed quite a bit more than that conditional judging from the compiler output. Working from what you posted.#include <iostream>#include <fstream>#include <string>using namespace std;int check_freq(double x);int main(){ double Data; string display ("00000000"); ofstream myfile ("test.txt");Should be an ifstream, I believe if (myfile.is_open() ) { while (! myfile.eof() ) { getline (myfile,Data);You probably want myfile >> Data;. The source you posted and the source you ran through GCC used different getline()s (the global from <string> and the istream member) and neither them can do what you're trying to do here. In any case you need to validate the input. display (check_freq(Data)) += 1;I assume that's supposed to be display[check_freq(Data)]. You want to be using a vector (or array of something) instead of a string. } myfile.close(); } else cout <<"Unable to open file" << endl; cout << display; return 0;}int check_freq (double x){ int place = 0; if ( x =< 96.8 )Should be <= here and >= at the bottom. place doesn't serve any purpose. { place = 0; return place; } else if (x > 96.8 & < 97.3)You need the variable in both comparisons and you should be using && all through here: & is bitwise-and. Doesn't matter in this case, but still. Actually you can drop the first comparison completely; you already know that x > 96.8 here. { place =1; return place; } else if (x > 97.2 & < 97.7 ) { place = 2; return place; } else if (x > 97.6 & < 98.1 ) { place = 3; return place; } else if (x > 98 & < 98.5) { place = 4; return place; } else if (x > 98.4 & < 98.9) { place = 5; return place; } else if ( x > 98.8 & < 99.3) { place = 6; return place; } else if ( x => 99.3) { place = 7; return place; } cout << no match;Quotes. return 0;}Returning 0 here seems wrong. That should be a Can't Happen so you could just drop the error message and return (unless the compiler complains). If it's not Can't Happen you should probably have some kind of error handling. Edited January 15, 2006 by jcl Quote Link to post Share on other sites
iccaros Posted January 16, 2006 Author Report Share Posted January 16, 2006 (edited) ok here is what I have so far (added some couts to what is going on)I have to change to an array, but this compiles.. one step closer.. thank jcl for the suggestions.. huskeyw@huskeyw ~/projects/school/statistics $ cat prob2-10.cpp#include<iostream>#include <fstream>#include <cstring>using namespace std;int check_freq(double x);int main(){double Data;string display; //("00000000");ifstream myfile ("test.txt", ios::in);if (myfile.is_open() ){ while (! myfile.eof() ){myfile >> Data;cout << Data << endl;display [check_freq(Data)] += 1;}myfile.close();}else cout <<"Unable to open file" << endl;for (int y =0; y <=7; y++)cout << display[y];return 0;}//--------------------------functionsint check_freq (double x){int place;if ( x <= 96.8 ){place = 0;cout << "add 1 to group 1" << endl;return place;}else if (x < 97.3){place =1;cout << "add 1 to group 2" << endl;return place;}else if (x < 97.7 ){place = 2;return place;}else if (x < 98.1 ){place = 3;return place;}else if (x < 98.5){place = 4;return place;}else if (x < 98.9){place = 5;return place;}else if ( x < 99.3){place = 6;return place;}else if ( x >= 99.3){place = 7;return place;}cout << "no match" << endl;return 0;}this is not working (so I have to find another way.. I'll work on this part tonight)display [check_freq(Data)] += 1; <<-----this I was hoping woudl add one to the position being ++for (int y =0; y <=7; y++) <<-----------this works but since the above no worky.. this puts out bad data. cout << display[y];also one last thing, I change to cstrig as string had an error, will try agian with string.. but I need to reread up on c++ arrays Edited January 16, 2006 by iccaros Quote Link to post Share on other sites
jcl Posted January 16, 2006 Report Share Posted January 16, 2006 The code that uses display is fine, you just need to declare it as something else. Bothint display[] = {0,0,0,0,0,0,0,0};vector<int> display(8, 0);would be fine. (I can't remember if C++ automatically initializes array elements so I did it the old-fashioned way. The vector ctor above creates an eight element vector with elements initialized to 0). The vector solution is preferrable but if you aren't familiar with the container classes you might want to stick with C arrays. Quote Link to post Share on other sites
iccaros Posted January 16, 2006 Author Report Share Posted January 16, 2006 (edited) ok thanks to jcl, I have working code (at least it parses and accumaltes)I am also reading this site (due to the suggestion) http://www.cppreference.com/cppvector/for any one intrested here is the code to date..to dotake input on file namecreate the array with the size picked for output (say 3 slots or 20 slots for graphs)read data (from file) into an array (I think it will be easyer to handle this way, wrong?)as with above.. need to create a dynamic array to hold date based on data in the filefind min and max data rangeuse the size of output to get width of data, like in this example we have 8 slots and width of .4here is the simi (it works so far, not clean but works.... ) huskeyw@huskeyw ~/projects/school/statistics $ cat prob2-10.cpp#include<iostream>#include <fstream>#include <string>using namespace std;int check_freq(double x);int main(){double Data;int display[8]={0};ifstream myfile ("test.txt", ios::in);if (myfile.is_open() ){ while (! myfile.eof() ){myfile >> Data;//cout << Data << endl;display [check_freq(Data)] += 1;}myfile.close();}else cout <<"Unable to open file" << endl;for (int y =0; y <=7; y++){cout << display[y];cout << " ";}return 0;}//--------------------------functionsint check_freq (double x){int place;if ( x <= 96.8 ){place = 0;//cout << "add 1 to group 1" << endl;return place;}else if (x < 97.3){place =1;//cout << "add 1 to group 2" << endl;return place;}else if (x < 97.7 ){place = 2;return place;}else if (x < 98.1 ){place = 3;return place;}else if (x < 98.5){place = 4;return place;}else if (x < 98.9){place = 5;return place;}else if ( x < 99.3){place = 6;return place;}else if ( x >= 99.3){place = 7;return place;}cerr << "no match" << endl;}jcl, I took out the return 0; I belive I need an error (maby output data is not in range and ask for a new range..but then if it self dicovers max and min this will not be needed)also I think the if staments can be replaced.. will test..Thanks for the suggestions.. I perfer that to someone saying replace line 10 with this.. it makes me read.. Edited January 16, 2006 by iccaros Quote Link to post Share on other sites
iccaros Posted January 17, 2006 Author Report Share Posted January 17, 2006 (edited) help...ok Its late.. but why is this now segfaulting?I must be overloading something..newest version#include<iostream>#include <fstream> //file handaling#include <string> //strings, not sure if I need this#include<vector> // for c++ containersusing namespace std;//-----------------------funtions protypingint check_freq(double file_data, double width , double start_scale);//------------------------------main functionint main(){ double file_data; vector<int> display (8,0); //vector that holds the output.. type int double width = .4; double start_scale = 96.5; ifstream myfile ("test.txt", ios::in); //open file for reading if (myfile.is_open() )// check if file is open { while (! myfile.eof() ) //while not end of file do { myfile >> file_data; //write input to Data type double display [check_freq(file_data,width,start_scale)] += 1; //pass data to check_freq() funtion and return what position of display needs incramented } myfile.close();//close file } else cerr <<"Unable to open file" << endl; //error handaling for file open //---------------------------- temp disply--- needs moved to a funtion or its own headder for (int y =0; y <=7; y++) { cout << display.at(y); cout << " | "; } cout << endl; return 0;}//--------------------------end main funtion//--------------------------functions//---------function check_freq, this should be a loop not if statments that incrament the check based on width and slot amounts using min/maxint check_freq (double file_data, double width , double start_scale){ for (int x = 0; x <= width; x++) { if (file_data > start_scale + width ) return x; start_scale += width; } }./a.outSegmentation faultno compile errors?-----------editchanged funtions to fix for (int x = 0; x <= width; x++) as I do not want this I need to add a int slot so I changed to this for my funtion prototypeint check_freq (double file_data, double width , double start_scale, int slots);in int main() added int slots =7;function//---------function check_freq, this should be a loop not if statments that incrament the check based on width and slot amounts using min/maxint check_freq (double file_data, double width , double start_scale, int slots){ for (int x = 0; x <= slots; x++) { if (file_data > start_scale + width ) return x; start_scale += width; } }but still get a ./a.outSegmentation fault Edited January 17, 2006 by iccaros Quote Link to post Share on other sites
iccaros Posted January 17, 2006 Author Report Share Posted January 17, 2006 (edited) ok I changed funtion to //---------function check_freq, this should be a loop not if statments that incrament the check based on width and slot amounts using min/maxint check_freq (double file_data, double width , double start_scale, int slots){ for (int x = 0; x <= slots; x++) { cout << "start_scale =: " << start_scale << endl; cout << "file_data =: " << file_data << endl; if (file_data > start_scale + width ) return x; start_scale += width; } return slots; }no segfault but it does not loop the check? here is the outputhuskeyw@huskeyw ~/projects/school/statistics $ ./a.outstart_scale =: 96.5file_data =: 98.6start_scale =: 96.5file_data =: 98.6start_scale =: 96.5file_data =: 98start_scale =: 96.5file_data =: 98start_scale =: 96.5file_data =: 99start_scale =: 96.5file_data =: 98.4start_scale =: 96.5file_data =: 98.4start_scale =: 96.5file_data =: 98.4start_scale =: 96.5file_data =: 98.4start_scale =: 96.5file_data =: 98.6start_scale =: 96.5file_data =: 98.6start_scale =: 96.5file_data =: 98.8start_scale =: 96.5file_data =: 98.6start_scale =: 96.5file_data =: 97start_scale =: 96.5file_data =: 97start_scale =: 96.5file_data =: 98.8start_scale =: 96.5file_data =: 97.6start_scale =: 96.5file_data =: 97.7start_scale =: 96.5file_data =: 98.8start_scale =: 96.5file_data =: 98start_scale =: 96.5file_data =: 98start_scale =: 96.5file_data =: 98.3start_scale =: 96.5file_data =: 98.5start_scale =: 96.5file_data =: 97.3start_scale =: 96.5file_data =: 98.7start_scale =: 96.5file_data =: 97.4start_scale =: 96.5file_data =: 98.9start_scale =: 96.5file_data =: 98.6start_scale =: 96.5file_data =: 99.5start_scale =: 96.5file_data =: 97.5start_scale =: 96.5file_data =: 97.3start_scale =: 96.5file_data =: 97.6start_scale =: 96.5file_data =: 98.2start_scale =: 96.5file_data =: 99.6start_scale =: 96.5file_data =: 98.7start_scale =: 96.5file_data =: 99.4start_scale =: 96.5file_data =: 98.2start_scale =: 96.5file_data =: 98start_scale =: 96.5file_data =: 98.6start_scale =: 96.5file_data =: 98.6start_scale =: 96.5file_data =: 97.2start_scale =: 96.5file_data =: 98.4start_scale =: 96.5file_data =: 98.6start_scale =: 96.5file_data =: 98.2start_scale =: 96.5file_data =: 98start_scale =: 96.5file_data =: 97.8start_scale =: 96.5file_data =: 98start_scale =: 96.5file_data =: 98.4start_scale =: 96.5file_data =: 98.6start_scale =: 96.5file_data =: 98.6start_scale =: 96.5file_data =: 97.8start_scale =: 96.5file_data =: 99start_scale =: 96.5file_data =: 96.5start_scale =: 96.9file_data =: 96.5start_scale =: 97.3file_data =: 96.5start_scale =: 97.7file_data =: 96.5start_scale =: 98.1file_data =: 96.5start_scale =: 98.5file_data =: 96.5start_scale =: 98.9file_data =: 96.5start_scale =: 99.3file_data =: 96.5start_scale =: 96.5file_data =: 97.6start_scale =: 96.5file_data =: 98start_scale =: 96.5file_data =: 96.9start_scale =: 96.5file_data =: 97.6start_scale =: 96.5file_data =: 97.1start_scale =: 96.5file_data =: 97.9start_scale =: 96.5file_data =: 98.4start_scale =: 96.5file_data =: 97.3start_scale =: 96.5file_data =: 98start_scale =: 96.5file_data =: 97.5start_scale =: 96.5file_data =: 97.6start_scale =: 96.5file_data =: 98.2start_scale =: 96.5file_data =: 98.5start_scale =: 96.5file_data =: 98.8start_scale =: 96.5file_data =: 98.7start_scale =: 96.5file_data =: 97.8start_scale =: 96.5file_data =: 98start_scale =: 96.5file_data =: 97.1start_scale =: 96.5file_data =: 97.4start_scale =: 96.5file_data =: 99.4start_scale =: 96.5file_data =: 98.4start_scale =: 96.5file_data =: 98.6start_scale =: 96.5file_data =: 98.4start_scale =: 96.5file_data =: 98.5start_scale =: 96.5file_data =: 98.6start_scale =: 96.5file_data =: 98.3start_scale =: 96.5file_data =: 98.7start_scale =: 96.5file_data =: 98.8start_scale =: 96.5file_data =: 99.1start_scale =: 96.5file_data =: 98.6start_scale =: 96.5file_data =: 97.9start_scale =: 96.5file_data =: 98.8start_scale =: 96.5file_data =: 98start_scale =: 96.5file_data =: 98.7start_scale =: 96.5file_data =: 98.5start_scale =: 96.5file_data =: 98.9start_scale =: 96.5file_data =: 98.4start_scale =: 96.5file_data =: 98.6start_scale =: 96.5file_data =: 97.1start_scale =: 96.5file_data =: 97.9start_scale =: 96.5file_data =: 98.8start_scale =: 96.5file_data =: 98.7start_scale =: 96.5file_data =: 97.6start_scale =: 96.5file_data =: 98.2start_scale =: 96.5file_data =: 99.2start_scale =: 96.5file_data =: 97.8start_scale =: 96.5file_data =: 98start_scale =: 96.5file_data =: 98.4start_scale =: 96.5file_data =: 97.8start_scale =: 96.5file_data =: 98.4start_scale =: 96.5file_data =: 97.4start_scale =: 96.5file_data =: 98start_scale =: 96.5file_data =: 97start_scale =: 96.5file_data =: 97106 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |also Its adding a routine there is only 106 data points ( I am sure this is the last return but that stoped the segfault?)thanks for any helpP.sthis is not a school homework.. I decided to create a tool to help me quickly graph fro my statistics class isnce I am excel encombered Edited January 17, 2006 by iccaros Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.