first, format the code so that it uses proper indentation, and braces. That's the first step.
Comatose
Taboo Programmer
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
I guess the lessons of the previous thread haven't sunk in yet.
http://www.daniweb.com/forums/thread177531.html
Here's how to start
for(i=0;line[len]!=10;len++) <strong>{</strong>
<strong>}</strong>
Now press compile AND make sure it works.
Now add a few (not ALL) of your lines of code for the loop body, and try to compile it again.
Also learn to put { } on ALL your for loops, while loops, if statements and switch/case statements. It'll save you from spending ages finding yet another stupid bug because the code did what you asked it to, not what you thought you wanted.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
I guess you want to count the number of vowels, consonants etc.
Good,
you are almost there, lets look what wrong you did. I am referring to your orignal code.
cin.getline(len i,80);
This is totally wrong,
the syntax is
cin.getline(cstring,MAXINPUT)
So, in your case it should have been
cin.getline(line,80);
Next:
len is uninitialized, i.e. it contains junk values. Initialize it using strlen(line). strlen() is a function (in string.h) which returns the length of the string.
strlen("hello")=5
Next problem is the loop condition:
for(i=0;line[len]!=10;len++)
i is not defined use k instead. Moreover the condition is wrong, it should be (kfor(k=0;k<len;k++)
I also encourage you to use isalpha() which tells you if a character is a Alphabet.
It seems that you are very new to the language. Never mind, everyone starts from one.
But always remember to use standard compilers. I understand your school force you to use a nonstandard compiler like TC++ but then too its never too late to switch. Read : http://cppdb.blogspot.com/2008/10/why-you-shouldnt-use-you-use-old-c.html
Also, I know I have not explained every bit of your code to you. But if you require explanation in detail, feel free to ask.
Thank U
siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
/*
The English consonants are:
b, c, d, f, g, h, j, k, l, m, n,
p, q, r, s, t, v, w, x, y, z.
The English vowels are: a, e, i, o, u.
The letter y can also act as a vowel.
http://members.fortunecity.com/leanora/engpinen.html
*/
/* Dependencies:
#include <iostream>
#include <iomanip>
#include <string>
*/
using namespace std;
const string
Digits("0123456789"),
Vowels("AEIOUaeiou"/* add national vowels */),
Spaces(" \t\r\n"),
Consonants(
"BCDFGHJKLMNPQRSTVWXYZ"
"bcdfghjklmnpqrstvwxyz"
/* add national consonants */
);
const string::size_type NotFound = string::npos;
int main()
{
string line;
cout << "Let\'s play with phonetics...\n\n";
while (
cout << "Please, type a sentence"
" or press Enter to quit:\n",
getline(cin,line) && line.length()
)
{ // Comment the next line if you wish...
cout << line << '\n'; // echo for <file
char c;
int vowels = 0,
consonants = 0,
whitespaces = 0,
digits = 0,
others = 0
;
for (size_t i = 0; i < line.length(); ++i) {
c = line[i];
if (Consonants.find(c) != NotFound)
++consonants;
else if (Vowels.find(c) != NotFound)
++vowels;
else if (Digits.find(c) != NotFound)
++digits;
else if (Spaces.find(c) != NotFound)
++whitespaces;
else
++others;
}
cout
<< setw(4) << consonants << " consonants\n"
<< setw(4) << vowels << " vowels\n"
<< setw(4) << digits << " digits\n"
<< setw(4) << whitespaces<< " whitespaces\n"
<< setw(4) << others << " others"
<< endl;
}
return 0;
}
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
On ArkM's post,
Please try not to give the full answer in your post.
siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140