I am working on a program that needs to request user to enter positive 2 numbers.
The following is what my code is currently:
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 61 62 63 64 65 66 67 68
|
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
void count_up (int a, int b=0);
void count_down (int a, int b=0);
void count_between (int a, int b);
void get_positiveNumbers(int& a, int& b);
int main()
{
int firstNum;
int secondNum;
char answer;
do
{
get_positiveNumbers(firstNum, secondNum);
count_up(firstNum);
count_down(firstNum);
count_between(firstNum, secondNum);
cout << "Would you like to try again?" <<endl;
cin>> answer;
}while (answer == 'y');
return0;
}
void get_positiveNumbers (int& a, int& b)
{
cout<< "Enter 2 numbers, please;" <<endl;
cin >>a;
cin >>b;
while (a<=0 ||b<=0 && ((int)a !=a ||(int)b != b))
{
cout<< "You did not enter 2 positive numbers, please try again:" << endl;
cin >>a;
cin >>b;
}
}
void count_up(int a, int b)
{
do
{
cout<< b << " ";
}while (b++ !=a);
cout>> endl;
}
void count_down(int a, int b)
{
do
{
cout<< a << " ";
}while (a-- !=b);
cout << endl;
}
void count_between (int a, int b)
{
if (a < b)
{
count_up(b, a)
}
else
{
count_down(a,b)
}
}
|
So, as it is everything is working as expected, except. when the user enters a positive number for the first number and a negative number for the second number. The program should respond "you did not enter 2 positive numbers". It runs the program as if two positive numbers were entered....
I know the problem is in the while loop somehow....
Also, I am supposed to somehow change the get_positiveNumbers function so that call for a function that gets the input as a string
interpret if the string is positive
then convert it to an integer and sends it back to the main function
I am totally lost on how to do this...
Any help is appreciated. Thank you!