Thread: C++ newbie needs help

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    2

    Smile C++ newbie needs help

    Hi, ive just recently started using borland c++ v5.02 and am having some difficulty.

    I have to write a program that does the following;

    1, it should prompt for and allow the user to enter a text string such as one or more sentences
    and perform the following actions -

    2, it should inform the user of the number of words in the text string

    3, it should count and inform the number of upper and lower case characters in the text.

    it should count and inform the number of occurences of a given letter, selected by the user,
    wether it is present in upper or lowercase within the text; if the letter is not present,
    it should state 'not present in text'.

    the program may work in either of the following ways;

    once the text is entered it may perform each of the tasks in turn, then exits.
    the 'search and count for occurance letters' should allow the user to repeat for different letters.

    once the text is entered the user is given a menu of choices for the three items above,
    and allowed to choose repeatedly without re-running the program. the menu must also include an option to quit


    this is what i have so far, but the word count doesn't work correctly and i cant for the life of me figure out how
    to do the third task. someone please help me

    Code:
    #include <iostream.h>
    #include <cstring.h>
    #include <conio.h>
    
    void main() {
    	string s;
       int uppercase = 0;
       int lowercase = 0;
       int chcount=0;  //counts non-space characters
       int wdcount=1;  //counts spaces between words
       char ch = 'a';
    
      cout<<"Enter A Sentence Or Sentences ";
      getline(cin,s);
      for (int i =0; i<s.length(); i++)
         if (isupper(s[i]))
            uppercase++;
         else if (islower(s[i]))
           lowercase++;
      cout<<uppercase<<" = Number Of Characters That Are Upper Case  "<<endl;
      cout<<lowercase<<" = Number Of Characters That Are Lower Case  "<<endl;
      		while( ch != '\r' ) //loop until Enter typed
     {
     		ch = getche();
     		if( ch==' ' )
     		wdcount++;
     			else
     			chcount++;
     }
     			cout << "\nWords=" << wdcount << endl
     			<< "Letters=" << (chcount-1) << endl;
     }
    Last edited by random_dave; 11-30-2004 at 01:54 PM.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    1. Please use the [code][/code] tags to post code. Otherwise, the formatting is lost and your code gets really hard to read.

    2. You're using non-standard and even non-existent headers (I've never heard of any compiler supporting <cstring.h>). The correct headers to include are <iostream> and <cstring>. <conio.h> should be avoided if possible. Be aware that this puts quite a few of the symbols you use into the namespace std. Short-term solution is to put
    using namespace std;
    below your includes.

    3. But checking your source, you don't need <cstring>, you need <cctype>.

    4. main() returns int. No exceptions. You may, however, omit the return 0 at the end if you have a standard-compliant compiler.

    5. Don't mix I/O methods. Either use only C++ I/O or use only C I/O. Mind you, getche is neither, it's non-standard.

    6. If I understand the requirements correctly, you should be working on the same input for all tasks. You, however, read in first one string for upper and lower case counting, then another for word counting.
    Last edited by CornedBee; 11-30-2004 at 01:57 PM.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    #include <cstring>
    ...
    string s;
    Wrong header for using the string container... you want to be using <string>

    Code:
    #include <string>
    ...
    std::string s;
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    2
    ok, well ive only stared using c++ recently. the code i did myself first is

    Code:
    void main() {
    	string s;
       int uppercase = 0;
       int lowercase = 0;
    
      cout<<"Enter A Sentence Or Sentences ";
      getline(cin,s);
      for (int i =0; i<s.length(); i++)
         if (isupper(s[i]))
            uppercase++;
         else if (islower(s[i]))
           lowercase++;
      cout<<uppercase<<" = Number Of Characters That Are Upper Case  "<<endl;
      cout<<lowercase<<" = Number Of Characters That Are Lower Case  "<<endl;
    
     }
    how would i encorporate the other two functions that i need to use? I would have to use nested if statements right? Any help is appreciated

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    You can get a pretty good idea of the number of words by counting the number of spaces and adding 1 (usually no space after the last word), or get a more accurate count reading in one string at a time using >> instead of getline(). If the text has multiple spaces between sentences as in this paragraph, then the simple space counting trick would need to be modified (see below). At some point you will need to do char by char analysis to meet the requirements of indicating the number of any given char in the input. You can parse the string(s) you read in char by char, or simply read in one char at a time rather than one string or one line at a time. The char by char analysis will allow you to handle double spaces between sentences to get a more accurate count of the number of words if you count spaces.
    Last edited by elad; 11-30-2004 at 05:28 PM.

  6. #6
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79
    Hi Random_Dave. I wrote the original code that you posted over in the way I thought was better, trying not to change too much of what you wrote. I'm using the Dev-C++ compiler, which I recommend very highly. Please take all of the others advice. These guys are all very knowledgable. In particular, always use int for the return type of main(). I would also always use return 0 unless you are checking for an error. If you were, you might want to use return 1 or return -1 instead.

    Please notice that I removed the line:
    Code:
        ch = getche();
    It didn't make sense, and was causing trouble (as in compile time error).
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main() {
       string s;
       int uppercase = 0;
       int lowercase = 0;
       int chcount=0;  //counts non-space characters
       int wdcount=1;  //counts spaces between words
       char ch = 'a';
    
       cout<<"Enter A Sentence Or Sentences ";
       getline(cin,s);
       for (int i =0; i<s.length(); i++)
         if (isupper(s[i]))
            uppercase++;
         else if (islower(s[i]))
           lowercase++;
       cout<<uppercase<<" = Number Of Characters That Are Upper Case  "<<endl;
       cout<<lowercase<<" = Number Of Characters That Are Lower Case  "<<endl;
        while( ch != '\r' ) //loop until Enter typed
        {
             if( ch==' ' )
                 wdcount++;
             else
                 chcount++;
        }
        cout << "\nWords=" << wdcount << endl
                 << "Letters=" << (chcount-1) << endl;
        return 0;
     }
    Hope this helps. Steve
    Last edited by smitsky; 12-01-2004 at 08:14 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  2. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 07:49 PM
  3. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 12:53 PM
  4. Some help for a newbie?
    By Ilmater in forum C++ Programming
    Replies: 23
    Last Post: 04-19-2004, 07:44 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM