[英]c programming scanf
我得到了这个任务,这是我到目前为止所做的代码。 这个代码只接受一个字母,它应该比字母更多,所以我可以输入一个单词,它将是莫尔斯电码
#include "stdafx.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
int _tmain(int argc, _TCHAR* argv[])
{
char input[80], str1[100];
fflush(stdin);
printf("Enter a phrase to be translated:\n");
scanf("%c", &input);
int j = 0;
for (int i = 0; i <= strlen(input); i++)
{
str1[j] = '\0';
switch(toupper(input[i]))
{
..................
}
j++;
}
printf("\nMorse is \n %s\n", str1);
fflush(stdout);
//printf("%s\n ",morse);
free(morse);
}
你的scanf有%c
,只需要一个字符。 使用%s
读取c字符串:
scanf("%s", input);
scanf()
参数是指针类型。 由于c字符串名称是指向第一个元素的指针,因此不需要说地址( &
)。
如果你只阅读一个字符,你需要使用&
。
例如:
scanf("%c", &input[i]); // pass the address of ith location of array input.
使用%s
而不是%c
读取字符串。 字符串也已经是指针,不需要获取其地址。 所以改变这个:
scanf("%c", &input);
成:
scanf("%s", input);
scanf("%c", &input);
会读取一个字符,你可能正在寻找scanf("%s", input);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.