Using loop to count number of entries


Posted on 16th Feb 2014 07:03 pm by admin

I'm writing a program that must ask user to type in numbers. After each entry, the program has to report the cumulative sum of the entries to date. The program should terminate when user enters 0.

The code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include #include int main() { using namespace std; char number; int count = 0; cout << "Enter a number: "; cin.get(number); while (number != '0') { ++count; cin.get(number); }; cout << "Total of " << count << " inputs.n"; system("pause"); // I'm aware that this way is very bad return 0; }

The problem is that if I enter 0, the program will say there's 0 input in total(correct).
If I type in 1 and the next number is 0, program will say there's 2 entries in total.
And if I type in 11 and 0, I get 3 entries in total.
But even more strange is that if you type 111 then 11 then 0 you get total of 7 entries.

No, the program must not show me the sum of all numbers entered, just how many entries you made, untill you typ in 0.

No comments posted yet

Your Answer:

Login to answer
300 Like 45 Dislike
Previous forums Next forums
Other forums

Tree Menu
Hi guys,

Can anyone help me making a tree navigation system? I have a site where I'm allowin

HTML Form Server Side Validation
Hi, Im new here, im currently doing a website for a friend, and I have designed using snippets from

classic dynpro : hide area and auto adjust hights
Hi,

I want to develop a similar user interface as is used in standard transaction MIGO.

iMatch stored value with the current value in a loop
Hello,

I have a MySQL db were I store articles in.

I have a form to fill these article

cyrillic string conversion question
Hello,

First time here...

I would like to know if there is a way to convert a

serializing objects - loses methods - the point being?
Hi

I know that serializing an object will lose the methods. But whats the point in that? What

Include ("Absolute Path") of a file?
I'm not sure what I'm doing wrong. I am writing my php code in the same directory of the file that

Forms Authentication and Refresh at Login page
Hello, When I try to press the Login button in my webapplication at my login.aspx page nothing h

Warning message
I've put a website that I was doing live and I'm getting this warning message when I try to add a ne

Filename like the user name
I am creating a form and storing the values in file. Could anyone please tell me how to write a code

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash