İ dont know anything about file IO.if you have any idea how can write a c++ program to my hw pls share your ideas.homework is in the attchement.

>İ dont know anything about file IO
That's nice, so what are you going to do about it? If you said "Post to Daniweb and hope someone does my homework for me", you get a slap on the wrist. If you said "Open my book and try to figure it out on my own", you get a cookie.

>İ dont know anything about file IO
That's nice, so what are you going to do about it? If you said "Post to Daniweb and hope someone does my homework for me", you get a slap on the wrist. If you said "Open my book and try to figure it out on my own", you get a cookie.

no,you are wrong.i've tried lots of time.but couldn't reach a solution.then i decided to share that with you.if you dont want to help pls dont depress others.

A Classic example of where to use pseudo code....
see discussion at http://www.daniweb.com/techtalkforums/thread41482.html

PSEUDO CODE
// Input file names
=> code here
// define an array to accumulate results
=> code here
// in a loop, Read input file, line by line
=> code here
// Extract the various items from the line and add into the results array
=> code here
// end the loop
// Save the array data to the ouput file
=> code here

Now fill in the code, step by step. :)

>no,you are wrong.
Prove it.

>i've tried lots of time.
I don't see your attempts. All I see is a problem description. Where are the "lots of time" you've tried?

>if you dont want to help pls dont depress others.
I (and anyone else qualified to answer questions here) refuse to help people who want a free ride. If you've actually made an attempt as you say, you'll be able to tell us what you've tried, what didn't work and why, and what you learned from it. If you can't supply that information, we have no choice but to assume you haven't made an attempt and want a handout. If that depresses you, tough. It's how this forum works, and childish comments won't change that.

Member Avatar for iamthwee

>Slap on the wrists

>free cookies

Where do I sign up? ;)

ok.the problem it thah how can i use the isdigit() function to find the digits in the text.i can't construct the loop.i hope this much more specific question so you can help me.

Member Avatar for iamthwee

From your assignment, it looks as if you have to use char arrays for this. Personally, I'd prefer using std::strings, however, there's no point discussing them if they're not to be used.

So you separate each line of the text into a const char array.

Then you would loop through the array and test each char with the function isdigit(). If it finds a digit it will register with a non-zero quantity. Everytime it does this you increment a variable count.

You would do the same for the letters, vowels etc.

>the problem it thah how can i use the isdigit() function to find the digits in the text
That's much better, thank you. Most likely, you aren't allowed to use the std::string class yet, so you're stuck with C-style strings. In the case, you can simply loop from the beginning of the string until you reach a '\0' character. If the current character is a digit (as specified by std::isdigit), increment a counter:

#include <iostream>
#include <cctype>
#include <cstddef>

int main()
{
  char buf[1024];

  std::cout<<"Enter a line: ";

  if ( std::cin.getline ( buf, sizeof buf ) ) {
    int digits = 0;
    std::size_t i;

    for ( i = 0; buf[i] != '\0'; i++ ) {
      if ( std::isdigit ( buf[i] ) )
        ++digits;
    }

    std::cout<<"There are "<< digits <<" digits in \""<< buf <<"\"\n";
  }
}

>the problem it thah how can i use the isdigit() function to find the digits in the text
That's much better, thank you. Most likely, you aren't allowed to use the std::string class yet, so you're stuck with C-style strings. In the case, you can simply loop from the beginning of the string until you reach a '\0' character. If the current character is a digit (as specified by std::isdigit), increment a counter:

#include <iostream>
#include <cctype>
#include <cstddef>

int main()
{
  char buf[1024];

  std::cout<<"Enter a line: ";

  if ( std::cin.getline ( buf, sizeof buf ) ) {
    int digits = 0;
    std::size_t i;

    for ( i = 0; buf[i] != '\0'; i++ ) {
      if ( std::isdigit ( buf[i] ) )
        ++digits;
    }

    std::cout<<"There are "<< digits <<" digits in \""<< buf <<"\"\n";
  }
}

thank you,that really helped me.i hope i can be more sophisticated about c++ .

#include <iostream>
#include <ctype.h>
#include <fstream>
using namespace std;
int main() {
        ifstream infile;
        ofstream outfile;
        infile.open("poem.txt");
        if(! infile) 
                cout<<"file couldn't open";
        int cn=0;
        int i=0;
        while (!infile.eof() ) {
        char x[1024];
    
        infile>>x[i];
        if(isdigit(x[i])==0) {
                cn++;
        i++; }
    else
                i++;  
        }
        cout<<"NUMBER OF DIGITS   "<<cn<<endl; 
        cout<<"NUMBER OF CHARACTERS  "<<i; 
        
        return 0; }

in this code cant read the digits correctly but it doesn't give an error.where am i wrong??? :rolleyes:

>while (!infile.eof() )
eof() is not intended to be used as a loop condition. Use the result of your input request instead.

>if(isdigit(x)==0)
This says "if x is *not* a digit".

I also don't see the need for an array unless you want to limit the size of the file you can read to 1024 bytes. However, since you don't set that limit anywhere except the size of the array, you have a bug waiting to happen if the file has more than 1024 bytes and you walk past the end of the array.

You can also clean up your if statement because incrementing i happens on both branches. Therefore, you don't need an else clause and can simply increment i with every iteration of the loop:

#include <iostream>
#include <cctype>
#include <fstream>

using namespace std;

int main() {
  ifstream infile;

  infile.open("test.txt");

  if(! infile) 
    cout<<"file couldn't open";

  int cn=0;
  int i=0;
  char x;

  while (infile>>x) {
    i++;
    if(isdigit(x))
      cn++;
  }

  cout<<"NUMBER OF DIGITS   "<<cn<<endl; 
  cout<<"NUMBER OF CHARACTERS  "<<i; 

  return 0;
}

I would also recommend a consistent formatting style to make finding bugs easier.

>if(isdigit(x)==0)
This says "if x is *not* a digit".

will it be 1 instead of 0???

>will it be 1 instead of 0???
No, it will be non-zero. So to test for a digit you would say:

if ( isdigit ( x ) != 0 )

But since a test for != 0 is the default, most people simply do this:

if ( isdigit ( x ) )

And use the logical NOT for the opposite test because the intention is more obvious and consistent with the true test:

if ( isdigit ( x ) ) /* if x is a digit */
if ( !isdigit ( x ) ) /* if x is not a digit */

thank you...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.