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.
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
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
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
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? ^_^
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.
Try after typing the words you want, press Enter first, then Ctrl + z, then, Enter again to show the results.
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).
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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
}
|
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...
|
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. ...
|
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...
| |
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |