Program to count characters entered by user

F

faizankhan666

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.
 
P

Peter Jansson

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/
 
C

Corylation

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.
 
R

red floyd

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 said:
void main()
int main()

The return type from main is *ALWAY* int. Never anything else.
 
R

Ron Natalie

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.
 
J

Jim Langston

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top