Hello aimen112,
I will divide my explanation into 3 parts.
1 part:the Positive section
as you did tell your users to input POSITIVE number and you did add instructions there are users
that may "check the bounds' of your program and input NEGATIVE number.
for that you have to make a loop that insure you that the numbers the user entered are POSITIVE.
for this you can use Do While loop;
if you dont know what it is a Do While loop is a loop that runs the code inside the Do brackets
and then check the while condition if false it will repeat until the while condition will turn out true
*We use this method so you wont get any ERR for the while condition cause the value of first/second are not initialized.
1 2 3 4 5 6 7 8 9 10 11 12
|
do{
cout << "Enter the FIRST positive integer: ";
cin >> first;
if (first < 0)
cout << "*Number recieved is NEGATIVE please enter POSITIVE" << endl;
} while (first < 0);
do {
cout << "Enter the FIRST positive integer: ";
cin >> second;
if (second < 0)
cout << "*Number recieved is NEGATIVE please enter POSITIVE";
} while (second < 0);
|
Part 2: checking small or large:
If you put in the instructions that you first print SMALL to LARGE you have to stand behind your statement cause that what you told your user.
for that you have to make IF that will check who is bigger and who is smaller.
1 2 3 4 5 6 7 8
|
if (first < second) {
smaller = first;
larger = second;
}
else {
smaller = second;
larger = first;
}
|
in this way you check what is the large variable and what is the small one.
if you dont want to do it u can delete those instructions and change the code. (i wrote my answer to you as you wrote your first instructions)
another thing is in this way that you save the small and large values in new variable you are able to save the original user input to thier original variables ("first","second") so you may use them in the future if u develop this code to print more than what you intented to.
Part 3:Priting loop
I think you didnt quite understood Handy Andy explanation about the for loop.
in your code the for loop will continue for ever because one simple principal the for loop goes like this
for(Declaring variable with value; putting condition for the variable that we declared; changing the variable that we declared on so the for loop will have end point)
*Example: for(int i = 0; i < 5; i++)
in this example each time the for loop runs "I" will increase by 1 until he will be larger than 5.
in your code each time the for loop runs the "First" variable isnt changing so he will always be LOWER than "Second"
to solve it you can use 2 ways:
First way: using temporary variable to save first or second value than change it to incease or decrease his value to make for loop ends.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
displayCounter = smaller; // using temporary variable that we can change but still keep the user input original value
cout << "Displaying: small to large" << endl;
for (int i = 0; i <= (larger-smaller); i++) // for loop that runs aslong i smaller or equal to difference between the large or small inputs
{
cout << displayCounter << endl;
displayCounter++; // changing the temporary number as we like to display on screen
}
displayCounter = larger; // ajusting temporary number to our use in this case to display large to small without changing original user input
cout << "Displaying: large to small" << endl;
for (int i = 0; i <= (larger-smaller); i++) //we used same for condition but this time we decreasing our temporary variable instread increasing it
{
cout << displayCounter << endl;
displayCounter--;
}
|
i added some comments inside the code so you will have less questions and move understanding.
Second way: using the "I" variable that we have to declare and use anyway for the FOR loop to display our difference between the numbers.
1 2 3 4 5 6 7 8
|
cout << "Displaying: Small to Large" << endl;
for (int i = 0; i <= (larger - smaller); i++) {
cout << smaller + i << endl;
}
cout << "Displaying: Large to Small" << endl;
for (int i = 0; i <= (larger - smaller); i++) {
cout << larger - i << endl;
}
|
for my opinion this way is much easier and saves you to create another variable and re declare his value few times on the program.
but i want you to understand easier so for the total program code example i will use the first way.
here what it should look like at the end:
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
|
#include <iostream>
using namespace std;
int main()
{
cout << "Intructions: Please enter two positive integers and this program will display the smaller one to the larger and the larger one to the smaller one. Does not matter in which order you input the positive integers." << endl;
int first;
int second;
int smaller;
int larger;
int displayCounter;
do{
cout << "Enter the FIRST positive integer: ";
cin >> first;
if (first < 0)
cout << "*Number recieved is NEGATIVE please enter POSITIVE" << endl;
} while (first < 0);
do {
cout << "Enter the FIRST positive integer: ";
cin >> second;
if (second < 0)
cout << "*Number recieved is NEGATIVE please enter POSITIVE" << endl;
} while (second < 0);
if (first < second) {
smaller = first;
larger = second;
}
else {
smaller = second;
larger = first;
}
displayCounter = smaller; // using temporary variable that we can change but still keep the user input original value
cout << "Displaying: small to large" << endl;
for (int i = 0; i <= (larger - smaller); i++) // for loop that runs aslong i smaller or equal to difference between the large or small inputs
{
cout << displayCounter << endl;
displayCounter++; // changing the temporary number as we like to display on screen
}
displayCounter = larger; // ajusting temporary number to our use in this case to display large to small without changing original user input
cout << "Displaying: large to small" << endl;
for (int i = 0; i <= (larger - smaller); i++) //we used same for condition but this time we decreasing our temporary variable instread increasing it
{
cout << displayCounter << endl;
displayCounter--;
}
while (true); // forever loop just to prevent the "end of program" message (there are way to do it cleaner but i dont want to complicate you
}
|
Hope i was clear enough for you to understand tried to simplfied it as much as i can.
if you have any questions im here
Have a nice day, sean. :)