c编程scanf

hal*_*o_4 2 c morse-code

我得到了这个任务,这是我到目前为止所做的代码.这个代码只接受一个字母,它应该比字母更多,所以我可以输入一个单词,它将是莫尔斯电码

#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);
}
Run Code Online (Sandbox Code Playgroud)

P.P*_*.P. 6

您的scanf %c只需要一个字符.使用%s读取C-字符串:

scanf("%s", input);
Run Code Online (Sandbox Code Playgroud)

scanf()指针类型的参数.由于c-string名称是指向第一个元素的指针,因此不需要说address-of(&).

如果你只阅读一个字符,你需要使用&.

例如:

scanf("%c", &input[i]); // pass the address of ith location of array input.
Run Code Online (Sandbox Code Playgroud)