I am a newbie and this is my homework for school. The program is suppose to print from 1 to 5 (not 5 to 1) and so far, i have only been able to make the program print from 5 to 1, any assistance with this will be appreciate. the program should only use while loop
#include <iostream>
#include <stdlib.h>
using namespace std;
int main (int argc, char *argv[])
{
//if statement to check only 2 argument can be passed
if (argc != 2)
{
cout << "ERROR!" << endl;
}
else
{
if (atoi(argv[1]) < 1) //if statement to check negative numbers
{
cout << "ERROR!"<< endl;
}
int temp = atoi(argv[1]); //convert the number inthe character argument to integer
int sum = 0; //variable declaration to find the sum passed in the while loop
//the while loop is used to print out the numbers entered in descending order
while (temp > 0)
{
cout << temp << endl; //output numbers in the iteration
sum = sum + temp; //sums the number of iteration
temp --; //counter, used to stop the while loop to avois an infinity loop
}
cout << "Sum is " << sum << endl;
}
return 0;
}