I'm stuck with a very simple problem

Hi,

I'm trying to learn how to code in C++. I have to create a simple program that will generate the multiplication table (from 1 to 10) of a given number entered by the user (that part I had no problem dealing with). However, the input of the user cannot exceed 10 (that's where I'm stuck at).

I tried this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;
int main(){
    int n;
    cout << "Enter a number that is less or equal to 10: ";
    if (n <= 10) {
        cin >> n;
        for (int i = 1; i <= 10; ++i) {
        cout << n << " * " << i << " = " << n * i << endl;
        }
    return 0;
    }
    else cout << "Invalid number. Try again" << endl;
    }


When I run my program and I enter, let's say, 15, it gives me the multiplication table of the number 15 (which is not what I want since the input cannot exceed 10).

Thank you in advance!
@dada51

You could try..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
int main()
{
   int n = 0;
do{
     cout << "Enter a positive number that is less or equal to 10: ";
     cin >> n;
     }while (n < 1  || n >10);
     for (int i = 1; i <= 10; ++i)
       {
        cout << n << " * " << i << " = " << n * i << endl;
        }
    return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main()
{
	int n;

	while ((cout << "Enter a number that is less or equal to 10: ") && !(cin >> n) || (n < 1 || n > 10)) {
		cout << "Invalid number. Try again\n";
		cin.clear();
		cin.ignore(1000, '\n');
	}

	for (int i = 1; i <= 10; ++i) {
		cout << n << " * " << i << " = " << n * i << endl;
	}
}

Looks to me that your problem stems from the fact you do the check on 'n' before the program reaches the input part of your code.
Hello dada51,

This is mostly your code rearranged to make better use of what you started with. Also with some additions and changes that I hope you will fine useful, but they are not required.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

using namespace std;

int main()
{
    constexpr int MAXNUM{ 10 };   // <--- Added.
    constexpr int MAXLOOP{ 10 };  // <--- Added.

    int n{};  // <--- Changed. Always a good idea to initialize your variables.

    cout << "Enter a number that is less or equal to " << MAXNUM << ": ";
    cin >> n;  // <--- Moved to here.

    if (n > MAXNUM)  // <--- Changed.
    {
        cout << "\n     Invalid number. Try again";  // <--- Changed. Added the "\n" and spaces. The "\n" is good, but the spaces are not required.

        return 1;  // <--- Leave the program indicating a problem.
    }

    for (int i = 1; i <= MAXLOOP; ++i)
    {
        cout << n << " * " << i << " = " << n * i << '\n';
    }

    return 0;
}

Something to consider.

The if statement could be replaced with a while or even seeplus's while loop.

Andy
Hello dada51,

I have always believed that if you do not understand what is wrong with your program then you will not understand how to fix it.

The code that you started with is fine if you are writing the code for the compiler. This may take a few milliseconds of the compile time. It is also possible to write
1
2
#include <iostream>
using to the closing } of "main all on 1 line. // Great for the compiler, but almost impossible to read or work on. 

When you write your source code you are writing it for someone to read, be it your-self or someone else, so make it easy to read. As an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

int main()
{
    int n;

    cout << "Enter a number that is less or equal to 10: ";

    if (n <= 10)
    {
        cin >> n;

        for (int i = 1; i <= 10; ++i)
        {
            cout << n << " * " << i << " = " << n * i << endl;
        }

        return 0;
    }
    else cout << "Invalid number. Try again" << endl;
}

How you use the {}s is up to you, but be consistent in their use.

Adding the blank lines also can make problems show up easier.

Looking at your code:

Line 7. A better name than "n" would be helpful. I eventually changed "n" to "baseNum" which helps in the rest of the program to see what you are using. It is also a good policy to initialize your variables when you define them. If for no other reason than to know that they do not contain a garbage value.

On my computer an uninitialized "int" generally has the value of (-858993460) and an uninitialized "double" has the value of (-9.2559631349317831e+64). Neither is something that you would want.

This leads to a problem with line 11. To look at it another way: if (-858993460 <= 10). This would make your if statement always true.

Line 13 is entering a value for "baseNum", ("n"), but after you have checked if the value if OK. Kind of backwards. What you have would allow "baseNum" to hold a value of 100 or any number > 10.

Line 20 should be after line 22. If you are inside the if statement the else will be bypassed and if you bypass the if statement when the else is finished you are at the end of the program. So putting the "return 0" inside the if statement has no benefit because either way when either if or else is done you are at the end of "main" and the program is over.

I hope that you understand better what is wrong and that the changes offered make more sense now.

Andy
Thank you for all the great replies (I didn't expect that in such a short time).

I think the big mistake was putting the "return 0" before the "else cout".

My program is now written that way:

#include <iostream>

using namespace std;

int main()

{

int n;

cout << "Enter a number that is less or equal to 10: ";

cin >> n;

if (n <= 10)

for (int i = 1; i <= 10; ++i)

{

cout << n << " * " << i << " = " << n * i << endl;

}

else {

cout << "Invalid number. Try again" << endl;

}

return 0;

}



@Andy: Everything you've said makes sense. There are some functions you've mentioned that are still unknown to me. Like I said, I'm at the very beginning of programming.

Thank you again!
Hello dada51,

Much better, but please use code tags.

dada51 wrote:

@Andy: Everything you've said makes sense. There are some functions you've mentioned that are still unknown to me.


You will have to tell me what they are before I can help.

Andy
@Handy Andy I think dada was referring to the keywords constexpr as i don't see any exotic functions within your examples.
@CodeGoggles,

I see your point. I realized I did not scroll up far enough and was looking at the wrong bit of code.

@dada51,

"constexpr" is available from the C++11 standards and as I understand it it is an update to the older qualifier "const". Either one will work to create a constant variable that can not be changed. I just prefer using the newer version where possible.

If your compiler has a problem with "constexpr" or the uniform initializer the {}s then you are not using the C++11 standards, but something previous to that.

Andy
A couple minutes ago I didn't have a clue about what you meant by "code tags".

Right now, I'm reading this thread: https://www.cplusplus.com/articles/jEywvCM9/

Now I understand a little more about what you guys are expecting from me.

The functions that are still unknown to me are, for example, "constexpr (...) MAXNUM{}" and "constexpr (...) MAXLOOP{}", but I found this doc (https://docs.microsoft.com/en-us/cpp/cpp/constexpr-cpp?view=msvc-160) that I will have to read soon.

EDIT @ Andy: I had not seen your reply. Thank you!
Last edited on
Topic archived. No new replies allowed.