Hi, I'm working on a program that will allow the user to input an integer between 1 and 999 and display the numbers as shown by the example of inputing 3 which would result in the output below.

000
001
002
003

I've already written the part of the program that takes the imput from the user and splits the three digit integer into the 3 parts to be used in the for loop (for example 123 would be split into 1, 2 and 3.

We're required to use three nested for loops in order to display the numbers in the form I've listed above. Below is the code I have already written that takes in the integer from the variable and splits it up. I've racked my brain trying to figure out how to use the for loops to do what I need to do but I'm helpless. I'm not asking you to code my homework for me (which I know is a pet peeve of you forum goers) but if you could even give me an idea of how to set up the for loops I would be eternally grateful. I have tried multiple variations of nested for loops but none have worked.

Here is the code I have written:

/* Name: Scott Kamen
Student Number: 9054
Assignment number and descrition: Lab 02 Assignment (Program to out put all numbers
up to the number inputed by the user; ex 2 is input: 000, 001, 002 is displayed)
Lab Instructor: Huma Kamal
Lab Day and Time: Wed at 6:00 */

#include <iostream>
using namespace std;

int main() {

 // Declare integer variable to store user input in
 int userinteger;

 // While loop to read integers inputed by user until an appropriate
 // one is input
 while (userinteger < 0 || userinteger > 999) {

   // Prompt user to input integer and then store input in userinteger
   // variable
   cout << "Input a positive integer between 0 and 999: ";
   cin >> userinteger;

   // Test variable userinteger to see if it's inside desired range
   // and display error message if it's not
   if (userinteger < 0 || userinteger > 999)
     cout << "Error: Please input an integer between 0 and 999" << endl;
 }

 // Mod Algorithm to split up variable userinteger
 int firstdigit = (userinteger/100)%10;
 int seconddigit = (userinteger/10)%10;
 int thirddigit = userinteger%10;

 // For Loops to Display Digits in Correct Format (This is where I am stuck...)

 return 0;
}

Below is a link to the lab page if you would care to look at it.

http://www.cs.wmich.edu/~nelson/CS111FALL04/lab02/lab02.html

thanks so much,

-Scott

Hello ,,Try this

**************Embedd it after ur codei-e before return 0;*****************

int f=firstdigit;
 int s=seconddigit;
 int t=thirddigit;

int k=0;


for(int i=0;i<=firstdigit;i++)
{ 
         if(f>=1) s=9;else s=seconddigit;
    for(k=0;k<=s;k++)
     {
         if(i==firstdigit && k==seconddigit) t=thirddigit;else t=9;

         for(int j=0;j<=t;j++)
         {
             cout<<i<<k<<j<<endl;
         }
    }
    s--;
f--;
}

*************************************************

Hope it will work , but if still having some problems ask me,

Fahad

Edited 3 Years Ago by Dani: Formatting fixed

Write the code given by me in place of this statement

// For Loops to Display Digits in Correct Format (This is where I am stuck...)

Fahad

Thanks fahad. The code works great and makes total sense.

thanks again,

-Scott

This article has been dead for over six months. Start a new discussion instead.