为什么我的C程序不要求选择字符

时间:2018-03-30 22:56:41

标签: c

我正在为大学进行C作业。它会询问用户一个数字并将其转换为二进制或八进制。但我的所有代码都是它询问它们的数字,然后询问它们是否需要二进制或八进制然后终止。我想要它,以便他们应该能够选择二进制或八进制,然后它将运行代码。我将把代码粘贴到下面。

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

int main(){

    int mynumholder[100];
    int userinput;
    int i;
    char userchoice;

    printf("Enter the number that you want to be converted.");
    scanf("%d",&userinput);

    printf("Click B for binary and O for Octal");
    scanf(" %c",&userchoice);

    if (userchoice == "B"){

        for( i =0; userinput>0;i++){

            mynumholder[i] = userinput%2;
            userinput = userinput / 2;
        }

        printf("Now I will print the binary values");

        for(i =i-1; i >= 0;i--)
        {
            printf("%d",mynumholder[i]);
        }
    }else{
        if(userchoice == "O"){
            int o = 0;
            while(userinput > 0){
                mynumholder[o] = userinput%8;
                userinput = userinput / 8;
            }

            for(i = i - 1; i>= 0; i--){
                printf("%d\n", mynumholder[o]);
            }
        }
    }
    return 0;
}

2 个答案:

答案 0 :(得分:1)

双字引号(&#34;&#34;)表示字符串,单引号(&#39;&#39;)表示字符...

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

    int main(){

    int mynumholder[100], userinput, i;
    char userchoice;

    printf("Enter the number that you want to be converted: ");
    scanf("%d",&userinput);

    printf("Press B for binary and O for Octal: ");
    scanf(" %c",&userchoice);

    if (userchoice == 'B' || userchoice == 'b') 
    {
         for(i = 0; userinput > 0; ++i)
         {
             mynumholder[i] = userinput%2;
             userinput = userinput / 2;
          }

          printf("Now I will print the binary values: ");

          for(i = i - 1; i >= 0; --i) printf("%d", mynumholder[i]);
    }
    else
    {
        if(userchoice == 'O' || userchoice == 'o')
        {
            int o = 0;
            while(userinput > 0)
            {
                mynumholder[o] = userinput%8;
                userinput = userinput / 8;
            }

            for(i = i - 1; i>= 0; i--) printf("%d\n", mynumholder[o]);
        }
    }
    return 0;
    }

答案 1 :(得分:1)

几个问题:

  1. 您需要与char进行比较,而不是字符串。 if (userChoice == 'B')

  2. 您没有在八进制案例的o循环中递增while

  3. 八进制案例中的for循环使用的是i,而不是o

  4. 以下是更正后的代码:

    #include <stdio.h>
    #include <stdlib.h>
    
    int main(){
    
        int mynumholder[100];
        int userinput;
        int i;
        char userchoice;
    
        printf("Enter the number that you want to be converted.");
        scanf("%d",&userinput);
    
        printf("Click B for binary and O for Octal");
        scanf(" %c",&userchoice);
    
        if (userchoice == 'B'){
    
            for( i =0; userinput>0;i++){
    
                mynumholder[i] = userinput%2;
                userinput = userinput / 2;
            }
    
            printf("Now I will print the binary values");
    
            for(i =i-1; i >= 0;i--)
            {
                printf("%d",mynumholder[i]);
            }
        }else{
            if(userchoice == 'O'){
                int o = 0;
                while(userinput > 0){
                    mynumholder[o++] = userinput%8;
                    userinput = userinput / 8;
                }
    
                for(o = o - 1; o>= 0; o--){
                    printf("%d\n", mynumholder[o]);
                }
            }
        }
        return 0;
    }