C++ Help Read File


Recommended Posts

ok let start with what this is tring to be..

I am writing a program to help me in my statistics class

it 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++ errors
g++ prob2-10.cpp
prob2-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 `<<' token
prob2-10.cpp: In function `int check_freq(double)':
prob2-10.cpp:35: error: syntax error before `<' token
prob2-10.cpp: At global scope:
prob2-10.cpp:40: error: syntax error before `else'
prob2-10.cpp:76: error: syntax error before `<<' token


here is my input file
[code]

uskeyw@huskeyw ~/projects/school/statistics $ cat test.txt
98.6
98.6
98
98
99
98.4
98.4
98.4
98.4
98.6
98.6
98.8
98.6
97
97
98.8
97.6
97.7
98.8
98
98
98.3
98.5
97.3
98.7
97.4
98.9
98.6
99.5
97.5
97.3
97.6
98.2
99.6
98.7
99.4
98.2
98
98.6
98.6
97.2
98.4
98.6
98.2
98
97.8
98
98.4
98.6
98.6
97.8
99
96.5
97.6
98
96.9
97.6
97.1
97.9
98.4
97.3
98
97.5
97.6
98.2
98.5
98.8
98.7
97.8
98
97.1
97.4
99.4
98.4
98.6
98.4
98.5
98.6
98.3
98.7
98.8
99.1
98.6
97.9
98.8
98
98.7
98.5
98.9
98.4
98.6
97.1
97.9
98.8
98.7
97.6
98.2
99.2
97.8
98
98.4
97.8
98.4
97.4
98
97

I 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..

Link to post
Share on other sites

ok changed my first if (in main) to read

if (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 loop

my errors are now

huskeyw@huskeyw ~/projects/school/statistics $ g++ prob2-10.cpp

prob2-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 `<' token

prob2-10.cpp: At global scope:

prob2-10.cpp:42: error: syntax error before `else'

prob2-10.cpp:78: error: syntax error before `<<' token

Edited by iccaros
Link to post
Share on other sites

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 by jcl
Link to post
Share on other sites

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;
}

//--------------------------functions


int 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 by iccaros
Link to post
Share on other sites

The code that uses display is fine, you just need to declare it as something else. Both

int		 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.

Link to post
Share on other sites

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 do

take input on file name

create 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 file

find min and max data range

use the size of output to get width of data, like in this example we have 8 slots and width of .4

here 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;
}

//--------------------------functions


int 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 by iccaros
Link to post
Share on other sites

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++ containers

using namespace std;

//-----------------------funtions protyping
int check_freq(double file_data, double width , double start_scale);





//------------------------------main function
int 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/max
int 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.out

Segmentation fault

no compile errors?

-----------edit

changed 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

prototype

int 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/max
int 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.out

Segmentation fault

Edited by iccaros
Link to post
Share on other sites

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/max
int 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 output

huskeyw@huskeyw ~/projects/school/statistics $ ./a.out

start_scale =: 96.5

file_data =: 98.6

start_scale =: 96.5

file_data =: 98.6

start_scale =: 96.5

file_data =: 98

start_scale =: 96.5

file_data =: 98

start_scale =: 96.5

file_data =: 99

start_scale =: 96.5

file_data =: 98.4

start_scale =: 96.5

file_data =: 98.4

start_scale =: 96.5

file_data =: 98.4

start_scale =: 96.5

file_data =: 98.4

start_scale =: 96.5

file_data =: 98.6

start_scale =: 96.5

file_data =: 98.6

start_scale =: 96.5

file_data =: 98.8

start_scale =: 96.5

file_data =: 98.6

start_scale =: 96.5

file_data =: 97

start_scale =: 96.5

file_data =: 97

start_scale =: 96.5

file_data =: 98.8

start_scale =: 96.5

file_data =: 97.6

start_scale =: 96.5

file_data =: 97.7

start_scale =: 96.5

file_data =: 98.8

start_scale =: 96.5

file_data =: 98

start_scale =: 96.5

file_data =: 98

start_scale =: 96.5

file_data =: 98.3

start_scale =: 96.5

file_data =: 98.5

start_scale =: 96.5

file_data =: 97.3

start_scale =: 96.5

file_data =: 98.7

start_scale =: 96.5

file_data =: 97.4

start_scale =: 96.5

file_data =: 98.9

start_scale =: 96.5

file_data =: 98.6

start_scale =: 96.5

file_data =: 99.5

start_scale =: 96.5

file_data =: 97.5

start_scale =: 96.5

file_data =: 97.3

start_scale =: 96.5

file_data =: 97.6

start_scale =: 96.5

file_data =: 98.2

start_scale =: 96.5

file_data =: 99.6

start_scale =: 96.5

file_data =: 98.7

start_scale =: 96.5

file_data =: 99.4

start_scale =: 96.5

file_data =: 98.2

start_scale =: 96.5

file_data =: 98

start_scale =: 96.5

file_data =: 98.6

start_scale =: 96.5

file_data =: 98.6

start_scale =: 96.5

file_data =: 97.2

start_scale =: 96.5

file_data =: 98.4

start_scale =: 96.5

file_data =: 98.6

start_scale =: 96.5

file_data =: 98.2

start_scale =: 96.5

file_data =: 98

start_scale =: 96.5

file_data =: 97.8

start_scale =: 96.5

file_data =: 98

start_scale =: 96.5

file_data =: 98.4

start_scale =: 96.5

file_data =: 98.6

start_scale =: 96.5

file_data =: 98.6

start_scale =: 96.5

file_data =: 97.8

start_scale =: 96.5

file_data =: 99

start_scale =: 96.5

file_data =: 96.5

start_scale =: 96.9

file_data =: 96.5

start_scale =: 97.3

file_data =: 96.5

start_scale =: 97.7

file_data =: 96.5

start_scale =: 98.1

file_data =: 96.5

start_scale =: 98.5

file_data =: 96.5

start_scale =: 98.9

file_data =: 96.5

start_scale =: 99.3

file_data =: 96.5

start_scale =: 96.5

file_data =: 97.6

start_scale =: 96.5

file_data =: 98

start_scale =: 96.5

file_data =: 96.9

start_scale =: 96.5

file_data =: 97.6

start_scale =: 96.5

file_data =: 97.1

start_scale =: 96.5

file_data =: 97.9

start_scale =: 96.5

file_data =: 98.4

start_scale =: 96.5

file_data =: 97.3

start_scale =: 96.5

file_data =: 98

start_scale =: 96.5

file_data =: 97.5

start_scale =: 96.5

file_data =: 97.6

start_scale =: 96.5

file_data =: 98.2

start_scale =: 96.5

file_data =: 98.5

start_scale =: 96.5

file_data =: 98.8

start_scale =: 96.5

file_data =: 98.7

start_scale =: 96.5

file_data =: 97.8

start_scale =: 96.5

file_data =: 98

start_scale =: 96.5

file_data =: 97.1

start_scale =: 96.5

file_data =: 97.4

start_scale =: 96.5

file_data =: 99.4

start_scale =: 96.5

file_data =: 98.4

start_scale =: 96.5

file_data =: 98.6

start_scale =: 96.5

file_data =: 98.4

start_scale =: 96.5

file_data =: 98.5

start_scale =: 96.5

file_data =: 98.6

start_scale =: 96.5

file_data =: 98.3

start_scale =: 96.5

file_data =: 98.7

start_scale =: 96.5

file_data =: 98.8

start_scale =: 96.5

file_data =: 99.1

start_scale =: 96.5

file_data =: 98.6

start_scale =: 96.5

file_data =: 97.9

start_scale =: 96.5

file_data =: 98.8

start_scale =: 96.5

file_data =: 98

start_scale =: 96.5

file_data =: 98.7

start_scale =: 96.5

file_data =: 98.5

start_scale =: 96.5

file_data =: 98.9

start_scale =: 96.5

file_data =: 98.4

start_scale =: 96.5

file_data =: 98.6

start_scale =: 96.5

file_data =: 97.1

start_scale =: 96.5

file_data =: 97.9

start_scale =: 96.5

file_data =: 98.8

start_scale =: 96.5

file_data =: 98.7

start_scale =: 96.5

file_data =: 97.6

start_scale =: 96.5

file_data =: 98.2

start_scale =: 96.5

file_data =: 99.2

start_scale =: 96.5

file_data =: 97.8

start_scale =: 96.5

file_data =: 98

start_scale =: 96.5

file_data =: 98.4

start_scale =: 96.5

file_data =: 97.8

start_scale =: 96.5

file_data =: 98.4

start_scale =: 96.5

file_data =: 97.4

start_scale =: 96.5

file_data =: 98

start_scale =: 96.5

file_data =: 97

start_scale =: 96.5

file_data =: 97

106 | 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 help

P.s

this 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 by iccaros
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...