-
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;
}
-
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.
-
Code:
#include <cstring>
...
string s;
Wrong header for using the string container... you want to be using <string>
Code:
#include <string>
...
std::string s;
-
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
-
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.
-
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:
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