473,486 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Strings and file read in

2 New Member
For some reason my program is not reading the last input in the two loops i have completed so far. In the first main while loop i had to use words=1 to get the right value but i know by the average calculated on output it is not reading the last word and the same problem occurs in void worddisplay. Help appreciated
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. #include <string>
  5. #include <cctype>
  6. #include <fstream>
  7. using namespace std;
  8.  
  9. void worddisplay(string,int); // Function to display file in proper format                             
  10. int longest(string,int); // Function for longest word                                                  
  11. int wordlength(string); // Function to determine word length                                           
  12. int lettercount(string); // Function to count the letters in a word                                    
  13. int digitcount(string); // Function to count digits in word                                            
  14. int consonants(string); // Function to count lowercase consonants in word                              
  15. int charcount(string); // Function to count number of non alnum characters                             
  16.  
  17. int main()
  18. {
  19.   ifstream infile; // Represent the input file                                                         
  20.   string fname; // Actual name of file                                                                 
  21.   string text; // Variable used for input of non-whitespace from input file                            
  22.   int num; // Used for number of words to display per line in output           int lng;
  23.   int wlen=0; // Variable used for word length                                                         
  24.   int digits=0; // Count variable for number of digits                                                 
  25.   int words=1; // Count variable for number of words                                                   
  26.   int concount=0; // Count variable for number of consonants                                           
  27.   int letcount=0; // Count variable for number of letters                                              
  28.   int chcount=0; // Count for number characters not letters of digits                                  
  29.   double average; // Equation for finding average word length                                          
  30.  
  31.   cout << "Please enter the name of the input file" << endl;
  32.   cin >> fname;
  33.   cout << "Please enter number of words per line to display (2-10)" << endl;
  34.   cin >> num;
  35.   cout << " " << endl;
  36.   cout << "Ryan Hedderly Assignment #9 Section #1001" << endl;
  37.   cout << " " << endl;
  38.   infile.open(fname.c_str());
  39.   cout << "Input file: " << fname << endl;
  40.   cout << " " << endl;
  41.   infile >> text;
  42.   while(!infile.eof())
  43.     {
  44.       wordlength(text);
  45.       lettercount(text);
  46.       digitcount(text);
  47.       consonants(text);
  48.       charcount(text);
  49.       lng = wordlength(text);
  50.       longest(text,lng);
  51.       wlen = wlen + wordlength(text);
  52.       letcount = letcount + lettercount(text);
  53.       digits = digits + digitcount(text);
  54.       concount = concount + consonants(text);
  55.       chcount = chcount + charcount(text);
  56.       words++;
  57.       infile >> text;
  58.     }
  59.   worddisplay(fname,num);
  60.   infile.close();
  61.   cout << " " << endl;cout << left << setw(30) << "Longest word:" << " " << endl;
  62.   cout << left << setw(35) << "# words:" << right << setw(7) << words << endl;
  63.   cout << left << setw(35) << "# letters:" << right << setw(7) << letcount << endl;
  64.   cout << left << setw(35) << "# lower case consonants:" << right << setw(7) << concount << endl;
  65.   cout << left << setw(35) << "# digits:" << right << setw(7) << digits << endl;
  66.   cout << left << setw(35) << "# non alphanumeric chars:" << right << setw(7) << chcount << endl;
  67.   cout << fixed << setprecision(3);
  68.   average = static_cast<double>(wlen)/words;
  69.   cout << left << setw(35) << "average word length:" << right << setw(7) << average << endl;
  70.   return 0;
  71. }
  72. void worddisplay(string fname, int num)
  73. {
  74.   ifstream infile;
  75.   string text;
  76.   int count=0;
  77.  
  78.   infile.open(fname.c_str());
  79.   infile >> text;
  80.   while(!infile.eof())
  81.     {
  82.       cout << text << " ";
  83.       count++;
  84.       if(count==num)
  85.         {
  86.           cout << endl;
  87.           count=0;
  88.         }
  89.       infile >> text;
  90.     }
  91.   infile.close();
  92.   cout << endl;
  93. }
  94. int wordlength(string text)
  95. {
  96.   text.length();
  97. }
  98. int longest(string text, int lng)
  99. {
  100. }
  101. int lettercount(string text)
  102. {}
  103. int digitcount(string text)
  104. {
  105. }
  106. int consonants(string text)
  107. {
  108. }
  109. int charcount(string text)
  110. {
  111. }
Nov 20 '11 #1
1 2278
johny10151981
1,059 Top Contributor
Its not possible to help you by reading this code, please write your code in code tag

and more over please try to implement debugging
Nov 21 '11 #2

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

Similar topics

2
1622
by: Jaap de Bergen | last post by:
Hello people, I'm optimizing my script because my server is under heavy load in the evening. I have a question which methode is more efficient for my server: ==================...
2
2186
by: Boris Boutillier | last post by:
HI all, I came across a strange behaviour with read method, and I'm not sure if this is a filesystem problem or a misuse of this method. I do the simple following thing : f = open...
3
2764
by: Tony Murphy | last post by:
Strange problem, I read a file into a string using ifstream, ostringstream and string and end part of the file is missing (file size ~9.5kb, ~9k read). its a html file. using windows nt 4 ...
7
2225
by: Naren | last post by:
Hello All, Can any one help me in this file read problem. #include <stdio.h> int main() {
6
3288
by: r.z. | last post by:
This piece of code crashes my app: basic_ifstream<TCHARfile(levelfilepath, ios_base::binary); unsigned int name_length; file.read( (TCHAR*)&name_length, sizeof(unsigned int) ); //crashes here -...
2
1909
by: js | last post by:
Hi list. I'm writing a tail -f like program in python and I found file.read() doesn't work as I think it should. Here's the code illustrating my problem. ### #!/usr/bin/env python import...
2
1514
by: Prateek | last post by:
I have a wierd sort of problem. I'm writing a bunch of sets to a file (each element is a fixed length string). I was originally using the built-in sets type but due to a processing issue, I had...
2
6081
by: frank knuckles | last post by:
After exporting an Access table to Excel, I would like to make the Excel file Read-Only. Is it possible to do this with VBA from within Access? Thanks. FK
0
6964
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...
0
7123
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,...
0
7175
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
7319
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...
1
4864
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...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
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 ...
1
598
muto222
php
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.