Forums | MacLife
You are not logged in.
#1 2002-12-09 9:56 am
- Superpual
- Member
- From: CheeseLand Wisconsin
- Registered: 2001-12-03
- Posts: 213
Comp Sci teacher doesn't know this...
Could someone tell me how to do something in C++.
I need to write a function that determines what the longest word in a file is. The function should display the longest word and its length on the terminal screen.
I asked my Computer Science teacher and he said "Find out on the internet".
Offline
#2 2002-12-09 10:26 am
- Fracai
- Evacipate

- From: St. Elsewhere
- Registered: 2000-05-25
- Posts: 2835
Re: Comp Sci teacher doesn't know this...
well, you'll want to:
1. open the file
2. read in the next word
3. use strlen() to get the length of the string
4. if the length is the longest, store the word
5. repeat from 2 until you get to the end of the file
6. close the file
7. display the word and length
I'm assuming you know how to do each step, and it's the steps you needed. If you need more than this I can help more. Let me know.
Offline
#3 2002-12-09 12:25 pm
Re: Comp Sci teacher doesn't know this...
assuming ANSI C
this is a summary, probably won't compile as is:
#include <fstream>
#include <string>
#include <iostream>
ifstream x;
string y="*"; //string or character array
int z;
string longestword;
x.open("file")
do
{
z=strlen(y);
x>>y;
if(strlen(y)>z)
{z=strlen(y);longestword=y;}//yeah, so that's the only descriptive variable I used
}
while(x);
Offline
#4 2002-12-10 8:49 am
- Superpual
- Member
- From: CheeseLand Wisconsin
- Registered: 2001-12-03
- Posts: 213
Re: Comp Sci teacher doesn't know this...
Thanks for your help, worked very well.
Offline
#5 2002-12-31 3:32 pm
- dstaudigel
- Member
- From: San Diego CA USA Posts: 5105
- Registered: 2000-10-04
- Posts: 255
Re: Comp Sci teacher doesn't know this...
Personally, I would do it in C (why am I so old-school at 15?), but, other than that, I would use a while loop, not a do-while loop, that way, if the file is bad, it skips over it altogether.
Daniel Staudigel
Offline
#6 2003-01-02 12:10 am
- Tetrachloride
- ❖ ❖ ❖

- Registered: 2001-01-29
- Posts: 7150
Re: Comp Sci teacher doesn't know this...
I asked my Computer Science teacher and he said "Find out on the internet".
Probably, your comp sci teacher thought the question was too easy.
--------
Time for a story, Almost 2 and one half decades ago, I heard this -- The professor gave his students the assignment: "write a program to enter 2 numbers, add them together and print it out." It seems that it took the whole class working together to figure it out.
Offline
#7 2003-01-02 12:40 am
- dstaudigel
- Member
- From: San Diego CA USA Posts: 5105
- Registered: 2000-10-04
- Posts: 255
Re: Comp Sci teacher doesn't know this...
I asked my Computer Science teacher and he said "Find out on the internet".
Probably, your comp sci teacher thought the question was too easy.
--------
Time for a story, Almost 2 and one half decades ago, I heard this -- The professor gave his students the assignment: "write a program to enter 2 numbers, add them together and print it out." It seems that it took the whole class working together to figure it out.
Still, what a lame-ass comp-sci teacher.
Offline
#8 2003-01-02 2:32 am
- Tetrachloride
- ❖ ❖ ❖

- Registered: 2001-01-29
- Posts: 7150
Re: Comp Sci teacher doesn't know this...
dstaudigel wrote:
I asked my Computer Science teacher and he said "Find out on the internet".
Probably, your comp sci teacher thought the question was too easy.
Still, what a lame-ass comp-sci teacher.Very possibly. A more direct hint would have been to say, "look in the standard functions library".
Some judgment depends on whether this is a university level entrance course where half of the goal is to make as many students as possible drop the course, or a friendly high school, tech school, or local college course. 1-2 of my university instructors have been rather callous, out of the bunch I had. Some judgment depends on whether the instructor thought th e student was smart but lazy, or somewhat smart, etc. I would say only 1 of my instructors really had a talent for getting the numerous messages across, 2-4 others were pretty okay, 1-4 deserved to a demotion, or even to be fired.
Offline
#9 2003-01-02 10:14 am
- dstaudigel
- Member
- From: San Diego CA USA Posts: 5105
- Registered: 2000-10-04
- Posts: 255
Re: Comp Sci teacher doesn't know this...
Tetrachloride wrote:
I asked my Computer Science teacher and he said "Find out on the internet".
Probably, your comp sci teacher thought the question was too easy.
Still, what a lame-ass comp-sci teacher.Very possibly. A more direct hint would have been to say, "look in the standard functions library".
Some judgment depends on whether this is a university level entrance course where half of the goal is to make as many students as possible drop the course, or a friendly high school, tech school, or local college course. 1-2 of my university instructors have been rather callous, out of the bunch I had. Some judgment depends on whether the instructor thought th e student was smart but lazy, or somewhat smart, etc. I would say only 1 of my instructors really had a talent for getting the numerous messages across, 2-4 others were pretty okay, 1-4 deserved to a demotion, or even to be fired.I've never had any comp-sci teachers, the closest I ever had was relatively enigmatic when he was busy with beginners, but when he had time, he'd sit down and just get right to work with me, it was great.
Offline

