我试图理解为什么我在其中一个变量userNumberInput上添加了零。我一直在试图弄清楚为什么在没有好的解决方案或解释的情况下发生这种情况。它只发生在输出文件中,并且由于变量设置为0而似乎没有发生。
例如,如果输入90,则控制台显示90,但输出文件显示900。
#include <stdio.h>
#include <stdlib.h>
void Fibonacci (int userInputNumber);
void UserInput (int* userInputNumber);
FILE *fpOut;
int main(int argc, const char * argv[]) {
//Creating global variable for userInput to be passed from function to function
int userInputNumber = 0;
//Opening file for writing output to
if (!(fpOut = fopen("csis.txt", "w"))) {
printf("csis.text could not be opened for output");
exit(1);
}
//Calling two functions and passing appropriate vaiables to each
UserInput(&userInputNumber);
Fibonacci(userInputNumber);
fclose(fpOut);
return 0;
}
void UserInput (int* userInputNumber) {
//Asks the user for a number for the generator
printf("Enter a number into the Fibonacci Generator: ");
scanf("%d", &*userInputNumber);
fprintf(fpOut,"Enter a number into the Fibonacci Generator: %d", *userInputNumber);
//While the user continues to enter a negative number it forces the user to enter a new number
while (*userInputNumber < 0) {
printf("Invalid user input, enter a positive number\n\n");
fprintf(fpOut,"Invalid user input, enter a positive number\n\n");
printf("Enter a number into the Fibonacci Generator: ");
scanf("%d", &*userInputNumber);
fprintf(fpOut,"Enter a number into the Fibonacci Generator: %d", *userInputNumber);
}
}
void Fibonacci (int userInputNumber) {
//Defines variables used in this function
int firstNumber, secondNumber, i, seriesNumber, length;
firstNumber = 0;
secondNumber = 1;
seriesNumber = 0;
i = 0;
length = 0;
//For loop that adds the two previous number in the series and prints it and then updates the previous numbers
for (i = 0; seriesNumber < userInputNumber; i++) {
if (i <= 1) {
seriesNumber = i;
length++;
} else {
seriesNumber = firstNumber + secondNumber;
firstNumber = secondNumber;
secondNumber = seriesNumber;
length++;
}
//Statement to break the loop when the next number in the series goes above the user input
if (seriesNumber > userInputNumber) {
length -= 1;
break;
}
//Printing the sequence
printf("%d\n",seriesNumber);
fprintf(fpOut,"%d\n",seriesNumber);
}
//Printing the length of the sequence
printf("Length of the sequence: %d", length);
fprintf(fpOut,"Length of the sequence: %d", length);
}
答案 0 :(得分:1)
您必须在打印输入的值后打印换行符,或者0
这是序列的第一个术语,似乎会附加到输入中。
尝试更改两个
fprintf(fpOut,"Enter a number into the Fibonacci Generator: %d", *userInputNumber);
到
fprintf(fpOut,"Enter a number into the Fibonacci Generator: %d\n", *userInputNumber);