English 中文(简体)
视觉演进室 C++ 代码:假设数学持续给我0%的结果,我感到困惑的是,为什么和如何固定。
原标题:Visual Studio C++ code: Arithmetic math keeps giving me the result of 0% and I am confused why and how to fix it
  • 时间:2023-12-18 00:58:19
  •  标签:
  • c++

我是《科索沃法典》的新篇章,而我是《塞浦路斯法典》的正文。 我正试图实施一个简单的用户投入方案,能够增加2个数字。 终端窗口给我带来0%的结果,我无法确定原因。 我现在要记住的是,编码是怎样的。 我使用的方法比我可能要多,但我只是试图击.。 我有一个MacBook Pro,如果这对我取得这一令人ir慕的结果有什么影响。 我将下面列出该守则,并列入我拍摄的屏幕。 感谢你们的帮助!

#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);
}

终端窗口显示:

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给我在终端窗口上的0%。

问题回答

@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 to represent a newline character. Replace that line with:

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

正确的法典是:

#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:" <<  
 ;
    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.

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:
";
    cout << "Input a number: ";
    cin >> x;
    cout << "Input another number: ";
    cin >> y;
    
    z = x * y;
    cout << "Hello World! Your result is: " << z <<  
 ;
    return 0;
}

之后,你应当:

> % 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




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签