MHB Using a while loop to print the counting numbers up to a set value

AI Thread Summary
The discussion focuses on correcting a while loop in C++ that is intended to print numbers from 1 to a given positive integer, userNum. The initial code incorrectly counts down from userNum instead of counting up. To fix this, the loop should use the variable i as an index, starting from 1 and incrementing until it exceeds userNum. The correct implementation involves initializing i to 1, using a while loop that checks if i is less than or equal to userNum, and printing i followed by a space. This adjustment ensures the output matches the expected format, displaying numbers in ascending order.
needOfHelpCMath
Messages
70
Reaction score
0
Write a while loop that prints 1 to userNum, using the variable i. Follow each number (even the last one) by a space. Assume userNum is positive. Ex: userNum = 4 prints:
1 2 3 4

Code:
#include <iostream>
using namespace std;

int main() {
   int userNum = 0;
   int i = 0;

   userNum = 4;    // Assume positive
   
   
while ( userNum > i) {
cout << userNum << " ";
userNum = userNum - 1;
}

   cout << endl;

   return 0;
}
Run
✖ Testing with userNum = 4
Expected output: 1 2 3 4
Your output: 4 3 2 1
 
Technology news on Phys.org
Re: What i am doing wrong?

You need to use [m]i[/m] to "index" your while loop, which should run as it is less than or equal to [m]userNum[/m]. Since [m]i[/m] is initialized to zero, you will want to increment it before the comparison, using [m]++i[/m], and then you will not need to increment it within the loop, you will only need to print its value along with the trailing space.
 
Re: What i am doing wrong?

MarkFL said:
You need to use [m]i[/m] to "index" your while loop, which should run as it is less than or equal to [m]userNum[/m]. Since [m]i[/m] is initialized to zero, you will want to increment it before the comparison, using [m]++i[/m], and then you will not need to increment it within the loop, you will only need to print its value along with the trailing space.

"Awesome To The Max"
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...

Similar threads

Replies
1
Views
6K
Replies
2
Views
13K
Replies
4
Views
11K
Replies
28
Views
30K
Replies
1
Views
1K
Replies
1
Views
2K
Back
Top