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

File io

19
I have been using file io for like a week now, not an expert but this is for my computer science class.

Say the file is 71cIN.txt and inside the file it said "What a beautiful day to be programming."

And what i have to do is display all the words, and count all the letter 'e's in the sentence.

this is wat i have so far

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     string sWord;
  10.     int n, arnCountE[40], nNumber;
  11.     ifstream fin;
  12.     fin.open("71cIN.txt");
  13.     while(!fin.fail())
  14.                 {
  15.         fin >> sWord;
  16.         cout << sWord;
  17.     }
  18.     fin.close();
  19.  
  20.     return 0;
  21. }
  22.  
what the problem is is that the words are all together

Output
(Whatabeautifuldaytobeprogramming.)

how do i correct this?
and i am having trouble with the arrays (counting the 'e's)
can someone help :(
thanks
Oct 25 '06 #1
7 1757
hapa
31
Why dont you use the getline function it will read the whole line and you can store it is a string and cout the output
Oct 25 '06 #2
hapa
31
for instance

string s1;

getline(fin,s1)

cout<<s1<<endl;


it stores the whole line in the s1
Oct 25 '06 #3
hapa
31
[quote=Fir3Bat]I have been using file io for like a week now, not an expert but this is for my computer science class.

Say the file is 71cIN.txt and inside the file it said "What a beautiful day to be programming."

And what i have to do is display all the words, and count all the letter 'e's in the sentence.

this is wat i have so far

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     string sWord;
  10.     int n, arnCountE[40], nNumber;
  11.     ifstream fin;
  12.     fin.open("71cIN.txt");
  13.     while(!fin.fail())
  14.                 {
  15.         fin >> sWord;
  16.         cout << sWord<<' ';               // just add a space here only no need for using getline function
  17.     }
  18.     fin.close();
  19.  
  20.     return 0;
  21. }
  22.  

use this code now
i have added a space in Cout statement in the while part
Oct 25 '06 #4
Fir3Bat
19
well that works thanks
and for the counting the 'e's??
sry for so many questions
Oct 25 '06 #5
Ganon11
3,652 Expert 2GB
As for the 'e's, you can write a simple function that takes a string variable and loops through the positions of the string. Compare each character to 'e' if they equal e, increment an integer variable. The function can return that integer when finished. Pseudocode may look like this:

int numOfE(string message) {

int sum = 0;
for (int i = 0; loop i through the message.length) {

if (character of message at position i is e (or E))
sum++;
}

return sum;
}

You may not know this, but using the statement message[integer here] returns the character in that specific position - it's very easy to compare that character to 'e' or 'E'.
Oct 25 '06 #6
Fir3Bat
19
As for the 'e's, you can write a simple function that takes a string variable and loops through the positions of the string. Compare each character to 'e' if they equal e, increment an integer variable. The function can return that integer when finished. Pseudocode may look like this:

int numOfE(string message) {

int sum = 0;
for (int i = 0; loop i through the message.length) {

if (character of message at position i is e (or E))
sum++;
}

return sum;
}

You may not know this, but using the statement message[integer here] returns the character in that specific position - it's very easy to compare that character to 'e' or 'E'.
ok, just 1 problem, wat do u mean by character of message at position i??
wont i have to use arrays?
this is wat i have
for (n = 0; n > 40; n++)
{
if ( (here is my problem) ) - how do i check the position?
{
sum++;
}
return sum;
}
Oct 26 '06 #7
Ganon11
3,652 Expert 2GB
ok, just 1 problem, wat do u mean by character of message at position i??
wont i have to use arrays?
this is wat i have
for (n = 0; n > 40; n++)
{
if ( (here is my problem) ) - how do i check the position?
{
sum++;
}
return sum;
}
The loop you have here will check the first 40 characters of a string - all well and good if you have a string of length 40. However, this is unlikely. The first thing I'd suggest is using the string's length (by using the stringVariable.length() method) to control n.

Expand|Select|Wrap|Line Numbers
  1. for (int n = 0; n < stringVariable.length(); n++) {
Next, the comparison.

n will loop through position 0 to stringVariable.length() - 1; in other words, every position of a character in the string. Consider the following statement:
Expand|Select|Wrap|Line Numbers
  1. char ch = stringVariable[n];
This stores the character at position n into ch - you can then compare ch to 'e' or 'E' with the following if statement:
Expand|Select|Wrap|Line Numbers
  1. if (ch == 'E' || ch == 'e') {
The || means "OR" to C++; in other words, the if statement will be true if ch is E OR if ch is e.

The character of message at position i means the following:

Suppose I have the following statements:

Expand|Select|Wrap|Line Numbers
  1. ...
  2. string message;
  3. message = "This is a sample string.";
  4. ...
Suppose I then declare a char variable named ch, and set ch to message[10]. Then ch is now 's' - the s starting the word sample. A string is actually an array of characters - the string above, message, is actually an array of type char with length 24 (having values at message[0]...message[23]). Thus, you can access any character in message by accessing the position of that character in the string as you would a normal array. The g in that string, therefore, is message[22], and so on.
Oct 26 '06 #8

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

Similar topics

2
by: matt | last post by:
I have compiled some code, some written by me, some compiled from various sources online, and basically i've got a very simple flat file photo gallery. An upload form, to upload the photos and give...
5
by: Dave Smithz | last post by:
Hi There, I have a PHP script that sends an email with attachment and works great when provided the path to the file to send. However this file needs to be on the same server as the script. ...
7
by: Joseph | last post by:
Hi, I'm having bit of questions on recursive pointer. I have following code that supports upto 8K files but when i do a file like 12K i get a segment fault. I Know it is in this line of code. ...
3
by: StGo | last post by:
How can i read/write file's custom attributs(like subject,author...) in C#??? Thanks :))
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
13
by: Sky Sigal | last post by:
I have created an IHttpHandler that waits for uploads as attachments for a webmail interface, and saves it to a directory that is defined in config.xml. My question is the following: assuming...
1
by: Roy | last post by:
Hi, I have a problem that I have been working with for a while. I need to be able from server side (asp.net) to detect that the file i'm streaming down to the client is saved...
3
by: Shapper | last post by:
Hello, I created a script to upload a file. To determine the file type I am using userPostedFile.ContentType. For example, for a png image I get "image/png". My questions are: 1. Where can...
0
by: troutbum | last post by:
I am experiencing problems when one user has a document open through a share pointing to the web site. I use the dsolefile to read the contents of a particular directory and then display them in a...
0
by: thjwong | last post by:
I'm using WinXP with Microsoft Visual C++ .NET 69462-006-3405781-18776, Microsoft Development Environment 2003 Version 7.1.3088, Microsoft .NET Framework 1.1 Version 1.1.4322 SP1 Most developers...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
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
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
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...

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.