我正在爲一個開始的C++類進行家庭作業分配,並且我有點失落。
這是作業:如果初始輸入是C++中的奇數整數,則將兩個整數相乘
創建一個C++程序,要求用戶輸入一個數字。
該程序的輸出應該是以下之一: 您輸入了一個偶數。 或 您輸入了一個奇數。
如果用戶輸入一個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()
你問第二個數字,然後做:'cin >> num1 >> endl;'不應該在'num2'中?你也說你得到很多錯誤,但實際上並沒有告訴我們這些錯誤是什麼。 (PS:'cout << num'這是無效的,你沒有聲明'num'作爲一個變量) – Borgleader 2015-02-09 21:32:57
你應該記得從'main'返回一個值(而不是讓控制權流走)。 – inetknght 2015-02-09 21:34:56
我認爲語法高亮顯得很明顯,你在那裏有一些流浪的智能引號。 'cin >> endl;'應該消失。 @inetknght,當流出'main'的末尾時返回0。 – chris 2015-02-09 21:35:47