Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how I make that the program dosent accept numbers less or equall to 0

+2 votes
asked Oct 28, 2021 by JUAN CAMILO LUNA TORRES (140 points)
#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
    int num,juan_i,juan_p;
    cout<< "Put a whole number"<<endl;
    cin>> num;
    juan_i = 0;
    cout<<endl;
    while (juan_i<=20) {
        juan_p = juan_i*num;
        cout<<juan_i<<" x "<<num<<" = "<<juan_p<<endl;
        juan_i=juan_i+1;
    }
    return 0;
}

1 Answer

0 votes
answered Oct 30, 2021 by Dawidh Feltz (150 points)

I would include the following code lines in bold to your code. It should work.

#include <iostream>
using namespace std;
int main(int argc, char *argv[]) 

{
    int num,juan_i,juan_p;
    cout<< "Put a whole number"<<endl;
    cin>> num;

    if (num >= 1)                //Use if statement as a filter to filter out negative numbers that are less than 0.
    {

        juan_i = 0;
        cout<<endl;
        while (juan_i<=20) 

        {
        juan_p = juan_i*num;
        cout<<juan_i<<" x "<<num<<" = "<<juan_p<<endl;
        juan_i=juan_i+1;
        }

    else

    {

    cout << "That is not a valid number!" << endl;            // Use this as else statement, you can write anything saying that the number has to be positive.

    }
    return 0;
}

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...