Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,378 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,161 people online right now. Registration is fast and FREE... Join Now!




C++ assignmenet..

 
Reply to this topicStart new topic

C++ assignmenet.., unable to count the letters

shin_ono
post 29 Aug, 2008 - 08:37 PM
Post #1


New D.I.C Head

*
Joined: 29 Aug, 2008
Posts: 3

Hi guys,

i have a problem with my program, the counting of character etc "a,b,A,B,..." the program doesn't seems to be working.. any idea on what can be done? and also if i need to count the special char like %$# what type of input do i need to add in?

CODE

#include <stdio.h>
#include <ctype.h>

#define EOL '\n'

main()
{
    char letter[80];
    int tag, count, linecount = 1,wordcount = 1,character = 1, number =0;
    int i;

    while ((letter[0] = getchar()) != '*')  {

        /* read in a line of text */
        for (count = 1; (letter[count] = getchar()) != EOL; ++count)
          ;

    for(i=0;letter[i]!= '*';++i)

    {
    
    if((letter[i]==' ')||(letter[i]=='\n'))
       wordcount++;                        //increment of words
    if(letter[i]=='\n')
       linecount++;                        //increment of line
    if(isalpha(letter[i]))                   //increment of char
       character++;

    }
    }
  printf("\n\n\t\tWORDS  LINE  CHARACTOR \n ");
  printf("\t\t%d \t%d \t%d", wordcount,linecount,character);    //giving output
                
printf("\nGood Bye");
return 0;
    }
{
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 29 Aug, 2008 - 10:06 PM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,027



Thanked 173 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


Try something like this, it will get you a lot closer to your goal I think...

cpp

#include <stdio.h>
#include <ctype.h>


#define EOL '\n'

int main()
{
int linecount = 1,wordcount = 1,character = 1;
int ch;

// Prompt message to enter a sentence
printf("Enter your sentence and end it with a '*'...\n");

// Loop through each character and compare it to terminator char
while ((ch = getchar()) != '*') {

if ((ch == ' ') || (ch == '\n'))
wordcount++; //increment of words
if (ch=='\n')
linecount++; //increment of line
if (isalpha(ch)) //increment of char
character++;

}

// Print our results
printf("\n\n\t\tWORDS LINE CHARACTER \n ");
printf("\t\t%d \t%d \t%d", wordcount,linecount,character); //giving output

printf("\nGood Bye");
return 0;
}


The idea is that we just keep a tally of the characters as you go along. Notice that we store the character, compare it, increment our counters and keep going until we hit the asterisk. Then we go ahead and print out the counters.

It cleans out a bunch of unused variables, shortens up some of your lines, saves you on having to run other loops etc.

Give it a whirl and see if it can be something you would use.

Enjoy!

"At DIC we be char counting, card counting, and sesame street snufflupagus counting code ninjas... sesame street's count has nothing on us!" decap.gif

This post has been edited by Martyr2: 29 Aug, 2008 - 10:07 PM
User is offlineProfile CardPM

Go to the top of the page

shin_ono
post 30 Aug, 2008 - 07:44 PM
Post #3


New D.I.C Head

*
Joined: 29 Aug, 2008
Posts: 3

your program works fine for me, thanks..
but lets say if i want it to get out of the loop using EOF instead of *, what code do i need to add in?

as EOF = pressing ^z at the keyboard.. and i believe getchar() read in 1 char at a time, rite?

many thanks
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 30 Aug, 2008 - 07:58 PM
Post #4


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,328



Thanked 57 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


getchar will read from standard in. Which means once you hit enter, the input is fed to the program.
User is offlineProfile CardPM

Go to the top of the page

shin_ono
post 30 Aug, 2008 - 10:07 PM
Post #5


New D.I.C Head

*
Joined: 29 Aug, 2008
Posts: 3

lets say if i edited his code, to end with ^Z

CODE

#include <stdio.h>
#include <ctype.h>


#define EOL '^Z'

int main()
{
    int linecount = 1,wordcount = 1,character = 1;
    int ch;

    // Prompt message to enter a sentence
    printf("Enter your sentence and end it with a 'ctrl + Z'...\n");

    // Loop through each character and compare it to terminator char
    while ((ch = getchar()) != EOL)  {
    
        if ((ch == ' ') || (ch == '\n'))
           wordcount++;                        //increment of words
        if (ch=='\n')
           linecount++;                        //increment of line
        if (isalpha(ch))                   //increment of char
           character++;

    }

    // Print our results
    printf("\n\n\t\tWORDS  LINE  CHARACTER \n ");
    printf("\t\t%d \t%d \t%d", wordcount,linecount,character);    //giving output
                
    printf("\nGood Bye");
    return 0;
}


somehow, the program keeps looping to scan in letter.. anyone can help?
did i gave the wrong parameter for it?
User is offlineProfile CardPM

Go to the top of the page

sensui
post 30 Aug, 2008 - 10:35 PM
Post #6


D.I.C Head

Group Icon
Joined: 24 Aug, 2008
Posts: 132



Thanked 20 times

Dream Kudos: 50
My Contributions


Use the macro EOF ( end of file ) defined in stdio.h instead of EOL.
Example:
cpp
while ((ch = getchar()) != EOF)

But when running your program you should press twice CONTROL+Z to give the output.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 06:15AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month