473,586 Members | 2,695 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Homework Help

7 New Member
Hi, I'm a new user of this webside and also a begginer of C++. Hopefully can get somehelp from you guys. :)

Now I meet some problems, anybody can help me with the coding?

Requirement: Write a simple C++ program using cin and cout. The program will read from cin, cout the number of words read, until the end of file(eof). Then display the last word together with the number of words read.

Sample Input:

I am a fresh man here
Please kindly help me 1,2,3

Sample Output:

last word: 1,2,3
Word count: 11

Friends, hopefully can get your reply before 31, Sep... :D

Thanks a lot.
Aug 29 '06 #1
7 2586
Niheel
2,456 Recognized Expert Moderator Top Contributor
Hi GAOZH,

Our policy on the site for homework help is that you must attempt to the problem first yourself. If you run into problems we can help you debug your code.

KUB
Aug 29 '06 #2
GAOZH
7 New Member
I worte a simple code below for the above mentionned requirements. Can tell me in what condition the program can not satisfy the requirements? :)

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

int main()
{
string firstWord, lastWord;
int count = 0;

cin >> firstWord; // Get the first word
lastWord = firstWord;

while(!cin.eof( )) // Check whether is end of file
{
cin >> lastWord; // Get the last word
count++;
}

cout << firstWord << " " << lastWord << " " << count << endl;
system("pause") ;

return 0;
}//end main
Aug 31 '06 #3
Banfa
9,065 Recognized Expert Moderator Expert
This doesn't work because cin.eof() is never true, i.e. you never get to the end of the input stream
Aug 31 '06 #4
GAOZH
7 New Member
End of input stream can be done by press Ctrl + z. I compile and run the above code and got all the output correct (Which I think so... : ) ). Then I changed the while loop from " while (!cin.eof()) " to "while (cin)", I got the same output. Is there any differences between the two while loops? ^_^
Aug 31 '06 #5
Banfa
9,065 Recognized Expert Moderator Expert
I tried Ctrl + Z and it didn't work, which isn't to say it does't work for you.

Logically the stdin never ends.
Aug 31 '06 #6
GAOZH
7 New Member
Try after typing the words you want, press Enter first, then Ctrl + z, then, Enter again to show the results.
Sep 4 '06 #7
Banfa
9,065 Recognized Expert Moderator Expert
Try after typing the words you want, press Enter first, then Ctrl + z, then, Enter again to show the results.
That is not intuitive and I would be surprised if you lost marks because of it (or found that like me your professor/teacher thinks the program does not work because you can't end the input stream).
Sep 4 '06 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

7
1764
by: tjshadyluver | last post by:
ok i have gotten this damn project almost done but i am frustrated on this one step. getting 18-35 together and 36-50 and so on. here is my code how can i get them combined in one line instead of writing it out a million times. please help me. i have to write a program that you type in the age and it stores it in a text file then i have to...
2
2273
by: N3TB1N | last post by:
Let me try again. I could use some help with this assignment, even though my teacher does not grade assignments.but because I need to know this stuff for a test very soon, but haven't been in class for awhile and don't know what I'm doing. I have included my (probably wrong) answers for the first few questions. It would be great if...
11
1691
by: spawn | last post by:
but I've been struggling with this for far too long and I'm about to start beating my head against the wall. My assignment seemed simple: create a program that will cacluate the running total of user inputs until it hits 100. At 100 it should stop. That's not the problem, in fact, that part works. It's the adding that isn't working. How...
1
1572
by: mastern200 | last post by:
I need some homework help with an assignment due wed. I need to make a program where in this program calculates the average of a set of test scores. The program will ask the user how many scores there will be. The user will be prompted to enter score 1, score 2, etc. After the scores are entered the computer will print the total number of...
5
2988
by: karafire2003 | last post by:
I was wondering if someone can help me with my homework. here's the assignment: a. Write a C program that has a declaration in main() to store the string "What's for lunch?" into an array named message. There should be a function call to restaurant() that accepts the message as an argument named menu and then displays the message using the...
1
1559
by: itgetsharder | last post by:
Hey, i was wondering if anyone could help me. i have two questions that i cannot complete for a homework assignment: This method should convert its parameter (a string like "3.1415") to the corresponding value of type double. If the string supplied is not a valid number, it should return 0.0 as its result. Note that you can use the method...
8
29150
by: garyrowell | last post by:
I have been at this programme for hours trying to work out what is wrong. Any help would be very much appricated. Here is the breif I received. The program This week you are going to write three classes: Card.java, Deck.java and DeckTester.java. The specification for each class is given below. Card.Java This is a simple class that...
3
4223
by: alireza6485 | last post by:
I need to write a C# program that asks for a pattern and for each pattern beeps one and goes silence for 1 second. This is my code: for (int i=0;i< pattern; i++) { Console.Beep(); ***************** //I have no idea what to put for 1 sec of silence }
0
7836
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8199
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7950
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8212
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3835
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2343
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1447
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.