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%
2
Answers
@G3773 It looks like the issue in your code is with the line:
cout << "We're going to multiply 2 numbers:" << '/n';
andreturn (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:
Hope to be helpful for your understanding.
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?
After running that you should get: