0

I am trying to output a triangle sort of figure based on the user entered number. For example, if the user enters 3, the output should be:

1

12

123

but all I am getting is

1

2

3

what do I need to fix?

//File: digits.cpp
//Created by: Noah Bungart
//Created on: 02/16/2020

#include <iostream>
#include <cmath>
using namespace std;

int main(){

    int Rows;
    cout << "Enter number of rows: ";
    cin >> Rows;

    int i = 1;
    int j = 1;

    while (i <= Rows){
        while (j <= i){     //Should be iterating through j loop but isn't
            if (i < 10){
                cout << j;
            }
            if (i >= 10){
              cout << (j % 10); 
            }
            j++; 
        }
        cout << endl;
        i++; 
    }
    return(0);
}
2
  • initialize j to 1 just before the 2nd while Commented Feb 17, 2020 at 2:44
  • 2
    And when you used your debugger to run your program, what did you see? This is what a debugger is for. If you don't know how to use a debugger this is a good opportunity to learn how to use it to run your program one line at a time, monitor all variables and their values as they change, and analyse your program's logical execution flow. Knowing how to use a debugger is a required skill for every C++ developer, no exceptions. With your debugger's help you should be able to quickly find all bugs in this and all future programs you write, without having to ask anyone for help. Commented Feb 17, 2020 at 2:45

1 Answer 1

1

You just forgot to "reset" the j variable.

Just add a j = 1; just after the i++ (or before the second while loop).

Sign up to request clarification or add additional context in comments.

1 Comment

@Pan1c don't forget to accept the answer if it solved your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.