Adding next values.

Hi everyone. Im a beggining programming student and am stuck with this program im writing. Basically, im asking for the user to enter a sequence of numbers one by one and then adding the totals at the end. If the numbers are less than 0, dont add them. I stuck on the ending where im adding the numbers. Any advice?

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
36
37
38
39
40
41
42
43
44
45
46
47
48
//Write a program that reads a collection of positive and negative numbers and multiplies
//only the positive integers. Loop exit should occur when three consecutive negative numbers
//are read.

#include <iostream>
#include <string>
#include<cmath>

using namespace std;

int main()
{
    int totalnum;
    int numbers;
    int totals=0;
    int countnum;
    
    
    countnum=0;
    
    cout<< "How many numbers do you have? "<<endl;
    cin>> totalnum;
    
    while(countnum<totalnum)
    {
        cout << "Please enter number: " <<endl;
        cin >> numbers;
        
        totals= totals+numbers;
        
        countnum++;
    }
    
    if(numbers>=0)
    {
        cout<< totals <<endl;
    }
    else
    {
        numbers = 0;
    }
    
    cout<< "The totals of your entered values are "<< totals<< endl";
    
    system("pause");
    return 0;
    
} 
if you are supposed to disregard numbers < 0, you should do so before adding them to the total.
Ok so i moved it. Now what happens is it doesnt add properly. For example if i put 2 numbers, first is 12 and second is 23, it says total is 23. here is the new format.

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
36
37
38
39
40
41
42
43
44
45
46
47
//Write a program that reads a collection of positive and negative numbers and multiplies
//only the positive integers. Loop exit should occur when three consecutive negative numbers
//are read.

#include <iostream>
#include <string>
#include<cmath>

using namespace std;

int main()
{
    int totalnum;
    int numbers;
    int totals=0;
    int countnum;
    
    
    countnum=0;
    
    cout<< "How many numbers do you have? "<<endl;
    cin>> totalnum;
    
    while(countnum<totalnum)
    {
        cout << "Please enter number: " <<endl;
        cin >> numbers;
        
        countnum++;
    }
    
    if(numbers>=0)
    {
        cout<< totals <<endl;
    }
    else
    {
        numbers = 0;
    }
    
    totals= totals+numbers;
    cout<< "The totals of your entered values are "<< totals<< endl;
    
    system("pause");
    return 0;
    
}
You're only saving the latest "numbers" that you have input. The 12 that you entered before the 23 is no longer in any memory. Change the bounds of your while loop (which, by the way, would make more sense as a for loop) to encompass changing the total as well.
Last edited on
You went about it backwards, think more like this:

1
2
3
get number
if number < 0 then number = 0
total += number
Why do you have strings and math included?

1
2
3
4
5
6
7
8
9
10
    if(numbers>=0)
    {
        cout<< totals <<endl;
    }
    else
    {
        numbers = 0;
    }
    
    totals= totals+numbers;
these should be in the while loop. Though I would simply do
1
2
3
4
if(numbers > 0)
{
    totals += numbers;
}
so something like:

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
#include <iostream>

int main()
{
    int numbersToInput = 0;
    std::cout << "How many numbers would you like to input? ";
    std::cin >> numbersToInput;

    int sum = 0;
    int inputNumber = 0;
    int numbersInputted = 0;
    while(numbersInputted < numbersToInput)
    {
        std::cout << "Please enter a positive number: ";
        std::cin >> inputNumber;
        if(inputNumber > 0) //positive
        {
            sum += inputNumber;
            ++numbersInputted;
        }
   }

    std::cout << "The sum of the numbers is: " << sum << std::endl;

    return 0;
}


By the way what you said you need to do is different than what the comment says. So are you getting the sum or reading in numbers and multiplying them together like comment says? Also, you need to keep into consideration if 3 negative numbers are inputted in a row. So something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int consecutiveNegatives = 0;

//in the while loop while loop if it is positive (or zero I guess) set consecutivesNegatives to 0 

//then after use an elseif like this
else if(inputNumber < 0) //negative
{
    ++consecutiveNegatives;
}

//then
if(consecutiveNegatives == 3) //3 in a row
{
    numbersInputted = numbersToInput; //or you can use: break;
}


So the overall product would be something like:

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
36
37
38
39
#include <iostream>

int main()
{
    int numbersToInput = 0;
    std::cout << "How many numbers would you like to input? ";
    std::cin >> numbersToInput;

    int sum = 0;
    int inputNumber = 0;
    int numbersInputted = 0;
    int consecutiveNegatives = 0;
    while(numbersInputted < numbersToInput)
    {
        std::cout << "Please enter a positive number: ";
        std::cin >> inputNumber;
        if(inputNumber > 0) //positive
        {
            sum += inputNumber;
            ++numbersInputted;
            consecutiveNegatives = 0;
        }
        else if(!inputNumber) //zero is not positive nor negative (same as == 0)
        {
            consecutiveNegatives = 0;
        }
        else //negative
        {
            if(++consecutiveNegatives == 3) //increment then check if there are 3 in a row
            {
                 break; //or: numbersInputted = numbersToInput
            }
        }
   }

    std::cout << "The sum of the numbers is: " << sum << std::endl;

    return 0;
}
Last edited on
Ok i see my mistakes now. Thanks guys i appreciate it. It nice to know i have a place were there are so many people willing to help a beginner. Much appreciated!
Topic archived. No new replies allowed.