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:

Did you know?Explore Trending and Topic pages for more stories like this.
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

Problem assigning value to variable in "IF" function
Does this script makes sense? I am trying to take the value that is set to "authenticat" a

How do I get the row number from from an sql table query
Hi all

I have a table that I query and it returns a number of rows.
mysql_num_rows($query)

textfield unchanged
hi,
i want my form to know whether its textbox is unchange or not.

if($textbox == 'unchang

pointer 102 question
I read a book
1
2
3
4
5
6
7
8
9
10
11
12
int main() {

Online Event Ticket Sales
Has anyone wrote a script for online tickets sales?

I have been googling and found lots of th

Check Date/Time in PHP
What I need the code to check is that $reQuest is 24 hours in the past then return a time based on h

Simple Variable Question
Hi everyone.... again,

I am really getting into php still. Learning more every day. I love it

Simple PHP/Java Help..
Hi,
I'm trying to build a little program to do with calculating postage. Unfortunately i haven't

no idea what to do...
I'm fed up. I had issues with code that I have working on a different site that didn't work on my cu

Struct/union and scope problem!
HI all , I have
Code: in header.h typedef struct Node Link; /* ---------

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