How to create series of input numbers?

Pages: 12
Hello everyone, i'm been practice C++ for a while just a new hobby in technology.

i start with mathematics with numbers and some cout, cin function

in other program like excel i can enter series of number and after that we can:

1. Sum them up
2. Divide them, multiple, list the highest number or smallest or whatever with number since they have some build-in F(x) and its very to use like writing in real life.

in C++ i've been experienced little much more trouble

#include <iostream>
void main()

well after this part i can declare a lot of variables but they are not so practical since the amount of number is fix (eg. 10 is 10 and no more). What i can do if i want to enter as many as i can and after that the mathematics will be later based these input numbers.

Any help will be so appreciated
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main()
{
     int num = 0;
     double sum = 0;
     cout << "enter numbers to be sum: ";
     cin >> num;
     
     while(num != 0)   //your input will not end unless it is zero
    {
        sum += num;
        cin >> num;
     }
    
     cout << "sum of your numbers is: "<< sum << endl;
     return 0;
}


i wrote a simple code example here...
i think this code will give you an ideal...
hope it helps...
also you can't void main
I'm trying to understand your code, i will explain it myselft, could you correct me?

i'm not understand the meaning of 13. sum += num; and 17.

could you break 13. and 17. in more detail
i can help if thats okay
13) += is a shorter way of saying var1 = var1 + value2
17) what is in "" is the text that is printed and the word sum prints whatever value is currently stored in it
quick ex of line 13:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

using namespace std;

int main()
{
int var1 = 5, var2 = 2;
var2 += var1;
//pretend that var2 doesn't have a new value yet
var2 = var2 + var1;
cout<< var2;

both make var2 = 7
aramil is right
in line 13, the sum+= num means [sum = sum + num]
line 17, it print out the result of the total sum..
Well i tried this program and it does not work at all, after enter first number they just stuck
my program? no it wont work because it is just meant to show examples i wrote it horribly

#include <iostream>
using namespace std;

int main()
{
     int num = 0;
     double sum = 0;
     cout << "enter numbers to be sum: ";
     cin >> num;
     
     while(num != 0)   //your input will not end unless it is zero
    {
        sum += num;
        cin >> num;
     }
    
     cout << "sum of your numbers is: "<< sum << endl;
     
    system("pause");
     return 0;
}


i'm using dev c++, and it seem doesn't have any errors.... you must press space or enter after each number you enter...
That makes me kind of confuse, then how many variable i need to write in total just two right?
you can write as many as you want, you just have to end the input by enter 0.

example:
your input : 1 2 3 4 5 0 (zero mean end the while loops, so no more input)

sum = 1 + 2 + 3 + 4 + 5

output : 15
Last edited on
i use microsoft visual studio, does it have any different?

i understand you code now, but it just does not work, cant understand why?
Last edited on
it shouldnt unless you have to include a special header file
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
// Created by Tuan Minh Pham s0222607 CQU University course COIT0222607 assignment 1
// The program was created in order to caculate series of any numbers and displays these output
// 1. The number of values entered
// 2. Sum of all positive numbers
// 3. Average of first and last numbers
// 4. 70% of smallest positive number
// 5. Largest number entered
// 6. The number of times the largest number is entered
// 7. The two numbers in descending order

#include <iostream>
using namespace std;

int main()
{
     int num = 0;
     int sum = 0;
     cout << "enter numbers to be sum: ";
     cin >> num;
     
     while(num != 0)   //your input will not end unless it is zero
    {
        sum += num;
        cin >> num;
     }
    
     cout << "sum of your numbers is: "<< sum << endl;
     
    system("pause");
     return 0;
}


This is my full picture now

Still the same........................ after remove line 29
Last edited on
can you try remove line 29...
i'm not sure about it, because i'm not familiar with visual studio... aramil can help?
Last edited on
anyone can help? i'm not really sure why vs doesn't works, but you can try it on dev c++ if you want....
Last edited on
no sorry i can't help i have xcode i just remember reading somewhere that certain ides need special header files.
Grax, in the whole picture is my detail of my assignment, need to pull some number and calculate them. Can you give me some other advices for that stuff, going nowhere for past few days and even not pass the first part which is create input
you'll need to include stdlib.h if you're planning on using system("pause") or you can try to get rid of it instead.

I think visual studio actually halts the program automatically at the end if I remember it right, but if it doesn't you need to add the system("pause") to halt program execution before closing so you may view the results.

It's not really a good way of doing things though since it's not portable, but maybe that's what they're teaching you in school. If it is, just do what they expect you to do. :P
Last edited on
Pages: 12