Help with simple counter program

Hello guys, I must admit that I have no idea how to complete this task at hand. I am stuck taking this class even though it is not my major and I am completely lost. I would appreciate any and all help but I understand if you cannot. I am an image designer and maybe I can return the favor.

What I need :
Simple c++ program to accomplish the following

- Ask user to enter numbers ( Entering 0 stops the collection displays result

The output displays ::: The sum of numbers entered
The output displays ::: The number of numbers entered
The output displays ::: Whether the total is GREATER or LESSer than 100


Example :

(System) Please Enter A Number - Entering 0 will stop the collection -
(User) : 9
(User) : 2
(User) : 1
(User) : 0

The sum of the numbers entered is : 11
The number of inputs entered are : 3
The value is : Less than 100

The only tricky part i think would be getting the counter to not include 0 in the "number of numbers entered"

closed account (o3hC5Di1)
Hi there,

Let's break it down:

Ask user to enter numbers ( Entering 0 stops the collection displays result


You will need to keep asking numbers, i.e. repeat a certain task. This calls for a loop. Within that loop, we can ask the user for input and check whether the number was indeed a 0. Let's put this in code:

1
2
3
4
5
6
7
8
9
10
int input=1; //temporary variable to hold user input

while (input != 0) //while user input is not equal to zero
{
    std::cout << "Please enter a number: "; //Ask number to the user
    std::cin >> input; //read users number

    if (input ==0) //if the user inputted a zero, stop the loop.
        break;
}


Now, we also need to keep a sum as well as a counter, which is no more work than adding two variables:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int input=1; //temporary variable to hold user input
int sum=0; //extra variables
counter=1;

while (input != 0) //while user input is not equal to zero
{
    std::cout << "Please enter a number: "; //Ask number to the user
    std::cin >> input; //read users number

    if (input ==0) //if the user inputted a zero, stop the loop.
        break;

    sum += input; //increase sum by input
    counter++; //increase counter by one
}


All that would be left to do is print these results to the user. I hope this helps, please do let us know if you require any further assistance.

All the best,
NwN
Thank you so much, when I get home ill try and piece it together with the my current code. I appreciate the help wish I could return the favor.
I am confused, overlooking stuff I dont know if that is the right code.

I don't understand how I can output this from that.
Is there code in there that would allow me to call this to the output? '
"The total sum is : LESS than 100 / Greater than 100"
Ok so this is what I have so far and im encountering two issues.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Kevin Johnson -- Week 3 Checkpoint
//

#include "stdafx.h"
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
	return 0;
}

int main()
{

using std::cin;
using std::cout;
using std::endl;


int input=1; //temporary variable to hold user input
int sum=0; //extra variables
int counter=1;

while (input != 0) //while user input is not equal to zero
{
    std::cout << "Please enter a number: "; //Ask number to the user
    std::cin >> input; //read users number

    if (input ==0) //if the user inputted a zero, stop the loop.
        break;

    sum += input; //increase sum by input
    counter++; //increase counter by one
}
}


The issues im having are the following

1) how do i make it so when 0 is entered it triggers the program to stop asking for numbers and instead displays the following

( sum of the numbers entered)
(Number of numbers entered)

and the second problem

2) I don't see any code or any way of outputting whether the sum is greater or less than 100


So i need the program to output the following


Sum of numbers entered :
Number of numbers entered :
Is the sum greater or less than 100 :
Last edited on
int main()
{

using std::cin;
using std::cout;
using std::endl;


int input=1; //temporary variable to hold user input
int sum=0; //extra variables
int counter=1;

while (input != 0) //while user input is not equal to zero
{
std::cout << "Please enter a number: "; //Ask number to the user
std::cin >> input; //read users number

if (input == 0) //if the user inputted a zero, stop the loop.
{
break;
}
else if (input != 0)
{
sum += input; //increase sum by input
counter++; //increase counter by one
}

}
counter = counter - 1 ; //Easiest fix to the 0 counting in counter that I could come up with this late
std::cout << "Sum of numbers entered: " << sum << endl ;
std::cout << "Number of numbers entered: " << counter << endl ;

if( sum < 100 )
{
std::cout << "The sum is less than 100" ;
}
else if( sum > 100 )
{
std::cout << "The sum is greater than 100" ;
}
else if( sum == 100 )
{
std::cout << "The sum is equal to 100" ;
}

}

Here's my best, hope this helps.
closed account (Dy7SLyTq)
a) use code tags
b) no point in doing using ... if your just going to do std::command
c) be careful with what your writing. a lot was either redundant or un neccesary

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using std::cin;
using std::cout;
using std::endl;

int main(int argc, char *argv[])
{
	int Input, Sum = 0, Counter = 0;

	do
	{
		cout<<"Please enter a number: ";
		cin>> Input;
	
		if(Input != 0)
		{
			Sum += Input;
			Counter++;
		}
	}while(Input != 0);

	cout<<"Sum of numbers entered: "<< Sum << endl;
	cout<<"Number of numbers entered: "<< counter << endl;
	cout<<"The sum is ";

	if     (Sum < 100)  cout<<"less than 100";
	else if(Sum > 100)  cout<<"greater than 100";
	else if(Sum == 100) cout<<"equal to 100";
}
Last edited on
closed account (o3hC5Di1)
Hi there,

Since you PM'd me I thought I'd follow up on this question - I think DTSCode pretty much covered it though.
Please do let us know if you require any further help.

All the best,
NwN
Topic archived. No new replies allowed.