C++ HELP PLEASE very very important!

Discussion in 'Computer Science & Culture' started by Bardia, Jul 2, 2003.

Thread Status:
Not open for further replies.
  1. Bardia Registered Member

    Messages:
    2
    I have never programmed with c++ and i need this simple program, but i need it fast.
    A program which counts the number of , characters, words and lines in a text file and also tells how many characters is digit, character, punctuation and other.

    I would pray that best things happenes to who ever does this for me. thast all i can do. please guys thank you. I need it ASAP
     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. Ender Registered Senior Member

    Messages:
    294
    Thats not how we do things here. YOu need to post some of the code that you have, then we will correct/ add to it wor you.
     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. Bardia Registered Member

    Messages:
    2
    Yes I understand. Blow you can see the problem andthe solution i have. but it doesnt work. it would be great if you could see whats the problem and fix it. to be hones its not even my program, i had to write it for somebody but i didnt have time. so if you can help me it would be Amazing. I dont have much time too!

    Problem:

    You have been asked to write a text analysis program.
    This program will calculate and output the overall
    number of characters, words and lines within a text
    file. In
    addition, the program will analyze the number of
    characters and determine how many characters are
    alphabetic, numeric, or punctuation.



    Sample Output:

    The text file consisted of 2 lines.
    The text file consisted of 27 words.
    The text file consisted of 128 characters
    Alphabetic 100
    Numeric 12
    Punctuation 8
    Other 8

    -------------------------
    Code:



    #include iostream>
    #include fstream>
    #include stdlib.h>
    #include stdio.h>
    #include cstdlib>

    int main()
    {
    int alphacout=0,
    digitcout=0,
    wordcout=0,
    linecout=0,
    puncout=0,
    charcout=0,
    othercot=0;
    int curr_char,
    pre_char;
    char ch;
    //ifstream constructor opens the file
    ifstream infile("c:\\text1.txt",ios::nocreate);

    if (!infile){
    cerr<<"File could not be opened\n";
    exit(1); //ends Entire program!!
    }

    while(!Infile.eof())
    {
    Infile.get(ch);

    if(isalpha(ch))
    ++alphacout=0;
    else if(isdigit(ch))
    ++digitcout;
    else if(ispunctuation(ch))
    ++puncout;
    else if(ch=='\n')
    ++linecout;
    else if (curr_char==' '&& pre_char!=' ')
    ++wordcout;
    else
    ++ othercout

    charcount=alphacount+digitcountcount+puncout+othercout;
    }

    ofstream outfile("c:\\text1.txt",ios:

    Please Register or Log in to view the hidden image!

    ut);
    if(!infile.is.open()){
    cerr<<"unable to open outputfile\n";
    exit(1);
    }
    cout<<"The text file consisted of
    "<<alphacount<<"lines"<<endl;
    cout<<"The text file consisted
    of"<<wordcout<<"words"<<endl;
    cout<<"The text file consisted
    of"<<charcout<<"characters"<<endl;
    cout<<"\tAlphabetic"<<'\t'<<alphacout<<endl;
    cout<<"\tNumeric"<<'\t'<<digitcout<<endl;
    cout<<"\tPunctuation"<<'\t'<<puncout<<endl;
    cout<<"\tOther"<<'\t'<<othercout<<endl;

    infile.close();
    return 0;
    }
     
    Last edited: Jul 2, 2003
  6. Google AdSense Guest Advertisement



    to hide all adverts.
  7. CuriousGene Supreme Allied Commander Registered Senior Member

    Messages:
    114
    Christ Sake Kid . . .

    DO YOUR OWN HOMEWORK.

    IS THERE A MODERATOR FOR THIS LIST??? WE NEED TO BAN THESE POSTS.
     
  8. testify Look, a puppy! Registered Senior Member

    Messages:
    508
    First: Is there something wrong with posting code and asking for help with it? I know we frown upon simply asking to write a complete program/script, but as far as I know there is nothing wrong with asking for some help.

    Second: I think the moderators know their jobs quite well. I am not quite sure how sciforums has been setup with vBulletin but I am more than confident that there is someone that moderates this particular forum. You do not need to tell them what should be banned or not be banned. More so, I do not think that your current amount of experience on sciforums gives you the ability to decide what posts should be banned.
     
  9. pragmathen 0001 1111 Registered Senior Member

    Messages:
    452
    Ready the Lynch Mob!

    My I/O with regards to file handling isn't up to par just yet, but you get the gist of it. Keep plugging away Bardia--it looks like you understand a good deal of what you're asking about!

    #include &lt;conio.h&gt;
    #include &lt;stdlib.h&gt;
    #include &lt;stdio.h&gt;

    <b>void</b> main(&nbsp; )
    {
    &nbsp;&nbsp;&nbsp;<b>int</b> digit = 0;
    &nbsp;&nbsp;&nbsp;<b>int</b> alpha = 0;
    &nbsp;&nbsp;&nbsp;<b>int</b> words = 0;
    &nbsp;&nbsp;&nbsp;<b>int</b> punct = 0;
    &nbsp;&nbsp;&nbsp;<b>char</b> inputString[500];
    &nbsp;&nbsp;&nbsp;cout << "Enter a string of whatever you want:\n-->";
    &nbsp;&nbsp;&nbsp;cin.getline(inputString, 500, '\n');
    &nbsp;&nbsp;&nbsp;<b>for</b> (<b>unsigned int</b> i = 0; i < strlen(inputString); i++)
    &nbsp;&nbsp;&nbsp;{
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>if</b> (isdigit(inputString))
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;digit++;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>else if</b> (isalpha(inputString))
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alpha++;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>else if</b> (ispunct(inputString))
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;punct++;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>else if</b> (isspace(inputString) && inputString[i-1] != '&nbsp;')
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;words++;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
    &nbsp;&nbsp;&nbsp;}
    &nbsp;&nbsp;&nbsp;cout << "The string contains " << alpha << " characters.\n";
    &nbsp;&nbsp;&nbsp;cout << "The string contains " << digit << " numericals.\n";
    &nbsp;&nbsp;&nbsp;cout << "The string contains " << punct << " punctuations.\n";
    &nbsp;&nbsp;&nbsp;cout << "The string contains " << words + 1 << " words.\n";
    &nbsp;&nbsp;&nbsp;getch(&nbsp; )
    }

    Sample:
    Enter a string of whatever you want:
    -->Hi, my name is Bardia and I have 2 posts already!
    The string contains 36 characters.
    The string contains 1 numericals.
    The string contains 2 punctuations.
    The string contains 11 words.
     
  10. S.Tarafdar Registered Senior Member

    Messages:
    35
    Equality

    CuriousGene=SG.N
    (To(/w)o intellectuals!!)
     
  11. CuriousGene Supreme Allied Commander Registered Senior Member

    Messages:
    114
    Way to jump to conclusions guys . . .

    It's so obviously clear that is a hw assignment. There is nothing wrong with posting code and asking for help as long as it follows a general sense of ethics. Testify, that looks like a good chunk of code to me. It clearly sends a message to the student . . . "you do not need to finish all your programming assignments since you can abuse www.sciforums.com as a medium to obtain your programming solutions for homework submission." If you argue against that, you need to seriously check your code of ethics Testify.

    Second, I believe it is in my right to ask the moderator (which I believe there is none for this particular forum) to ban posts that ask for homework solutions. Most importantly, your notion that my twenty-something odd posts is a metric of my ability to decide what posts should be banned is naive. Clearly, given your amount of posts, there has been no addition of wisdom imparted on you.
     
  12. testify Look, a puppy! Registered Senior Member

    Messages:
    508
    *waves a finger at himself* BAD TESTIFY. Well, ok I guess he has sufficient (heh) experience on the forums, but I still see no need to grill Bardia for his post.
     
Thread Status:
Not open for further replies.

Share This Page