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 :).
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?
switch(operation){
case SUM: result = sumatoria(); break;
case FACT: result = factorial(); break;
case MIN: result = minimum(); break;
}
std::cout << result << '\n';
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.
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!):
#include <iostream>
// #include <iomanip> // Don't need this
// #include <cmath> // Don't need this
usingnamespace 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
elseif(v==2)
{
b-=a;
}
// Multiply by a
elseif(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
elseif(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
elseif(a>b)
b=a;
}
// Find minimum of input a values
elseif(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
elseif(a<b)
b=a;
}
c++;
}
} while (a!=0);
cout << "B=" << b << endl;
return 0;
}
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 ?