0

I'm a newbie in the programming world, and i've decided to start with C++ code a few days ago as my first programming language. I just started to read an online course which i'm guiding on (and aply while i'm reading it).

The course in question assigns a serie of small optional exercises, wich go hand in hand with the topic that is being dealt at that moment.

One of this optional exercises is: "Create a program that multiplies two whole numbers in the following way: it will ask the user for a first whole number. If the number that you type is 0, it will write on the screen "The product of 0 by any number is 0". If a number other than zero has been entered, the user will be prompted for a second number and the product of both will be displayed."

How it says, I did my best to coding that program.

The code of what I did is:

#include <iostream>
using namespace std;

int main ()
{
    int a;
    int b;
    int solve;
    cout << "Enter a number: ";
    cin  >> a;
    if (a!=0)
    {
        cout << "Enter another number: ";
        cin >> b;
    }
    if (b!=0)
    {
        solve = a * b;
        cout << "The result of your operation is: " << solve << endl;
    }
        else cout << "The product of 0 by any number is 0." << endl;
        
return 0;
}

So, I press F9 to compile, then F10 to run it.

I proceed to testing it.

  1. I put and different number from zero, I put another one. Throws me a multiplication of both. Nice.
  2. I put and different number from zero, I put another one that actually its zero. Throws me the message of "else" order. Nice. **BUT
  3. I put and number equal to zero, and throws me the message of the "cout" order of "if (b!=0)".**

I didn't really know what I had done wrong, so, I ask for help from a friend who has some more experience than me, and tells me that actually it's nothing wrong. In fact, he proved me sending to me a screen cap of his Dev C++ with my code in it, and how it runs just how it had to be.

Then, I opened an online compiler (https://www.onlinegdb.com/online_c++_compiler#) to get down my doubts, and yes, there runs correctly too.

So, here's my question? What's the problem? Why that happens?

I have the DevC++ 5.11 TDM-GCC 4.9.2, and I'm using the deafult compiler.

Please, I would like some of help, I feel more comfortable compiling in PC than online, it's more quick.

Thank you anyway for reading until here.enter image description here

cigien
  • 57,834
  • 11
  • 73
  • 112
Alberu
  • 1
  • 3
    `int b;` You need to initialise your variables inside the function, otherwise the compiler may leave them with any garbage value. Then `b != 0` evaluates to true. – TrebledJ Dec 16 '20 at 16:30
  • 1
    @TrebledJ I think that's a reasonable answer. You can post one if you want. – cigien Dec 16 '20 at 16:37
  • If you're not forced to use it, avoid Dev-C++. It's probably the worst tool you could pick today. – sweenish Dec 16 '20 at 16:47
  • @sweenish Which programming tool would you recommend for me? – Alberu Dec 16 '20 at 17:28
  • Literally anything else. If you want something lighter-weight, maybe CodeBlocks. If you want a full IDE, VS Community or XCode or CLion. If you have no problems putting the pieces together yourself, VSCode or even emacs or vim. Dev-C++ is unsupported and hasn't even fully implemented C++11, a nearly 10 year old standard. – sweenish Dec 16 '20 at 17:34

1 Answers1

2

The reason why this happens, as @TrebledJ mentioned, is that you've not initialised the variable b. So you can get over this problem just by initialising b to any value of your choice (preferrably 1 or 0 for simplicity). But there's a workaround if you don't want to initialise the values. I would transform your code to something like this:

if (a!=0)
    {
        cout << "Enter another number: ";
        cin >> b;
        if (b!=0)
        {
            solve = a * b;
            cout << "The result of your operation is: " << solve << endl;
        }
        else cout << "The product of 0 by any number is 0." << endl;
    }
else cout << "The product of 0 by any number is 0." << endl;

Basically, what I'm doing is, if the value of a after input is 0, I'm not running the part for taking the user input of b.

DeBARtha
  • 460
  • 6
  • 17