homework help!!!

K

kristiek

ive been trying to write a program for 2 weeks now, and im still lost!
it's due tomorrow!!!! ANY help from anyone, would be soooooo
appreciated...i will owe you my life!!! here's the deal: I have to
write a program that encodes the text in a data file using a character
array named key that contains 26 characters. This key is read from the
keyboard; the first letter contains the character that is to replace
the letter a in the data file, the second letter contains the letter
that is to replace the letter b in the data file, and so on. Assume
that all punctuation is to be replaced by spaces. Check to be sure
that the key does not map two different characters to the same one
during the encoding.
Requirement:
1.Read data from keyboard to key array. And give the user the hint
that EACH
ELEMENT IN THE key array MUST BE DIFFERENT FROM EACH OTHER.
2.If the character in the data file is between ‘a' and ‘z', replace it
according
to key array. If the character in the data file is a punctuation,
replace it by
space. Otherwise, (such as capital letters) the character in the data
file stays same.

So that's the assignment...and even tho i am sure all you master
programmers out there are going to laugh at what i have, i am going to
post my program thus far...please help!!!!!


#include <iostream>
#include <fstream>
#include <string>
using namespace std;

ifstream file1 ("infile.txt");
ofstream file2;

int main ()
{

char * key;
key = new char[26]; //creating an array, known as "key"


for (int i=0; i<26; i++)

cout << "Enter the code you wish to use for the key. The first
character
you type will replace the letter 'a', the second will replace 'b', and
so on. Be
careful not to repeat the same character twice." << endl;

cin >> key ;

char * text;
text = new char [100]; //creates an array to hold the text from the
input file
int i=0; // creates an index counter so the array can be read into


file1.open (filename.c_str()); //reading in from the file

if (file1.fail()) //checking to make sure the file opens correctly
{
cout << "Error opening input file. Please check and try again."
<< endl;
}
else
{
while (! file1.eof())
{
text=file1.get(c);
}
file1.close();
}
return0;
}
 
O

osmium

kristiek said:
ive been trying to write a program for 2 weeks now, and im still lost!
it's due tomorrow!!!! ANY help from anyone, would be soooooo
appreciated...i will owe you my life!!! here's the deal: I have to
write a program that encodes the text in a data file using a character
array named key that contains 26 characters. This key is read from the
keyboard; the first letter contains the character that is to replace
the letter a in the data file, the second letter contains the letter
that is to replace the letter b in the data file, and so on. Assume
that all punctuation is to be replaced by spaces. Check to be sure
that the key does not map two different characters to the same one
during the encoding.
Requirement:
1.Read data from keyboard to key array. And give the user the hint
that EACH
ELEMENT IN THE key array MUST BE DIFFERENT FROM EACH OTHER.
2.If the character in the data file is between 'a' and 'z', replace it
according
to key array. If the character in the data file is a punctuation,
replace it by
space. Otherwise, (such as capital letters) the character in the data
file stays same.

So that's the assignment...and even tho i am sure all you master
programmers out there are going to laugh at what i have, i am going to
post my program thus far...please help!!!!!


#include <iostream>
#include <fstream>
#include <string>
using namespace std;

ifstream file1 ("infile.txt");
ofstream file2;

int main ()
{

char * key;
key = new char[26]; file://creating an array, known as "key"

There is nothing wrong with:
char key[26];

It is simpler and just as effective.
for (int i=0; i<26; i++)

cout << "Enter the code you wish to use for the key. The first
character
you type will replace the letter 'a', the second will replace 'b', and
so on. Be
careful not to repeat the same character twice." << endl;

That message should be moved outside the for loop.
cin >> key ;

char * text;
text = new char [100]; file://creates an array to hold the text from the


That's your biggest problem. 100 is much too small for a file, and any
number, it can be argued, is too small. The assignment wants you to
replace, in situ, each character that need to be replaced. You want
fstream, early on, where you have ofstream. Get rid of the notion of a
buffer. Get a char, modify it if required and write it back. In any event
there is a one to one correspondence between input and output.
input file
int i=0; // creates an index counter so the array can be read into


file1.open (filename.c_str()); file://reading in from the file

if (file1.fail()) file://checking to make sure the file opens correctly
{
cout << "Error opening input file. Please check and try again."
<< endl;
}
else
{
while (! file1.eof())
{
text=file1.get(c);
}
file1.close();
}
return0;
}


Posting a problem such as this can result in many wildly diverging
solutions and advice and can confuse you even further. There are people who
think any solution that doesn't use the STL library extensively is flawed
and childish. From the assignment, I don't think your instructor is one of
those people. What I am saying is, look at STL centric solutions with a
jaundiced eye; with two days to go you don't have time to learn the STL now.
Note that there are several helpful functions for this assignment in
<cctype>. You may not have time to do the check for bad keys
(duplicate/missing letters). That can be added after the main program is
working.
 
K

Karl Heinz Buchegger

kristiek said:
So that's the assignment...and even tho i am sure all you master
programmers out there are going to laugh at what i have, i am going to
post my program thus far...please help!!!!!

I won't do your assignment, just some simple annotations.

First and most important: Indent your code !!!
This is so important that I want to repeat it: Indent your code!
At the moment your code is a mess of statements all starting at
the left border. No wonder that you could not come up with more
in 2 weeks.

Second: Don't do the whole program in one big rush. Write a little
bit, then test it.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

ifstream file1 ("infile.txt");
ofstream file2;

int main ()
{

char * key;
key = new char[26]; //creating an array, known as "key"

for (int i=0; i<26; i++)

cout << "Enter the code you wish to use for the key. The first
character
you type will replace the letter 'a', the second will replace 'b', and
so on. Be
careful not to repeat the same character twice." << endl;

cin >> key ;


Have you ever run your program? At the moment your program will
print 26 times the text "Enter the code ...." followed by
1 user input to read one single character.

Test what you have!!!! Start testing as soon as there is something
that can be tested. Even if that something is just: output some text
and input some characters. A good thing to verify that this indeed
works as you expect it to work is to output what was read from
the user:

cout << "Enter the code ...."

for( int i = 0; i < 26; i++ )
cin >> key;

cout << "You entered:\n"
for( int i = 0; i < 26; i++ )
cout << key;
cout << "\n";
char * text;
text = new char [100]; //creates an array to hold the text from the

Why are you dynamically allocating all those character arrays (same for
key), there is no need to:

char text[100];

does the same thing. The only difference is that you don't have to manage
the lifetime of the array on your own.
input file
int i=0; // creates an index counter so the array can be read into

file1.open (filename.c_str()); //reading in from the file

if (file1.fail()) //checking to make sure the file opens correctly
{
cout << "Error opening input file. Please check and try again."
<< endl;
}
else
{
while (! file1.eof())
{
text=file1.get(c);
}


That's a big no no. In C++ the strategy to read from a file is different.
One does not read until eof occours. One reads as long as the read was
successfull. Only after that eof is checked to verify that all of the
file was read:

char NextChar;

while( file1.get( NextChar ) )
{
// since we don't know better for now, we simply
// output the read character

cout << NextChar;
}

if( !file1.eof() )
cout << "Something serious happend during reading the file\n";

file1.close();
}
return0;
}

All that is left is to manipulate the character read from the file
according to the rules specified in the assignment.
Again: Don't do anything in one big rush. Work in steps: Choose one
of the replacement rules and implement it. Then test if your implementation
really works the way it should. Start with simple things. It is easy to
identify if a character is a lower case character or an upper case character.
functions islower() and isupper() are there for doing just that. So your
first attempt could eg. be:

while( file1.get( NextChar ) )
{
// do something with lower case characters
// at the moment I simply output them
if( islower( NextChar ) )
cout << NextChar;

// do something with upper case characters
// -> output them
else if( isupper( NextChar ) )
cout << NextChar;

// else must be punctuation or anything else
// replace with space
else
cout << ' ';
}

and test that again. All the character should stay the same. Only the
punctuation and/or some digits should be replaced with ' ' characters.
 
J

jonathanmcdougall

ive been trying to write a program for 2 weeks now, and im still lost!
it's due tomorrow

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

Don't do that. At most, be specific :

using std::ifstream;
using std::eek:stream;
using std::cout;
using std::cin;

and put these at the most restrictive scope (in this case, in main())
ifstream file1 ("infile.txt");
ofstream file2;

Why globals?
int main ()
{
Nice!

char * key;
key = new char[26]; //creating an array, known as "key"

Don't forget to delete it! By the way, why not use std::string?
for (int i=0; i<26; i++)

Hmm.. braces?

{
cout << "Enter the code you wish to use for the key. The first
character
you type will replace the letter 'a', the second will replace 'b', and
so on. Be
careful not to repeat the same character twice." << endl;

cin >> key ;


What if the user enters something like 'dsf9809r3' ? And you forgot the
closing brace. You should move the message ouside the loop.

}
char * text;
text = new char [100]; //creates an array to hold the text from the

You must delete that one too. Again, what about std::string's?
int i=0; // creates an index counter so the array can be read into
file1.open (filename.c_str()); //reading in from the file

1) Why have you specified a file when defining file1 if you open
a new one here?
2) What is 'filename' ?
if (file1.fail()) //checking to make sure the file opens correctly

if ( ! file1 )

could do also.
{
cout << "Error opening input file. Please check and try again."
<< endl;
}
else
{
while (! file1.eof())

Never check for eof() _before_ reading. Always afer.
{
text=file1.get(c);
}
file1.close();
}
return0;


Does that compile?


No quite sure about what this program does so far. Here are some hints :

1) Ask the user a string of 26 characters for the key
2) Ask for filenames (perhaps)
3) Open the datafile
4) Read it all in a string
5) Encode that string
6) Write it back (perhaps)
7) Delete everything created with new and delete[] everything created by
new[].

8) Use std::string's
9) Use some intelligent variable names
10) Ident your code properly
11) Check input for errors
12) Post the real code next time

Have fun!

Jonathan
 
P

Pete

osmium wrote:
Posting a problem such as this can result in many wildly diverging
solutions and advice and can confuse you even further. There are
people who think any solution that doesn't use the STL library
extensively is flawed and childish.

Why is using the standard library bad?
It makes no sense to me to ignore the vast amounts of easy-to-use library
classes and functions.

- Pete

<snip>
 
B

Bill Seurer

Pete said:
osmium wrote:


Why is using the standard library bad?
It makes no sense to me to ignore the vast amounts of easy-to-use library
classes and functions.

CS students should know how to do the lower level stuff too especially
since that seems to have been the intent of the original assignment.
 
O

osmium

Pete said:
osmium wrote:


Why is using the standard library bad?
It makes no sense to me to ignore the vast amounts of easy-to-use library
classes and functions.

I didn't say or imply that it was bad, in general. My post was addressed to
one person taking one course and trying to solve one problem. College
courses are often organized around the sequence of a text book. Many books,
and therefore the courses, are not sequenced for an STL centric solution.
Most babies are born with no knowledge of the STL and it has to be learned.

For example, the OP was comfortable with an array of characters for the key.
Someone might propose a vector instead. Why complicate life for the OP?
 
P

Pete

osmium said:
I didn't say or imply that it was bad, in general. My post was
addressed to one person taking one course and trying to solve one
problem. College courses are often organized around the sequence of
a text book. Many books, and therefore the courses, are not
sequenced for an STL centric solution. Most babies are born with no
knowledge of the STL and it has to be learned.

Ok, makes sense. Though IMO the courses should focus on high level contructs
(STL stuff) then later explain how those constructs are implemented.
For example, the OP was comfortable with an array of characters for
the key. Someone might propose a vector instead. Why complicate life
for the OP?

Errr, it didn't look like he was comfortable with dynamic arrays to me...

- Pete
 
P

Pete

Bill said:
CS students should know how to do the lower level stuff too especially
since that seems to have been the intent of the original assignment.

IMO, they should learn about low-level stuff /after/ they have learned to
use higher level constructs, not the other way around.

- Pete
 
A

Andre Kostur

Ok, makes sense. Though IMO the courses should focus on high level
contructs (STL stuff) then later explain how those constructs are
implemented.

I'm rather of the opinion of the other direction. Learn the hard way
first. Then learn the easy way. I think you would risk too much of the
mindset of the student knowing the easy way, and brushing off the hard way
because they already know the easy way.
 
J

James Goodzeit

kristiek said:
ive been trying to write a program for 2 weeks now, and im still lost!
it's due tomorrow!!!! ANY help from anyone, would be soooooo
appreciated...i will owe you my life!!! here's the deal: I have to
write a program that encodes the text in a data file using a character
array named key that contains 26 characters. This key is read from the
keyboard; the first letter contains the character that is to replace
the letter a in the data file, the second letter contains the letter
that is to replace the letter b in the data file, and so on. Assume
that all punctuation is to be replaced by spaces. Check to be sure
that the key does not map two different characters to the same one
during the encoding.
Requirement:
1.Read data from keyboard to key array. And give the user the hint
that EACH
ELEMENT IN THE key array MUST BE DIFFERENT FROM EACH OTHER.
2.If the character in the data file is between 'a' and 'z', replace it
according
to key array. If the character in the data file is a punctuation,
replace it by
space. Otherwise, (such as capital letters) the character in the data
file stays same.

So that's the assignment...and even tho i am sure all you master
programmers out there are going to laugh at what i have, i am going to
post my program thus far...please help!!!!!


#include <iostream>
#include <fstream>
#include <string>
using namespace std;

ifstream file1 ("infile.txt");
ofstream file2;

int main ()
{

char * key;
key = new char[26]; //creating an array, known as "key"


for (int i=0; i<26; i++)

cout << "Enter the code you wish to use for the key. The first
character
you type will replace the letter 'a', the second will replace 'b', and
so on. Be
careful not to repeat the same character twice." << endl;

cin >> key ;

char * text;
text = new char [100]; //creates an array to hold the text from the
input file
int i=0; // creates an index counter so the array can be read into


file1.open (filename.c_str()); //reading in from the file

if (file1.fail()) //checking to make sure the file opens correctly
{
cout << "Error opening input file. Please check and try again."
<< endl;
}
else
{
while (! file1.eof())
{
text=file1.get(c);
}
file1.close();
}
return0;
}


I suggest you read
http://www.cse.buffalo.edu/academics-academic_integrity.shtml very carefully
before asking for help with your homework on Usenet. You might have bigger
problems than not getting your assignment in on time. HTH
 
B

Bill Seurer

Pete said:
Ok, makes sense. Though IMO the courses should focus on high level contructs
(STL stuff) then later explain how those constructs are implemented.

Note that this is the opposite of how almost everything else is taught.
For example, in mathematics you start with numbers and addition not
differential equations and calculus.
 
K

Kevin Goodsell

Andre said:
I'm rather of the opinion of the other direction. Learn the hard way
first. Then learn the easy way. I think you would risk too much of the
mindset of the student knowing the easy way, and brushing off the hard way
because they already know the easy way.

Same argument that is always used in this debate. I don't buy it. Any
person who has this mindset is doomed as a programmer anyway, regardless
of the order that they learn things.

-Kevin
 
J

John Harrison

Have you ever run your program?

I doubt they've ever compiled it.

Kristiek, if you are listening it's too late, you should have asked for help
earlier. Go to your teacher and ask for an extension, then you will be able
to throw away what you have already written and follow some of the good
advice being offered.

And the most important advice, the thing you should have being doing right
from the start is what Karl has already told you
Second: Don't do the whole program in one big rush. Write a little
bit, then test it.

Nothing, absolutely nothing, is more valuable than that piece of advice.
Start again, following that advice.

john
 
K

Kevin Goodsell

Bill said:
Note that this is the opposite of how almost everything else is taught.
For example, in mathematics you start with numbers and addition not
differential equations and calculus.

How is that the opposite? In both cases you learn the easier topics
first. In fact, this is how most topics are taught, because it makes sense.

-Kevin
 
B

Bill Seurer

John said:
And the most important advice, the thing you should have being doing right
from the start is what Karl has already told you


Nothing, absolutely nothing, is more valuable than that piece of advice.
Start again, following that advice.

Yup. When I am learning something new I always write little bits that
work with extra debugging stuff thrown in and then keep adding on. So
if it is something that is going to read a file, do something to the
contents, and then write something out I start with the shell of the
program, then have it open the files and report the results of that,
then read the complete files and show me the contents, and etc. one step
at a time.
 
D

David Harmon

Ok, makes sense. Though IMO the courses should focus on high level contructs
(STL stuff) then later explain how those constructs are implemented.

Note that this is the opposite of how almost everything else is taught.
For example, in mathematics you start with numbers and addition not
differential equations and calculus.[/QUOTE]

In order to define numbers, you must understand group theory. But in
teaching mathematics you do not start with group theory. You start with
the user level of numbers and addition and subtraction, and that will
carry you a long way before you need to dig down to the infrastructure.

Similarly, in C++ you should be using std::string extensively before you
ever do a char * = new char[].
 
B

Buster

David Harmon wrote:

[...]
In order to define numbers, you must understand group theory.
[...]

You mean set theory, but it's not true. There are many logical systems
in which it is possible to express the concept of integer. The point
still stands though, since we don't teach logic much at school.
 
D

Dietmar Kuehl

osmium said:
The assignment wants you to
replace, in situ, each character that need to be replaced. You want
fstream, early on, where you have ofstream. Get rid of the notion of a
buffer. Get a char, modify it if required and write it back. In any event
there is a one to one correspondence between input and output.

I disagree with this statement. In fact, I would claim that you are not
aware that the following produces undefined behavior:

std::fstream file("file"); // assume that beast exists
char c = 0;
file.get(c); // assume that this is also successful
file.put(c); // this causes undefined behavior!

To avoid this problem, you have to inject a seek between each switch between
reading and writing, eg.:

file.seekg(0, std::ios_base::cur);

Doing something like this will, however, slow down your program tremendously.
This probably does not matter for a homework assignment like this, in real
code it matters.
 
D

Dietmar Kuehl

Bill Seurer said:
Yup. When I am learning something new I always write little bits that
work with extra debugging stuff thrown in and then keep adding on.

Actually, this is how I work even when I'm working on something I'm
comfortable with! Once I'm content that everything works, I remove the
debug output. The funny thing is that I find loads of ridiculous and
simple errors early on.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,141
Latest member
BlissKeto
Top