Hi friends.. Last day at school my teacher gave the following code.. But it is not working.. 16 errors.. I am a beginner in C++ .I badly need your guidance a help in finding the errors... the for loops is all wrong.. To be frank I don't understand the working of this program.. please help me understand it,wont you?

here is the code

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char line[80];

int k,len,vow,cons,sp;
cout<<"Enter the string ";
cin.getline(len i,80);
for(i=0;line[len]!=10;len++)
{
if (line[k]!=" ")
if(line[k]=="A"||line[k]=="E"||line[k]=="I"||line[k]=="O"||line[k]=="U"||line[k]=="a"||line[k]=="e"||line[k]=="o"||line[k]=="u");
vow++;

else if(( line [k]>='A'&&line[k]<='Z')||(line[k]>='a'&&line[k]<='z'))
cons++;
else sp++;
{
cout<<"no of vowels = "<<vow;
cout<<"number of constants ="<<cons;
cout<<"No of special characters ="<<sp;
getch();
}

Recommended Answers

All 15 Replies

first, format the code so that it uses proper indentation, and braces. That's the first step.

Buddy.. the problem is that I don't understand the first part of the code,ie the for loop and len.. what is line k? please please help me...
then i will format it with braces and all

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++) [B]{[/B]
[B]}[/B]

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.

what is it supposed to do ,bro??

oh sorry i put up the post without refeshing my page,so didnt see that so many lesons have been put up for u already

Friends I cant thank you enough for your help and guidance.. Am working hard to become a C++ programmer

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char line[80];

int k,len,vow,cons,sp;
cout<<"Enter the string ";
cin.getline(len i,80);
for(i=0;line[len]!=10;len++)

{
if (line[k]!=" ")
if(line[k]=='A'||line[k]=='E'||line[k]=='I'||line[k]=='O'||line[k]=='U'||line[k]=='a'||line[k]=='e'||line[k]=='i'||line[k]=='o'||line[k]=='u');
vow++;

else if(( line [k]>='A'&&line[k]<='Z')||(line[k]>='a'&&line[k]<='z'))
cons++;
else sp++;
{
cout<<"no of vowels = "<<vow;
cout<<"number of constants ="<<cons;
cout<<"No of special characters ="<<sp;
getch();
}

I made some corrections.. now what is use of "i" ? Now there are 6 more errors.. It says that undefined symbol i ... Do reply buddies

take the code i give
u will understand urself

#include<iostream.h>
#include<conio.h>
#include<string.h>
int main()
{
char line[80];

int k,len,vow=0,cons=0,sp=0,i;
cout<<"Enter the string ";
gets(line);
for(k=0;line[k]!='\0';k++)
{
if (line[k]!=32 )//Ascii code for blank
{
      if(line[k]=='A'||line[k]=='E'||line[k]=='I'||line[k]=='O'||line[k]=='U'||line[k]=='a'||line[k]=='e'||line[k]=='o'||line[k]=='u')
      vow++;
      else if(( line [k]>='A'&&line[k]<='Z')||(line[k]>='a'&&line[k]<='z'))
      cons++;
       else sp++;
}
}
cout<<"no of vowels = "<<vow;
cout<<"number of constants ="<<cons;
cout<<"No of special characters ="<<sp;
getch();
}

the funcion gets does the job of taking the data in a line
i have not use cin.getline so i cant tell about that and the i in ur program is wrong it should be k as in my program

This code is working perfectly.. but what is wrong with code that I gave you..? cant u modify it so that it would work.. using getline and not gets

gets(line);
for(k=0;line[k]!='\0';k++)
{
if (line[k]!=32 )//Ascii code for blank
{
      if(line[k]=='A'

I dont understand this portion.. please kindly explain the working.. what exactly is "k" doing?

what is the difference between if (line[k]!=32 ) and if (line[k]!=" " )

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 (k<len). also, len++ is wrong, use k++
So the loop becomes

for(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

/*
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;
}

>I dont understand this portion.. please kindly explain the working.. what exactly is "k" doing?

"k" is a variable that gets incremented with each character in the line. Thus, line[k] is the current character in the loop.

>what is the difference between if (line[k]!=32 ) and if (line[k]!=" " )

No difference. ASCII 32 = space (I think :P). It would be line[k] != ' ', a single-quote denotes a char, double a C-string.

On ArkM's post,
Please try not to give the full answer in your post.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.