skip to Main Content

I am new to VS Code and I am rusty on c++. I am trying to do a simple user input program that can multiply 2 numbers. The terminal window is giving me a result of 0% and I am not sure why. I now remember how coding was so annoying haha. I used way more #includes than I probably should have but I was just trying to trouble shoot. I have a MacBook Pro if that has any impact on why I am getting this weird result. I will put the code down below and also include a screenshot I took. Thank you for any help!

#include <iostream>
#include <cmath>
#include <math.h>
#include <iomanip>
using namespace std;
int x = 0;
int y = 0;
int z;
int main(){
    cout << "We're going to multiply 2 numbers:" << '/n';
    cout << "Input a number: ";
    std::cin >> x;
    std::cout << "Input a number: ";
    cin >> y;
    z = x * y;
    std::cout << "Hello World! Your result is: " << z;
    return (0);
}

The Terminal Window shows:

cd "/Users/sgette/Documents/C++ Projects/.vscode/" && g++ math.cpp -o math &&       "/Users/sgette/Documents/C++ Projects/.vscode/"math
sgette@Scotts-MacBook-Pro C++ Projects % cd "/Users/sgette/Documents/C++ Projects/.vscode/"     && g+
+ math.cpp -o math && "/Users/sgette/Documents/C++ Projects/.vscode/"math
Input a number: 8
Input a number: 9
Hello World! Did you know that89is: 0%    
                                                   

Multiplication gives me a 0% in the terminal window

2

Answers


  1. @G3773 It looks like the issue in your code is with the line:
    cout << "We're going to multiply 2 numbers:" << '/n'; and return (0)

    You are using the character literal ‘/n’ which should be ‘n’ to represent a newline character. Replace that line with:

    cout << "We're going to multiply 2 numbers:" << 'n'; ,
    return 0

    The correct code is:

    #include <iostream>
    #include <cmath>
    #include <math.h>
    #include <iomanip>
    
    using namespace std;
    
    int x = 0;
    int y = 0;
    int z;
    
    int main() {
        cout << "We're going to multiply 2 numbers:" << 'n';
        cout << "Input a number: ";
        cin >> x;
        cout << "Input a number: ";
        cin >> y;
        z = x * y;
        cout << "Hello World! Your result is: " << z;
        return 0;
    }
    

    Hope to be helpful for your understanding.

    Login or Signup to reply.
  2. Your code was mostly right. But, for one, you don’t need all the imported math libraries. You are not doing any mathematical operations that need them.
    Second, why are the variable declarations outside of the main function?

    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main() {
        int x = 0, y = 0, z = 0;
    
        cout << "We're going to multiply 2 numbers:n";
        cout << "Input a number: ";
        cin >> x;
        cout << "Input another number: ";
        cin >> y;
        
        z = x * y;
        cout << "Hello World! Your result is: " << z << 'n';
        return 0;
    }
    

    After running that you should get:

    > % g++ math.cpp 
    > % ./a.out 
    > We're going to multiply 2 numbers: 
    >Input a number:
    > 8 
    > Input another number: 
    > 9 
    > Hello World! Your result is: 72
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search