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

PEAR Email Attachment w/ $gpg
I have no issues with this code if I take out the attachement section and include the info in the em

bind error during socket programming
i am kinda new to socket programming. wrote the below code for a server. but getting the bind error

Grabbing Values From an Array for Posting
I have a grid array that I am using with a form that when it hits the currently named test.php it lo

How to show more than 1 users with this code...
Hello,
i have a table that shows users only if I, as Administrator, want to be shown. But its sho

Material Issuing for receiving batch
Dear All experts in MM/ PP,

Material issuing from main stores to factory is currently usi

Which practice of iteration through containers is preferred
In the "real world" what kind of loop do most people use to iterate through a container like a vecto

question about stripslashes and real_escape_string
im cleaning up an old app that I wrote fixing some of the vulernabilities from attacks.

I hav

foreach iterator
Hi Guys
Does the foreach loop have an inbuilt iterator ?

Wrong Info sent Using $get in form
Hi i have this code that i tried to make to send some info from one page to aother with lots of ppls

getAlexaRank($url) function not working
I have made a function to get alexa rank
the site is here: http://mytestsite.rack111.com/1

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