How to use looping in C++

I am new to C++ and I am making a program to generate a multiplication table. Here is the code.
{Code}

#include <iostream>
using namespace std;
int main()

{
  int num;
    cout << "Enter a number to find Multiplication Table   ";
       cin >>num;

            for(int a=1;a<=10;a++)
                     {
                        cout<<num<<" x "<<a<<" = "<<num*a<<endl;

                     }
       cout << "Press ENTER to continue...\n";
       cin.get();
       getchar;
       return 0;

}

I need that after the multiplication table of one number is shown, the user should have a choice of entering another number or leaving. Like press “n” to enter another number or “e” to exit

You’re counting the variable a , loop is fine. You need some extra if statements and “==” operator You can add some if statements, if this == this then open brakets and write there what ever it’s going to happen, this operator “==” is for exact match so you may want to use “<” or “>”

Variable “a” if you have it in your header file you should be fine with it outside the function so you can make it even outside the loop, it will store the numbers.
Since it’s part of the loop you got to redefine the var and say that a = something else and this way you can have the other one inside the header as declaration, you cannot declare “a” in the header because it’s made in the loop and first declared there because it’s how loops get created.

You can include a directly in your statement, it should work within the construct tho, not outside of it.

Something simple.
If ( a< 5)
{
do what ever
}

If you are trying to have other options if it’s not smaller or bigger the same thing applies, you need an if statement or a function repetition, meaning the function get’s called again. You could include any number with a operator if a < 0 then open brakets and call the function again.
{
functionname();
}

You also got api code with press but you got to set the keys in the setup in the editor.
PlayerInputComponent->BindAxis
and
PlayerInputComponent->BindAction

These let’s you setup keys.
For example.
PlayerInputComponent->BindAction(“ResetVR”, IE_Pressed, this, &ATutorialCharacter::OnResetVR);

You have to define IE in the setup of key bindings it can be named anything.
Once you press it you have to have a function for it to set it off.

For example.
we call the function with the press instruction above.
void ATutorialCharacter::OnResetVR()
{
UHeadMountedDisplayFunctionLibrary::ResetOrientationAndPosition();
}

This is just for the vr so your function would look a bit different regarding some properties.

Another example.
PlayerInputComponent->BindAction(“PressE”, IE_Pressed, this, &AYourconstructor::yourfunctionname;
So there is E key Binding with the word PressE variable, the pressE is tied to a key on your keyboard . You can make it work with what you have and adapt it, the function decides what happens once you press the key

1 Like