簡體   English   中英

c編程scanf

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM