473,546 Members | 2,249 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<iostre am.h>
void main()
{
char character;
int i=1;
cout<<"enter the character "<<endl;
do
{
cin>>character;

i++;

}while(characte r!='#');
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 13107
fa***********@g mail.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<iostre am.h>
void main()
{
char character;
int i=1;
cout<<"enter the character "<<endl;
do
{
cin>>character;

i++;

}while(characte r!='#');
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<iostre am.h>
void main()
{
char character;
int i=1;
cout<<"enter the character "<<endl;
do
{

cin>>character;

i++;

}while(characte r!='#');
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***********@g mail.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<iostre am.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(characte r!='#');
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***********@g mail.com wrote:
And that is what i have tried:

#include<iostre am.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(characte r!='#');
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.goog legroups.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<iostre am.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(characte r!='#');
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
1762
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 until 'explicit' is entered. If the user fails to enter a valid keyword, the program terminates. #include <iostream> #include "KeyWord.h"
4
32923
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 user? Thanks
1
2607
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 as well. but don't know the exact keyword. any help would be appreciated. Thanks Regards Meryline
3
3642
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 statement).If either of the values is negative,the progra prints"Negative input" and terminates.Otherwie, it prints the values that the user enters. After...
1
3767
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 statement).If eithere of the value is negative, the program prints "negative input" and terminates.Otherwise it prints the value the user entered....
5
8942
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 characters. I then need to count the values in the cell and see if it adds up to 240 or more and then shorten that field by cutting off the excess and...
0
1310
by: Koteswara Rao K | last post by:
HI, How to call Sql * Loader Program Without passing Database user name and PAssword
0
7435
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7694
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7947
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7461
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6026
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1921
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.