|
-
Oct 5th, 2002, 04:13 PM
#1
Thread Starter
Addicted Member
Help with structures, another project
Project:
Consisting of creating a program that will read data from a file, place the data in an array of structures, sort the data, and write it to another file.
Specifics:
It should ask the user for the input file name and output file name. Input will consist of pairs of names and ssn #'s. The information should be read into an array of structures. Should be sorted in alphabetic order and sorted pairs should be written to output file.
Sample Input:
Johnson, Jay
123-45-6789
Kingston, Ken
543-43-2369
Sample Output:
Name: Bell, Bob
SSN: 354-23-5467
Name: Joe smoe
SSN: 344-55-3345
Requirements:
- Assume that there are no more then 100 name/number pairs in the input file
- Ensure that the input file exists and continuously re-prompt the user for an inpout file name until a valid one is entered
- Seperate functions should be used to read in the information, sort it, and write it out
PHP Code:
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
struct record
{
char name[50]
char ssn[12];
};
int main(int argc, char* argv[])
{
char filename1[40],filename2[40];
while(true)
{
cout << "Please provide the input file name: ";
cin.getline(filename1,40);
ifstream temp;
temp.open(filename1);
if(temp)
{
temp.close();
break;
}
cout << "Invalid file name!" << endl;
}
cout << "Please provide the output file name: ";
cin.getline(filename2,40);
copy_file(filename1,filename2);
return 0;
}
[COLOR=limegreen]// Not Sure What Goes In These Functions Exactly[/COLOR]
void read_in(record items[], int & count, char src[]);
{
}
void write_out(record items[], int count, char dst[])
{
}
[COLOR=limegreen]// Not Sure Where This Sort Function Goes[/COLOR]
void sort (record items[], int count)
{
int min;
for (int =0; i<(count -1); i++)
min =i;
for(int j=1+1; j<count; j++)
if(strcmp(items[j].name, items[min].name) <0)
min=j;
record temp;
temp = items[i];
items[i] = items[min];
items[min] = temp;
}
return;
}
Thats all my code so far. Am I on the right track? Im not sure what to or what input to put in those blank functions.
Cornedbee, and ModMad I need ur help again! LOL
-
Oct 6th, 2002, 04:37 AM
#2
First hint:
Code:
while(true)
{
cout << "Please provide the input file name: ";
cin.getline(filename1,40);
ifstream temp;
temp.open(filename1);
if(temp.good()) // this is the correct way to check
{
temp.close();
break;
}
cout << "Invalid file name!" << endl;
}
Second hint: Since filenames can include directories they can be very long. Make the filename arrays at least 256 characters large.
Third hint: Remove the write_out and read_in functions for now. Concentrate on writing the copy_file function.
The sort should be ok.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 6th, 2002, 09:38 AM
#3
Thread Starter
Addicted Member
WEll the file copy function is gonna be this:
PHP Code:
void copy_file(char src[], char dst[])
{
char buffer[80];
ifstream infile(src);
ofstream outfile(dst);
while(!infile.eof())
{
infile.getline(buffer,80);
outfile << buffer << endl;
}
infile.close();
outfile.close();
return;
}
Im not worried about that, Our teacher helped us with that one, but I just never included it in my post
-
Oct 6th, 2002, 09:46 AM
#4
Monday Morning Lunatic
Originally posted by CornedBee
Second hint: Since filenames can include directories they can be very long. Make the filename arrays at least 256 characters large.
No, make them strings. Paths may be longer than 256 characters on some systems...
...use std::getline(std::istream&, std::string&) to get the entire path.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Oct 6th, 2002, 01:19 PM
#5
Yes, even better. Except for the bug in getline when used with cin.
The copy_file function is useless. It is a frame that you can use, but actually you don't want to simply copy the files, you want to read in one file completly, sort it and write it out to the other file.
Since you know that there won't be more than 100 entries it's very simple, you don't even need a dynamic array.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 6th, 2002, 03:36 PM
#6
Thread Starter
Addicted Member
SO how would I get started on the function for reading in the information and writing it out? Or at least read it in, and then from there I could figure out how to code the writeout function
and then tell me if the rest of my code above is fine, and where to put my sort fujnction
Last edited by chugger93; Oct 6th, 2002 at 05:50 PM.
-
Oct 6th, 2002, 04:29 PM
#7
Monday Morning Lunatic
Originally posted by CornedBee
Yes, even better. Except for the bug in getline when used with cin.
The copy_file function is useless. It is a frame that you can use, but actually you don't want to simply copy the files, you want to read in one file completly, sort it and write it out to the other file.
Since you know that there won't be more than 100 entries it's very simple, you don't even need a dynamic array.
Bug? Which one?
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Oct 7th, 2002, 04:00 AM
#8
That it will somehow keep one line cached and return the previous. Which means you need to hit enter twice and then the next input gives you an empty line. Many questions about that already.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 7th, 2002, 06:13 AM
#9
Monday Morning Lunatic
Code:
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
while(1) {
getline(cin, s);
if(cin.eof() || cin.fail())
return 0;
cout << "Line: " << s << endl;
}
}
...produces:
Code:
[mike@relativity junk]$ ./cintest
Hello
Line: Hello
this is my line
Line: this is my line
Line:
test?
Line: test?
next line is Ctrl-D for an EOF...
Line: next line is Ctrl-D for an EOF...
[mike@relativity junk]$
All seems to work
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Oct 7th, 2002, 06:37 AM
#10
Thread Starter
Addicted Member
-
Oct 7th, 2002, 06:42 AM
#11
Strange, whenever I tried it it produced something like
Code:
Hello
Line: Hello
There
Line:
Damnit!
Line: Hello
etc...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 7th, 2002, 06:46 AM
#12
Well, it works now, so this is ok.
Maybe a bug in the VC++6 implementation of the C++SL.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 7th, 2002, 08:47 AM
#13
Thread Starter
Addicted Member
So is this something I would want for reading in my information function?
PHP Code:
void read_in(record items[], int &count, char src[])
{
char buffer[100];
while(!temp.eof())
temp.getline(buffer,100)
}
Or am i still way off? and would I still need the file copy function even if I have the write out funciton? Isnt that the same?
-
Oct 7th, 2002, 09:00 AM
#14
No, you would read it in a string, not a buffer. And it is meant for the file names.
#include <string>
#include <iostream>
using namespace std;
string sFileName;
getline(cin, sFileName);
Don't be too fixed on the copy_file and write_out and read_in functions. Basically you read need a struct that holds one block of info. Then you declare a static array of 100 such blocks. Then you read in the file into the array while counting how many blocks you actually read. Then you sort the array. Then you write the array out to the other file.
This means you need a main function which does (in pseudo-code)
Code:
string inFile = ReadAndValidateInFileName();
string outFile = ReadOutFileName();
datablockstruct blocks[100];
int numBlocksRead = ReadFile(inFile, blocks);
SortArray(blocks, numBlocksRead);
WriteFile(outFile, numBlocksRead);
You already have ReadAndValidateInFileName and ReadOutFileName. You also have SortArray. You only need to write ReadFile, datablockstruct and WriteFile.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 7th, 2002, 09:24 AM
#15
Thread Starter
Addicted Member
Ok so will the above code work if i changed the (buffer,100) to
(cin, sFileName)
or do I need more code
sorry, its just that ur code is confusing me cuz im using differnt names and variables
I have all this so far:
PHP Code:
using namespace std;
struct record
{
char name[50];
char ssn[12];
}
string sFileName;
int main(int argc, char* argv[])
{
record items[100];
{
char filename1[40],filename2[40];
while(true)
{
cout << "Please provide the input file name: ";
cin.getline(filename1,40);
ifstream temp;
temp.open(filename1);
if(temp)
{
temp.close();
break;
}
cout << "Invalid file name!" << endl;
}
cout << "Please provide the output file name: ";
cin.getline(filename2,40);
write_out(filename1,filename2);
return 0;
}
void read_in(record items[], int &count, char src[])
{
while(!temp.eof())
temp.getline(cin, sFileName);
}
void write_out(record items[], int count, char dst[])
{
}
void sort (record items[], int count)
{
int min;
for (int =0; i<(count -1); i++)
min =i;
for(int j=1+1; j<count; j++)
if(strcmp(items[j].name, items[min].name) <0)
min=j;
record temp;
temp = items[i];
items[i] = items[min];
items[min] = temp;
}
return;
}
-
Oct 7th, 2002, 10:04 AM
#16
PHP Code:
#include <iostream>
#include <string>
using namespace std;
struct record
{
string name;
string ssn;
}; // you need a ; here
int main(int argc, char* argv[])
{
record items[100];
string filename1,filename2;
while(true)
{
cout << "Please provide the input file name: " << endl;
getline(cin, filename1);
ifstream temp;
temp.open(filename1);
if(temp.good())
{
temp.close();
break;
}
cout << "Invalid file name!" << endl;
}
cout << "Please provide the output file name: ";
getline(cin, filename2);
int num;
read_in(items, num, filename1);
sort(items, num);
write_out(items, num, filename2);
return 0;
}
void read_in(record items[], int &count, const string &inFileName)
{
// temp doesn't exist here (ever heard of scoping?)
// inFileName is the name of the file to read from
// open it and read. Store the number of
// elements read in count. Read into items
// Use getline to read directly into the fields
// of items.
}
void write_out(record items[], int count, const string & dst)
{
// dst is the filename of the output file
// open it and write all entries in items
// (there are count entries) to it. Use the
// overloaded << operator and append
// a '\n' to each line.
}
void sort (record items[], int count)
{
int min;
for (int i=0; i<(count -1); i++)
min = i;
for(int j=1+1; j<count; j++)
if(items[j].name < items[min].name)
min=j;
record temp;
temp = items[i];
items[i] = items[min];
items[min] = temp;
}
// This is a useless line. You only need a
// return if you want to exit the function
// early or you need to return a value.
//return;
}
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 7th, 2002, 10:33 AM
#17
Thread Starter
Addicted Member
so something like this:?
PHP Code:
void read_in(record items[], int &count, const string &inFileName)
{
inFileName.open(filename1);
while(!inFileName.eof())
inFileName.getline(items[count], num[count]);
}
err this:?
PHP Code:
void read_in(record items[], int &count, const string &inFileName)
{
ifstream filename1(inFileName);
filename1.open;
while(!filename1.eof())
filename1.getline(items[count]);
}
Last edited by chugger93; Oct 7th, 2002 at 11:58 AM.
-
Oct 7th, 2002, 06:30 PM
#18
Thread Starter
Addicted Member
-
Oct 8th, 2002, 06:26 AM
#19
No. The second try is quite good but still has several problems.
The read_in function doesn't use the count parameter, it assigns something to it. The number of elements read.
Basically you have a loop that loops until the end of file is reached (like you have now). You also have a variable that starts at 0 and is incremented every loop. It counts the number of elements you read and tells you where to insert the new item in the item array.
Again you need to use the global getline(istream&, string&). Look at the code that reads the filenames.
And you can't pass a structure to getline. You must pass string objects.
If you pass a filename to the ifstream constructor it automatically opens the file, you don't need to call open (there is no open that takes no parameters, and especially none where you can omit the brackets).
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 8th, 2002, 09:39 AM
#20
Thread Starter
Addicted Member
Originally posted by CornedBee
Again you need to use the global getline(istream&, string&). Look at the code that reads the filenames.
And you can't pass a structure to getline. You must pass string objects.
If you pass a filename to the ifstream constructor it automatically opens the file, you don't need to call open (there is no open that takes no parameters, and especially none where you can omit the brackets).
ok right, so if i cant pass a filename to the ifstream, is it something like this?
PHP Code:
void read_in(record items[], int &count, const string &inFileName)
{
ifstream infile(inFileName);
infile.open;
while(!infile.eof())
infile.getline(items[count]);
}
and yah I know u cant use the getline that way, I keep getting errors around that statement, and I didnt know that. Can u gimmie an example of what u mean passing getline as string objects? is that just like getline(cin, whatever) ?
Also Cornedbee, our teacher gave us an example of how our function is gonna be layed out, errr I mean the top line
he said its gonna be
void read_in(record items[], int &count, char src[])
so it looks like everything is gonna be a char not a string. Mabe we can work with that instead? In anycase, I got 3 hours to finish this project, else im screwed
Last edited by chugger93; Oct 8th, 2002 at 10:10 AM.
-
Oct 8th, 2002, 10:36 AM
#21
Since you got only 3 hours left and you teacher is ignorant enough to force you to use char instead of string I will solve this for you.
Programming teachers need to be punished!
Code:
#include <iostream>
using namespace std;
struct record
{
char name[80];
char ssn[40];
};
void read_in(record [], int &, char []);
void sort(record [], int);
void write_out(record [], int, char []);
int main(int argc, char* argv[])
{
record items[100];
char filename1[300],filename2[300];
while(true)
{
cout << "Please provide the input file name: " << endl;
cin.getline(filename1, 300);
ifstream temp;
temp.open(filename1);
if(temp.good())
{
temp.close();
break;
}
cout << "Invalid file name!" << endl;
}
cout << "Please provide the output file name: " << endl;
cin.getline(filename2, 300);
int num;
read_in(items, num, filename1);
sort(items, num);
write_out(items, num, filename2);
return 0;
}
void read_in(record items[], int &count, char inFileName[])
{
ifstream is(inFileName);
int i;
for(i=0; !is.eof(); ++i) {
is.getline(items[i].name, 80);
is.getline(items[i].ssn, 40);
}
count = i+1; // not sure about that
// you need to test if it's too much
// to test:
cout << count << endl;
// if this is too much then count should
// be only i
}
void write_out(record items[], int count, char dst[])
{
ofstream os(dst);
for(int i=0; i < count; ++i) {
os << items[i].name << '\n' <<
items[i].ssn << '\n';
}
}
void sort (record items[], int count)
{
int min;
for (int i=0; i<(count -1); i++)
min = i;
for(int j=1+1; j<count; j++)
if(strcmp(items[j].name, items[min].name) < 0)
min=j;
record temp;
temp = items[i];
items[i] = items[min];
items[min] = temp;
}
}
This code is not tested. It may or may not compile. It may or may not work correctly. Your teacher is an idiot.
May the force be with you when facing this danger.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 8th, 2002, 10:36 AM
#22
Oh, btw it's called a function prototype.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 8th, 2002, 11:01 AM
#23
Thread Starter
Addicted Member
welp, corned it works dude, but the output gets a little wacky
when it writes it to a data file it puts it as
Johnson, Jay
123-45-6789
ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ ÌÌÌÌÌÌÌÌÌÌÌÌTomas, Roy
ÌÌÌÌÌÌÌÌÌÌÌÌTomas, Roy
Tomas, Roy
123-46-6652
thats a flaw in the writeout section prob eh? I'll take a look at it.
NO but ur absoultely right. He is an idiot. but u know what? hes a graduate student still in school, and hes teaching! I dunno why though!! So that has to have something to do with it.
Is it better to use strings? Just curiouis as to why...
thanx again dude
-
Oct 8th, 2002, 11:06 AM
#24
Thread Starter
Addicted Member
oops. i solved it, it was cuz of the extra i
Actually u know what cornedbee?
its supposed to say in the output
Name: blah blah
SSN: blah blah
So thats probaly gonna be added in the write_out i would presume
Also, its not sorting it alphabetically, hmmm
Last edited by chugger93; Oct 8th, 2002 at 11:25 AM.
-
Oct 8th, 2002, 11:41 AM
#25
Thread Starter
Addicted Member
Ok well, i figured out the Name: and SSN: part
just gotta get the sorting right lol
-
Oct 8th, 2002, 11:42 AM
#26
Yes, in the write_out function.
Don't know why it doesn sort.
string is better because it is easier to use, less likely to cause problems with overflow or memory leaks, easier to read and a few things I can't think of right now.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 8th, 2002, 12:27 PM
#27
Thread Starter
Addicted Member
i c
Well, im just gonna turn it in as is. Mabe its his code in the sort func that doesnt work, cuz Ive looked over it and I dunno.
Thanx cornedbee
-
Oct 8th, 2002, 04:24 PM
#28
Tell me how it turned out...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|