473,405 Members | 2,160 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,405 software developers and data experts.

Read input, return values

hi guys, i am trying to finish up a program i started this morning and basically i got the whole input and output down... i just need help with the function which will scan the file and return number of lines, amount of characters, and amount of alphabetical characters. I was wondering if i would need 3 different functions, and how would i go about setting those up.

here is my code so far

Expand|Select|Wrap|Line Numbers
  1. /*Write a program, which scans a file and counts the number of characters, the number of lines, and the number 
  2. of alphabetic characters in the file.Have main() open the file and pass the file pointer to a function named count. 
  3. Count passes back the number of characters and the number of alphabetic characters.  main() prints the answers and closes the file.
  4.  
  5. */
  6.  
  7. #include<iostream>
  8. #include<fstream>
  9. using namespace std;
  10.  
  11.  
  12.  
  13. char countline(char c);
  14. void valuefile(ifstream & in, ofstream & out);
  15.  
  16. int main (){
  17.  
  18.     ifstream in;
  19.     ofstream out;
  20.     char inName[100];
  21.     char outName[100];
  22.  
  23.     do{
  24.         cout << "Enter name of plain text file:\n";
  25.         cin >> inName;
  26.         in.open(inName);
  27.         if(!in.is_open()){
  28.             cerr << "Failed to open " << inName << " - Now exiting\n";
  29.             exit(-1);
  30.         }
  31.     }while(!in.is_open());
  32. do{
  33.     cout << "Enter name for output file:\n";
  34.     cin >> outName;
  35.     out.open(outName);
  36.     if(!out.is_open()){
  37.         cerr << "Failed to open " << outName << " - Now exiting\n";
  38.             exit(-1);
  39.     }
  40. }while (!out.is_open());{
  41. valuefile(in,out);
  42. cout << "Output file is in " << outName << endl;
  43. }
  44. return 0;
  45. }
  46.  
  47.  
  48.  
  49. char countline( char c){
  50.  
  51.     if (c == ' ' )
  52.         return c + 1;    // gives me a ! instead of adding amount of spaces
  53.     else 
  54.         return c;
  55. }
  56. void valuefile(ifstream & in, ofstream & out){
  57.     while( !in.eof()){
  58.         char c = in.get();
  59.         out << countline(c);
  60.  
  61.     }


any input would help, thanks guys
Dec 8 '06 #1
4 2697
DeMan
1,806 1GB
I would suggest three functions for code clarity, although because everything is related some people would advise to do it all cocurrently. To do this you would either need to be comfortable passing out pointers or give the results in an array or structure of some sort. I will assume the first, and outline the basic steps....

Depending on definition, counting lines can mean many things, I would advise you count end of line characters (looking for '\n' might work, but I think there is another value to represent end of line in this sort of context (though i can't think what it might be)) until you reach end of file (eof I think)
Your countline method returns ! because you are adding 1 to the character ' ' (which I don't think is what you want to do, that is, you are bumping the character forward)

Number of characters can also be done trivially using a while not eol loop, and simply count the characters. (Depending on what you are actually doing you might like to 'cheat' here. 1 character = 1 byte, so if you look at the file size it should be pretty close to the number of characters).

Number of of alphabetic characters is alslo reasonably easy. Implement a method that returns whether a character is alphabetical or not and count how many return true when you pass the characters from the file in. Basically, beacuse characters are numeric values, you can do normal logic on them as you would with numbers. You need to make sure that they are in one of the ranges 'a' to 'z' or 'A' to 'Z'
ie: if(thischar>'a'||thischar<'z'...etc);
you could also implement this as a switch statement eg:
Expand|Select|Wrap|Line Numbers
  1. switch(thischar)
  2. {
  3. case 'a': case'b': case 'c'......
  4. return true;
  5. default:
  6. return false
  7. }
  8.  
Dec 9 '06 #2
DeMan
1,806 1GB
I just reread my post, and think that for the most part, the tasks are trivial enough to be all in one place, though you might like to implement the isAlpha function. Then you can count lines, characters and alphabetic characters at the same time. (I have assumed above also, that when you want to count characters you want to count invisible ones too)
Dec 9 '06 #3
ok thanks for that response. my only problem now is programming the code to actually count these things, the way i keep trying it just changes the value to the next character.
Dec 9 '06 #4
DeMan
1,806 1GB
remove the method countline, and move the ifs into your main code wherever the method is currently read, then set up
in counter=0; (or similar) at the top of your program and increment that inside your if....

Expand|Select|Wrap|Line Numbers
  1. if(c=' ')
  2. {
  3.   counter++;
  4. }
  5.  
Dec 9 '06 #5

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

Similar topics

21
by: Gavin | last post by:
Hi, I'm a newbie to programming of any kind. I have posted this to other groups in a hope to get a response from anyone. Can any one tell me how to make my VB program read the Bios serial number...
6
by: Ruben | last post by:
Hello. I am trying to read a small text file using the readline statement. I can only read the first 2 records from the file. It stops at the blank lines or at lines with only spaces. I have a...
7
by: Randell D. | last post by:
Whats wrong with my sample script below? All I want to do is confirm that my form data is in my environment (basically, I'm writing a script that will check that some form fields have a value, and...
21
by: Jason Heyes | last post by:
I want to allow objects of my class to be read from an input stream. I am having trouble with the implementation. Here are the different approaches I have tried: // Version 1.0 - Default...
18
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
5
by: Pete | last post by:
I having a problem reading all characters from a file. What I'm trying to do is open a file with "for now" a 32bit hex value 0x8FB4902F which I want to && with a mask 0xFF000000 then >> right...
7
by: Naren | last post by:
Hello All, Can any one help me in this file read problem. #include <stdio.h> int main() {
1
by: Jose Reckoner | last post by:
I'm running python 2.3 on Windows XP. Anyone have a quick small script to convert .DT1 and .DEM data to ASCII or some other format? I don't need a viewer. Thanks!
12
by: sam | last post by:
hi all, i'm starting to put together a program to simulate the performance of an investment portfolio in a monte carlo manner doing x thousand iterations and extracting data from the results. ...
2
by: RyanS09 | last post by:
Hi- I have read many posts with specific applications of reading in text files into arrays, however I have not been able to successfully modify any for my application. I want to take a text file...
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:
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
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
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...
0
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...
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.