我必須完成C.C函數返回兩個值
編寫要求用戶輸入兩個正整數的函數分配如下,讀取這兩個數字,說A和B,以及一直詢問他們,直到用戶輸入兩個這樣的號碼。該函數將這兩個數字返回到它被調用的地方。
我在這裏有點困惑。我將如何要求用戶從一個函數中輸入兩個值?你不能只從main()函數做到這一點嗎?截至目前,我有以下功能代碼。它工作正常,但當然我需要在外部功能。
#include <stdio.h>
int main() {
int a(2); // initialize just as some positive number so as not to set off negative alert.
int b(2);
printf("Enter two positive numbers: \nFirst: ");
do {
if (a <= 0 || b <= 0) { // negative alert
printf("Woops. Those are negative. Try again. \nFirst: ");
}
scanf(" %d", &a);
printf("Second: ");
scanf(" %d", &b);
printf("\n");
} while (a <= 0 || b <= 0);
return(0);
}
在這裏混合C&C++,它看起來像...... – 2013-03-07 17:52:59
函數是一個函數。沒有理由你不能在你想要的任何功能上做到這一點。 – 2013-03-07 17:53:39
爲什麼你不能把這個代碼放在一個不同的函數中?我不確定我是否理解這個問題... – 2013-03-07 17:53:45