【发布时间】:2014-02-06 06:49:42
【问题描述】:
所以我尝试了数据类型 char 和数据类型 int。不管 printf 只显示输入单词的第一个字母。例如,如果有人输入 dog,它只会说 'd'。 我的猜测可能是 word[i] 的语法只包含第一个字母,但我不知道如何解决这个问题。 有谁知道如何解决这个问题?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define pause system("pause")
#define cls system("cls")
void getUserWords(int *counter, int words[]){
int i;
printf("How many words would you like to enter into the cross word puzzle\n");
scanf("%i",&*counter);
for(i=0; i < *counter; i++){
printf("Please enter word #%i\n",i+1);
scanf("%s",&words[i]);
printf("The word %c will be added to the crossword puzzle\n",words[i]);
pause;
cls;
}//end for
printf("Your all your words have been successfully added to the crossword puzzle");
cls;
}//end getUserWords
【问题讨论】:
-
为什么
counter是一个函数参数,如果在函数中有 2 行你用用户的输入覆盖它 -
我会保留它的记录以便在另一个函数中再次使用:)
-
好的 - 也许最好通过 return 语句将计数传回
main,所以在主函数中你可以将这个函数的返回值传递给另一个函数 -
@TimCastelijns“你用用户的输入覆盖它”——这不会发生......只有计数器指向的值被改变,而不是计数器本身。这种方法基本上没有什么问题(除了它应该被称为 counterp,而不是 counter),尽管返回计数器值比传递指向它的指针更干净(除非有多个返回值)。
-
@JimBalter
only the value counter points to is changed这就是我想说的。我以为他想将 counter 的值作为参数传递,我不知道这只是为了跟踪它以便以后重用它