如果初始输入是C++中的奇整数,则两个整数相乘
Multiplying two integers if initial input is an odd integer in C++
我正在为一堂刚开始的C++课做家庭作业,我有点不知所措
任务如下:
创建一个c++程序,要求用户输入一个数字。
程序的输出应为以下内容之一:您输入了一个EVEN号码。或您输入了ODD号码。
如果用户输入了ODD号码,请他们输入另一个号码。将这个数字乘以第一个数字并输出结果。
偶数/奇数部分很容易——我已经完成了这个部分。我对第二部分完全迷失了方向。我的错误太多了,我甚至不知道开始在哪里。如果有人能给我一个提示,告诉我我做错了什么,我将不胜感激。
#include <iostream>
using namespace std;
int main () {
int num1; // This is the original number entered by the user.
int num2; // This is the second number entered if the first number is odd.
cout << "Enter a number: "<< endl;
cin >> num1 >> endl;
if (num1 % 2 == 0) {
cout << num << " Your number is even." << endl;
} if (num1 % 2 != 0) {
cout << num1 << " Your number is odd. Please enter another number: “<< endl;
cin >> num1 >> endl;
} // end of if odd
cout << " Your two numbers multiplied equals (num1 *= num2)” << endl;
} // end of main ()
#include <iostream>
using namespace std;
int main () {
int num1; // This is the original number entered by the user.
int num2; // This is the second number entered if the first number is odd.
cout << "Enter a number: "<< endl;
cin >> num1;
if (num1 % 2 == 0) {
cout << num1 << " Your number is even." << endl;
}
else {
cout << num1 << " Your number is odd. Please enter another number: " << endl;
cin >> num2;
cout << " Your two numbers multiplied equals " << num1*num2 << endl;
} // end of if odd
return 0;
} // end of main ()
这是固定代码。您尝试了cout << num
,但没有num
变量,应该是num1
,cin >> endl
也是错误的。
出乎意料的是,你最后的”
不是"
,而是其他东西,它会产生很多错误。
奇数值的部分
if (num1 % 2 != 0) {
cout << num1 << " Your number is odd. Please enter another number: “<< endl;
cin >> num2 >> endl;
cout << " Your two numbers multiplied equals:" << (num1 * num2) << endl;
}
if (num1 % 2 != 0) {
cout << num1 << " Your number is odd. Please enter another number:"<< endl;
cin >> num2;
cout << " Your two numbers multiplied equals:" << (num1 * num2) << endl;
}
不要把公式放在引号之间。这会将它们变成字符串或字符,无法按需执行。即cout << " Your two numbers multiplied equals (num1 *= num2)” << endl;
如果将语句cout << " Your two numbers multiplied equals (num1 *= num2)” << endl;
放在if语句之外,则即使数字不是奇数,也会运行该语句。这与作业不符。并且num2为空仍将导致错误
相关文章:
- C++需要帮助从用户那里获得一个整数,并确保它在另外两个整数之间
- 比较C++中两个整数的最有效和最干净的方法是什么?
- 如何将字符串和整数读取到两个单独的动态数组中的程序编写?
- 为什么将两个浮点数相加会得到一个整数C++?
- 从两个 4x64 位整数数组中获取取模
- 为什么 Clang 和 GCC 中两个无符号整数之和的结果类型不同
- 比较两个整数在C++中与未知 int 类型的相等性
- 创建整数的 2D 数组,该数组将使用两个函数用随机数填充矩阵.我做错了什么?
- 如何组合两个整数向量
- 在两个整数之间交换最右边的N位
- 打印出两个整数之间的偶数
- 为什么将两个总和为 32k >整数相加时得到负数?
- C++ 两个线程,共享几个整数变量
- Python 等效代码,可像C++一样直接附加两个整数
- 是否有任何内置函数可以检查给定的两个数字在给定整数数组中的顺序是否相同?
- 移位运算符如何在查找两个整数中的不同位数?
- 如何安全地比较两个无符号整数计数器?
- 添加两个二进制数(整数数组)
- 运算符"|"如何计算两个整数?
- 如何找出在两个整数的乘积中设置了多少位(等于 1)