Having troubles with 0 and multiplications in while loop

Hello, I need some help in V==3 and V==5 lines below which you can see in my code.
What I need to do in "V==3", is to multiply every number of "a", but i dont know how to. I cant do b*=a, because b=0. To stop while loop, I simply need to enter, that "a=0". I need to skip that number in multiplication somehow as well. Similiar thing about "V==5". I need to skip 0 somehow, and allow it to show another lowest number. I would greatly appreciate some help here :).

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
  #include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
    int v,b=0,a,c=0;
    cout << "V="; cin >> v;
    while (a!=0)
    {
        cout << "A="; cin >> a;
        {if(v==1) b+=a;}
        {if(v==2) b-=a;}
        {if(v==3) b*=a;;}
        {if(v==4 && c==0) b=a;
        else if(a>b) b=a;}
        {if(v==5 && c==0) b=a;
        else if(a<b) b=a;}
        c++;
    }
    cout << "B=" << b << endl;
    return 0;
}

The quickest hackety-hack solution for stopping the loop when a is 0 would be to test the value of a immediately after it is captured and break out of the loop if it is equal to 0:

1
2
3
4
5
6
        cout << "A="; cin >> a;
        // If a is zero then break out of this loop immediately
        if( a == 0 )
            break;
        // otherwise keep on processing
        {if(v==1) b+=a;}


I may have misunderstood the other parts of your question, but I would have thought that if b is 0, then doing b*=a won't cause any problems?
1
2
3
4
5
6
switch(operation){
case SUM: result = sumatoria(); break;
case FACT: result = factorial(); break;
case MIN: result = minimum(); break;
}
std::cout << result << '\n';
Last edited on
ne555 ty for a reply, but I still havent learned those things, no idea what they do ;p Norm Gunderson, if b is 0, wont it always be zero then after multiplications ? And how can i skip number 0 in "V==3" and "V==5"? Sorry for my poor english btw :/
Numbers im supposed to write are 15;20;70;-20 and 0, but its for exiting loop.
Last edited on
Sorry, I didn't read your code properly - ne555's suggestion is a better way of structuring it to do what you want. However, if those techniques are currently beyond your learning, your original code is close - just a little confused with the scoping and use of your if statements (and needs some more comments!):

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
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
// #include <iomanip> // Don't need this
// #include <cmath> // Don't need this
using namespace std;
int main()
{
    int v,b=0,a,c=0;
    cout << "V="; cin >> v;
    do // Change this to a do while loop
    {
        cout << "A="; cin >> a;
        // Only process if a != 0
        if(a!=0)
        {
           // Add a
           if(v==1)
           {
               b+=a;
           }
           // Subtract a
           else if(v==2)
           {
               b-=a;
           }
           // Multiply by a
           else if(v==3)
           {
               // If you are on the first iteration of the loop, need to initialise b to something
               if(c==0)
                   b=a; // ???
               // Otherwise, do the multiplication
               else
                   b*=a;
           }
           // Find maximum of input a values
           else if(v==4)
           {
               // If you are on the first iteration of the loop, initialise b to a
               if(c==0)
                   b=a;
               // Otherwise set b = maximum of a and b
               else if(a>b)
                   b=a;
           }
           // Find minimum of input a values
           else if(v==5)
           {
               // If you are on the first iteration of the loop, initialise b to a
               if(c==0)
                   b=a;
               // Otherwise set b = minimum of a and b
               else if(a<b)
                   b=a;
           }
           c++;
        }
    } while (a!=0);
    cout << "B=" << b << endl;
    return 0;
}


See if that works.
Last edited on
Ty for your answer. V==3 should seem to work, but I cant get on my computer right now. But how about V==5 ? How do I skip 0 to look for other lowest number ?
You could always initialize `b' to the first number that you read.
1
2
3
4
5
6
7
std::cin>>operation;
std::cin>>number;
result = number;

while(std::cin>>number and number not_eq 0){
   //perform the operations
}
Topic archived. No new replies allowed.