简体   繁体   English

在printf(C)中显示字符串而不是数字

[英]Show string instead of numbers in printf (C)

I've got this code written in C that just on certain number displays a message 我已经用C编写了这段代码,只是在一定数量上显示一条消息

int main(void) {
int a, d;    
d = scanf("%d", &a);
switch(a){
    case 1:
        printf("ml' nob:\n%d\nQapla'\nnoH QapmeH wo' Qaw'lu'chugh yay chavbe'lu' 'ej wo' choqmeH may' DoHlu'chugh lujbe'lu'.\n", a);
        break;
    case 2:
        printf("ml' nob:\n%d\nQapla'\nQu' buSHa'chugh SuvwI', batlhHa' vangchugh, qoj matlhHa'chugh, pagh ghaH SuvwI''e'.\n",a);
        break;
    case 3:
        printf("ml' nob:\n%d\nQapla'\nqaStaHvIS wa' ram loS SaD Hugh SIjlaH qetbogh loD.\n",a);
        break;
    case 4:
        printf("ml' nob:\n%d\nQapla'\nHa'DIbaH DaSop 'e' DaHechbe'chugh yIHoHQo'.\n",a);
        break;
    case 5:
        printf("ml' nob:\n%d\nQapla'\nleghlaHchu'be'chugh mIn lo'laHbe' taj jej.\n",a);
        break;
    default:
        printf("ml' nob:\n%d\nluj\n", a);
        break;
}
return 0;}

But when i enter a string or char in input it gives me a lot of nonsense numbers. 但是,当我在输入中输入字符串或字符时,它给了我很多废话。 Is there a way to show the string that i input in default instead of the numbers. 有没有一种方法可以显示我默认输入的字符串而不是数字。

EDIT: input 编辑:输入

abc

Output 输出量

What i want : abc
What it shows: 3469312

Try this one..! 试试这个。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <stdio.h>
using namespace std;
int main(void) {
    int a, d;
    char str[10];
    scanf("%s", &str);

    char* err = NULL;
    a = strtol(str, &err,10);
    if (err==NULL)  //Conversion Error
        a = -1;

    switch((int)a){
    case 1:
        printf("ml' nob:\n%d\nQapla'\nnoH QapmeH wo' Qaw'lu'chugh yay chavbe'lu' 'ej wo' choqmeH may' DoHlu'chugh lujbe'lu'.\n", a);
        break;
    case 2:
        printf("ml' nob:\n%d\nQapla'\nQu' buSHa'chugh SuvwI', batlhHa' vangchugh, qoj matlhHa'chugh, pagh ghaH SuvwI''e'.\n",a);
        break;
    case 3:
        printf("ml' nob:\n%d\nQapla'\nqaStaHvIS wa' ram loS SaD Hugh SIjlaH qetbogh loD.\n",a);
        break;
    case 4:
        printf("ml' nob:\n%d\nQapla'\nHa'DIbaH DaSop 'e' DaHechbe'chugh yIHoHQo'.\n",a);
        break;
    case 5:
        printf("ml' nob:\n%d\nQapla'\nleghlaHchu'be'chugh mIn lo'laHbe' taj jej.\n",a);
        break;
    default:
        printf("ml' nob:\n%s\nluj\n", str);
        break;
    }
    return 0;}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM