File io

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Fir3Bat
    New Member
    • Oct 2006
    • 19

    File io

    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

    Code:
    #include <iostream>
    #include <fstream>
    #include <sstream>
    
    using namespace std;
    
    int main()
    {
    	string sWord;
    	int n, arnCountE[40], nNumber;
    	ifstream fin;
    	fin.open("71cIN.txt");
    	while(!fin.fail())
                    {
    		fin >> sWord;
    		cout << sWord;
    	}
    	fin.close();
    	
    	return 0;
    }
    what the problem is is that the words are all together

    Output
    (Whatabeautiful daytobeprogramm ing.)

    how do i correct this?
    and i am having trouble with the arrays (counting the 'e's)
    can someone help :(
    thanks
  • hapa
    New Member
    • Oct 2006
    • 31

    #2
    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

    Comment

    • hapa
      New Member
      • Oct 2006
      • 31

      #3
      for instance

      string s1;

      getline(fin,s1)

      cout<<s1<<endl;


      it stores the whole line in the s1

      Comment

      • hapa
        New Member
        • Oct 2006
        • 31

        #4
        [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

        Code:
        #include <iostream>
        #include <fstream>
        #include <sstream>
        
        using namespace std;
        
        int main()
        {
        	string sWord;
        	int n, arnCountE[40], nNumber;
        	ifstream fin;
        	fin.open("71cIN.txt");
        	while(!fin.fail())
                        {
        		fin >> sWord;
        		cout << sWord<<' ';               // just add a space here only no need for using getline function
        	}
        	fin.close();
        	
        	return 0;
        }

        use this code now
        i have added a space in Cout statement in the while part

        Comment

        • Fir3Bat
          New Member
          • Oct 2006
          • 19

          #5
          well that works thanks
          and for the counting the 'e's??
          sry for so many questions

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            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'.

            Comment

            • Fir3Bat
              New Member
              • Oct 2006
              • 19

              #7
              Originally posted by Ganon11
              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;
              }

              Comment

              • Ganon11
                Recognized Expert Specialist
                • Oct 2006
                • 3651

                #8
                Originally posted by Fir3Bat
                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.

                Code:
                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:
                Code:
                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:
                Code:
                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:

                Code:
                ...
                string message;
                message = "This is a sample string.";
                ...
                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.

                Comment

                Working...