470,572 Members | 2,421 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,572 developers. It's quick & easy.

Program to count characters entered by user

Complete homework Question is this:

Create a program that uses a do-while loop to count the number of char.
(not including whitespace) entered by the user.The count should end
when it encounters a # character in the input.

First of all i want to tell u that I am a bigenner.Just started C++ for
a month.

And that is what i have tried:

#include<iostream.h>
void main()
{
char character;
int i=1;
cout<<"enter the character "<<endl;
do
{
cin>>character;

i++;

}while(character!='#');
cout<<"the no. of characters you have entered are "<<i<<endl;
}
Output:
it is 80% right result but it counts two characters more than I have
written.....
hints welcomed.

Oct 18 '06 #1
5 12860
fa***********@gmail.com wrote:
Complete homework Question is this:

Create a program that uses a do-while loop to count the number of char.
(not including whitespace) entered by the user.The count should end
when it encounters a # character in the input.

First of all i want to tell u that I am a bigenner.Just started C++ for
a month.

And that is what i have tried:

#include<iostream.h>
void main()
{
char character;
int i=1;
cout<<"enter the character "<<endl;
do
{
cin>>character;

i++;

}while(character!='#');
cout<<"the no. of characters you have entered are "<<i<<endl;
}
Output:
it is 80% right result but it counts two characters more than I have
written.....
hints welcomed.
Check how and when i is incremented and look at the logic you have there
and compare it to what it should be.

Sincerely,

Peter Jansson
http://www.p-jansson.com/
http://www.jansson.net/
Oct 18 '06 #2
Start i at -1. You're counting the # in your character count, and
you're also putting an extra char in there by starting i at 1 instead
of 0.

On Oct 18, 1:28 pm, "faizankhan...@gmail.com" <faizankhan...@gmail.com>
wrote:
Complete homework Question is this:

Create a program that uses a do-while loop to count the number of char.
(not including whitespace) entered by the user.The count should end
when it encounters a # character in the input.

First of all i want to tell u that I am a bigenner.Just started C++ for
a month.

And that is what i have tried:

#include<iostream.h>
void main()
{
char character;
int i=1;
cout<<"enter the character "<<endl;
do
{

cin>>character;

i++;

}while(character!='#');
cout<<"the no. of characters you have entered are "<<i<<endl;

}Output:
it is 80% right result but it counts two characters more than I have
written.....
hints welcomed.
Oct 18 '06 #3
fa***********@gmail.com wrote:
Complete homework Question is this:

Create a program that uses a do-while loop to count the number of char.
(not including whitespace) entered by the user.The count should end
when it encounters a # character in the input.

First of all i want to tell u that I am a bigenner.Just started C++ for
a month.

And that is what i have tried:

#include<iostream.h>
Non-standard header. Use #include <iostream>
void main()
int main()

The return type from main is *ALWAY* int. Never anything else.
{
char character;
int i=1;
cout<<"enter the character "<<endl;
do
{
cin>>character;

i++;

}while(character!='#');
cout<<"the no. of characters you have entered are "<<i<<endl;
}
Output:
it is 80% right result but it counts two characters more than I have
written.....
hints welcomed.
Oct 18 '06 #4
fa***********@gmail.com wrote:
And that is what i have tried:

#include<iostream.h>
This is not a standard header.
void main()
This is not an allowed definition of main.
{
char character;
int i=1;
Why 1? You haven't entered anything yet.
cout<<"enter the character "<<endl;
do
{
cin>>character;

i++;
The first time through i is 2 at this point.

>
}while(character!='#');
Note that te test is after the increment, so you count the # as well.
cout<<"the no. of characters you have entered are "<<i<<endl;
}
Output:
it is 80% right result but it counts two characters more than I have
written.....
hints welcomed.
Oct 18 '06 #5
<fa***********@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
Complete homework Question is this:

Create a program that uses a do-while loop to count the number of char.
(not including whitespace) entered by the user.The count should end
when it encounters a # character in the input.

First of all i want to tell u that I am a bigenner.Just started C++ for
a month.

And that is what i have tried:

#include<iostream.h>
void main()
{
char character;
int i=1;
You haven't read any characters yet, so before the loop you have read 0
characters, not 1.
cout<<"enter the character "<<endl;
do
{
cin>>character;

i++;
Do you want to increment i all the time? What if it's a white space (space
or tab)? What if it's #? Maybe you should increment it in an if staement.
>
}while(character!='#');
cout<<"the no. of characters you have entered are "<<i<<endl;
}
Output:
it is 80% right result but it counts two characters more than I have
written.....
hints welcomed.
Plus white spaces will be counted too in your version.
Oct 18 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by livre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.