472,961 Members | 1,479 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,961 software developers and data experts.

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 13031
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jason Heyes | last post by:
This is a revised version of a post entitled "Class to support keywords". Please reply to this post instead of the old one. The following program repeatedly prompts the user for C++ keywords...
4
by: Lauren Wilson | last post by:
Using VBA code on a form, does anyone know the most effective way to count characters entered into a text box AS they are being entered and displaying the number of characters remaining to the...
1
by: meryline | last post by:
hi, iam new to c#, can any one tell me how to find the total number characters entered in a textbox. like in vb.net i find it using len(textbox.text) . iam trying to implement the same in c# sharp...
3
by: haelly | last post by:
Write a program that prompts the user to enter three different integer values.If the values are not different, the program prints a message"equal values" and terminates(hint: use the return...
1
by: haelly | last post by:
write a program that prompts the user to enter three different positive integer values.If the values are not different, the program prints a message"equal value" and terminates(hint:use the return...
5
by: Dean | last post by:
Hi, I have a table with non-unique identifiers. I need to take all the values with the same ID's and combine them into one field with a semicolon as a seperator. These values may exceed 255...
0
by: Koteswara Rao K | last post by:
HI, How to call Sql * Loader Program Without passing Database user name and PAssword
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.