why doesn't my decrement code work?

#include <cstdlib>
#include <iostream>
#include <string>
#include <stdio.h>
#include <windows.h>
#include <conio.h>
#include <exception>
#include <process.h>

using namespace std;

int main(int argc, char *argv[])
{

int v1 = atoi(argv[1]);
int v2= atoi(argv[2]);
int i ;
int input =100;
int a1 = input+v1;
int a2 = input + ( v1 * v2 );
int a3 = input-v1;
int a4 = input - ( v1 * v2 );

______________________________________
if(v1<0)
{
for ( i=a3; i>=a4 ; i-= v1 )
{
cout << i << "\n"; <---- what's wrong with this
when v1 is a negative no.
it's suppose decrement
}}
___________________________________________

for ( i=a1 ; i<=a2 ; i+= v1 )
{
cout << i << "\n";
}
system("PAUSE");
return 0;
}
If v1 is negative, then i -= v1 will add to i, since subtracting a negative number is the same as adding the positive version.
I changed it still nothing
When v1 is negative then it can be that int a4 = input - ( v1 * v2 ); is positive and greater than int a3 = input-v1;. For example let assume that v1 = -1 and v2 = 2. In this case a4 = 102 and a3 = 101. So the loop will be never executed


1
2
3
4
5
if(v1<0)
 {
 for ( i=a3; i>=a4 ; i-= v1 ) 
{
 cout << i << "\n"; 
 when v1 is a negative no. 
it's suppose decrement
 }}
Last edited on
Could you suggest a way to decrement it because it is still bait confusing too me.
I changed to

int a3 = input+v1;
int a4 = input +( v1 * v2 );

if i put 2, 4
the answers only comes out as 98 instead of 100, 98 , 96, 94 could any suggest a solution probably something wrong with by for loop
Last edited on
Let check your code

The initial value of i is set by a1 and equal to 102.

int a1 = input+v1;


The ccntrol condition is i <= a2, where a2 is equal to 108 (int a2 = input + ( v1 * v2 );) . So the loop

for ( i=a1 ; i<=a2 ; i+= v1 )
{
cout << i << "\n";
}


shall produce 102, 104, 106, 108

The first loop will not executed because v1 is greater than zero.
Last edited on
I figured it out !! woot woot thanks for help
I over complicated it all i needed was a1 a2 and flip the sign
Last edited on
Topic archived. No new replies allowed.