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

reading xml faster than DB call?
I am trying to optimize a high traffic website, and I enabled a feature where there are three photos

Add code to enable passing of checkbox variable on login
Ok, I have a login page, but want to add a checkbox that when checked and successful login is made i

SQL Injection
In my attempts to protect my database from mySQL injection I have created another problem for myself

Form help: Syntax & Logic
Hello again all,

I'm working on a form and ran into a wall (again) and can't seem to think th

Entering Data Problem
Code: $result = mysql_query("SELECT * FROM flights");
while ($row = mysql_fetch_array($

Preventing SQL Injection
I have a question about SQL Injection, In some of our code we use this: view plaincopy to clipboa

Sql and php order list script problems
Hi im having a few problems with trying to get the coding right on a orders list ive done (or trying

Stopping page post back
I have few literal contriols in my page and a repeater too. But inspite of using update panel, postb

Display search result
Hi!

I have a SQL database with information about albums and track (music).

This is wh

upload image name with extension using php
hi frds..

<input id="file1" type="file" name="file[]" &a

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