473,320 Members | 1,896 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,320 software developers and data experts.

C++ strings and output

I have written a function to output a certain number of words per line that is read in from an input file, then the user enters a number of words per line. For some reason the program does not always read the last word. Any help appreciated
this is what i have

void worddisplay(string fname, int num)
{
ifstream infile;
string text;
int count=0;

infile.open(fname.c_str());
infile >> text;
while(!infile.eof())
{
cout << text << " ";
count++;
if(count==num)
{
cout << endl;
count=0;
}
infile >> text;
}
cout << endl;
}
Nov 19 '11 #1
1 2015
johny10151981
1,059 1GB
What is wrong with your logical code:

Expand|Select|Wrap|Line Numbers
  1. read next word
  2. while(is end of file?)
  3. {
  4.  print read word;
  5.  read next word;// this word get printed after check the condition
  6.                 // after reading the last word you go to while condition check and the print it.
  7.                 // what is wrong with it?
  8.                 // it never print the last word. cause after reading the last word it get exited from loop and it does not do anything else.  
  9. }
so, finally rebuild your logic
Nov 21 '11 #2

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

Similar topics

20
by: Ravi | last post by:
Hi, I have about 200GB of data that I need to go through and extract the common first part of a line. Something like this. >>>a = "abcdefghijklmnopqrstuvwxyz" >>>b = "abcdefghijklmnopBHLHT"...
17
by: Noen | last post by:
def XOR(s1,s2): """ XOR string s1 with s2 """ output = "" # Argument check if (type(s1) and type(s2)) != type(""): raise TypeError, "Arguments are not strings" if len(s1) != len(s2): raise...
8
by: Marko Faldix | last post by:
Hello, with Python 2.3 I can write umlauts (a,o,u umlaut) to a file with this piece of code: import codecs f = codecs.open("klotentest.txt", "w", "latin-1") print >>f, unicode("My umlauts...
4
by: Kevin Mansel via .NET 247 | last post by:
Ok, basically this is my problem. I'm building a console app tocall a dos program. So i'm using the Shell command to call theprogram, now depending on what happens, I want to read theoutput that...
9
by: Diane | last post by:
Could you please explain me how can I output nested strings? Here is an example: "adsd{rfkm}xcv" The output should start from the inner parentheses, such as: dfF rfkm
2
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be...
0
by: flameboy | last post by:
I am currently trying to figure my way how to output strings to a file. I have seen all the many posts and such how to do it in a console program, and have had much success doing it that way. This...
4
by: roxorsoxor2345 | last post by:
I have this custom string class that I have created. #include <cstdlib> #include <iostream> using namespace std; class Cstring { private:
6
by: Jojo | last post by:
Hi all, I was wondering how I can perform formatted output with C++ strings. For example, suppose I have in plain C: sprintf(C_string, "%5.2f %6d"); How can I do such a thing with C++...
2
by: bearophileHUGS | last post by:
Helmut Jarausch: Asking in comp.compression is a good starting point. My suggestions (sorry if they look a bit unsorted): it depends on what language you want to use, how much you want to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.