PDA

View Full Version : C++ Help Needed



Moonbat
09-19-2006, 11:05 PM
I am making a simple program that takes a sentence(s) and counts up all of the letters in the given sentence(s). Now, I was wondering if anyone could write some source code because of my problem

I want an easier way to write a way to get letters besides using a huge if-then-else loop, such as "If char X == a or A, then add * to variable L, else if char X == b or B then..." so on and so forth (Yes, i didn't write that using C++ coding, just giving you the idea of what I would otherwise have to do)

If anyone could help, it would be appreciated:D

tocksarcle
09-20-2006, 12:44 AM
I am making a simple program that takes a sentence(s) and counts up all of the letters in the given sentence(s). Now, I was wondering if anyone could write some source code because of my problem

I want an easier way to write a way to get letters besides using a huge if-then-else loop, such as "If char X == a or A, then add * to variable L, else if char X == b or B then..." so on and so forth (Yes, i didn't write that using C++ coding, just giving you the idea of what I would otherwise have to do)


Try this:


char sentence[256];
cin.getline(sentence,256);
int length=strlen(sentence);
cout<<length;


The function strlen() is used to count the amount of characters in a string. This code will print the amount of characters in the sentence. Hopefully this will help.

Ezekiel
09-20-2006, 01:23 PM
Whenever manipulating strings in c++, you should check this reference:

http://www.cplusplus.com/ref/cstring/

strcat(), strcpy(), strcmp(), and strlen() are vital components of all c++ programs, so learn how to use them and you'll find things a lot easier than trying to re-invent the wheel.

Moonbat
09-20-2006, 05:35 PM
I know about strlen, but I want only to get letters, not spaces or punctuation. Is there any easier way to get only letters?

tocksarcle
09-20-2006, 06:17 PM
I know about strlen, but I want only to get letters, not spaces or punctuation. Is there any easier way to get only letters?




char i[256];
cin.getline(i,256);
int len=strlen(i);
for(int m=0;m<256;m++){
if(i[m]==" " || i[m]=="." || i[m]=="?" || i[m]=="!" || i[m]==","){
len--;
}else{
continue;
}

}
cout<<len;


This is one way to do it, though there might be a better way.

Moonbat
09-20-2006, 09:26 PM
It worked just fine for me.
'Preciate it tocksarcle:D

tocksarcle
09-20-2006, 10:05 PM
It worked just fine for me.
'Preciate it tocksarcle:D

No problem:)