473,402 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Homework Help

7
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 2573
Niheel
2,460 Expert Mod 2GB
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
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 Expert Mod 8TB
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
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 Expert Mod 8TB
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
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 Expert Mod 8TB
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
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...
2
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...
11
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...
1
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...
5
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...
1
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...
8
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...
3
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++) { ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.