我有一個完整的C程序,提示用戶輸入一個介於0和100000之間的數字。然後打印出數字的每個數字。我的代碼完成了這項任務,但是它並沒有通過值得理想的功能正確傳遞。我需要一些關於如何接受我所擁有的建議,並按功能正確傳遞用戶價值並返回。C程序按值傳遞
#include <stdio.h>
void print_1(int,int);
void print_10(int,int);
void print_100(int,int);
void print_1000(int,int);
void print_10000(int,int);
int main() {
int userInput;
printf("Please enter a positive number less than 100,000: \n");
scanf("%d", &userInput);
if (userInput > 0 && userInput < 100000)
{
printf("\nYou have entered: %d\n",userInput);;
}
else
{
printf("\nThe number you have entered is out of the specified range\n");
exit(0);
}
int lastDigit;
print_1(userInput,lastDigit);
} //End main
// ===========================================================================
// Function : Print_1
// Description : Prints out the 1st digit of user input
//
// Input :
// input userInput
// output userInput
// Return value :
// return value
//
// Notes: This function repeats several printing the lastDigit then slicing it
// off before passing on to a new function.
//============================================================================
void print_1(int userInput,int lastDigit)
{
lastDigit = userInput % 10;
printf("\n1\'s: %d \n", lastDigit);
userInput = (userInput - lastDigit)/10;\
print_10(userInput,lastDigit);
} // end print_1
// ===========================================================================
// Function : Print_10
// Description : Prints out the 1st digit of user input
//
// Input :
// input userInput
// output userInput
// Return value :
// return value
//
// Notes: This function repeats several printing the lastDigit then slicing it
// off before passing on to a new function.
//============================================================================
void print_10(int userInput,int lastDigit)
{
lastDigit = userInput % 10;
printf("\n10\'s: %d \n", lastDigit);
userInput = (userInput - lastDigit)/10;
print_100(userInput,lastDigit);
} // end print_10
// ===========================================================================
// Function : Print_100
// Description : Prints out the 1st digit of user input
//
// Input :
// input userInput
// output userInput
// Return value :
// return value
//
// Notes: This function repeats several printing the lastDigit then slicing it
// off before passing on to a new function.
//============================================================================
void print_100(int userInput,int lastDigit)
{
lastDigit = userInput % 10;
printf("\n100\'s: %d \n", lastDigit);
userInput = (userInput - lastDigit)/10;
print_1000(userInput,lastDigit);
} //end print_100
// ===========================================================================
// Function : Print_1000
// Description : Prints out the 1st digit of user input
//
// Input :
// input userInput
// output userInput
// Return value :
// return value
//
// Notes: This function repeats several printing the lastDigit then slicing it
// off before passing on to a new function.
//============================================================================
void print_1000(int userInput,int lastDigit)
{
lastDigit = userInput % 10;
printf("\n1000\'s: %d \n", lastDigit);
userInput = (userInput - lastDigit)/10;
print_10000(userInput,lastDigit);
} //end print_1000
// ===========================================================================
// Function : Print_10000
// Description : Prints out the 1st digit of user input
//
// Input :
// input userInput
// output userInput
// Return value :
// return value
//
// Notes: This function repeats several printing the lastDigit then slicing it
// off before passing on to a new function.
//============================================================================
void print_10000(int userInput,int lastDigit)
{
lastDigit = userInput % 10;
printf("\n10000\'s: %d \n", lastDigit);
userInput = (userInput - lastDigit)/10;
return userInput;
}//end print_10000
//End Program
這段代碼做了什麼?爲什麼這樣搞砸了? –
代碼中的所有函數參數都是按值傳遞的。 – Ben
_但它不能正確傳遞通過理想的函數的值 - 該語句似乎並不反映您粘貼的代碼中的任何有效內容。也許你應該描述你所遇到的問題,而不是你認爲的原因。 – mah