【问题标题】:I'm getting "the error expression must be a modifiable lvalue" in C我在 C 中得到“错误表达式必须是可修改的左值”
【发布时间】:2015-11-08 08:56:05
【问题描述】:

已经搞砸了一段时间,似乎可以处理它。有人看到我看不到的东西吗? Char 在嵌套 if 语句的最底部给了我主要问题 谢谢!

// This is a mix of psuedocode and actual C code 
#include "stdio.h" 
#include "conio.h" 
#include "stdlib.h" // rand is in here 
#include "time.h" // contains time structure 
#pragma warning (disable : 4996) // turn off stdio function warnings 


int main(void)
{
time_t t; // a time object for rand seed 

// seed random number generator 
srand((unsigned)time(&t));

// MAIN PROCESSION LOOP 

char ch1;
char ch2;
int get;
int chOK = 0;
do {
    // Get human's move 
        printf("Hey it's your turn!\n choose rock,");
printf(" paper, or scissors and see if you can be me young padawan");
    // character input validation loop 

    do {
        scanf("%c", &ch1);

            switch(ch1)
        {
            case'R':
                break;
            case 'r':
                break;
            case 'P':
                break;
            case 'p':
                break;
            case 'S':
                break;
            case 's':
                break;
            case 27:
                chOK = 1;
                break;
            default :

                chOK = 0;
                    break;
        }
    } while (chOK = 1);

    // Convert input to uppercase 
    // 
    if (ch1 > 'Z') ch1 -= 32;

                // Get computer's move 
                ch2 = rand() % 3; // C code to get a rand # 
                switch (ch2)
                {
                case 0:
            ch2 = 'R';
                    break;
                case 1:
                    ch2 = 'S';
                    break;
                case 3:
                    ch2 = 'P';
                    break;

                }
                // Check who won 
                if (ch1 == 'R' && ch2 == 'R')
                    printf("Rock Rock Draw");
                        if (ch1 = 'R' && ch2 = 'P')
                            printf("Rock Paper Puter");
                        if (ch1 = 'R' && ch2 = 'S')
                            printf("Rock Scissors Human");
                            if (ch1 = 'S' && ch2 == 'P')
                                printf("Paper Paper Draw");
                            if (ch1 = 'P' && ch2 = 'R')
                                printf("Paper Rock Human");
                            if (ch1 = 'P' && ch2 = 'S')
                                printf("Paper Scissors Puter");
                                    if (ch1 = 'S' && ch2 == 'S')
                                        printf("Scissors Scissors Draw");
                                    if (ch1 = 'S' && ch2 = 'P')
                                        printf("Scissors Paper Human");
                                    if (ch1 = 'S' && ch2 = 'R')
                                        printf("Scissor Rock Puter");


                                      } while (ch1 != 27)

这些嵌套的 if 字符给我带来了问题。

【问题讨论】:

  • 您应该使用== 来比较chars,而不是=。一个等号就是赋值。
  • 耶啊啊啊!!!!!!非常感谢!
  • scanf("%c", &ch1); --> scanf(" %c", &ch1); 消耗左边的换行符\n 字符。

标签: c error-handling


【解决方案1】:

注意逻辑比较运算符('==')和做作运算符('=')。 也尽量避免 scanf("%C"),尽可能使用 string 或 int,或者使用 _getch();

// This is a mix of psuedocode and actual C code
#include "stdio.h"
#include "conio.h"
#include "stdlib.h" // rand is in here 
#include "time.h" // contains time structure 
#pragma warning (disable : 4996) // turn off stdio function warnings 


int main(void) {
    time_t t; // a time object for rand seed

// seed random number generator
    srand((unsigned)time(&t));

// MAIN PROCESSION LOOP

    char ch1;
    char ch2;
    int get;
    int chOK = 0;
    do {
        // Get human's move
        printf("\n\nHey it's your turn!\n\n");
        printf("choose rock,paper, or scissors\n");
        printf("and see if you can be me young padawan : ");
        // character input validation loop

        do {
            chOK = 1;
            ch1=_getch();


            switch(ch1) {
                case'R':
                case 'r':
                        ch1='R';
                    break;
                case 'P':
                case 'p':
                        ch1='P';
                    break;
                case 'S':
                case 's':
                        ch1='S';
                    break;
                case 27:
                    break;
            default :
                chOK = 0;
            }

        } while (!chOK);

        printf("%c\n",ch1);

        // Get computer's move
        ch2 = rand() % 3; // C code to get a rand #
        ch2="RSP"[ch2];

        // Check who won
        printf("\n [ ");
        if (ch1 == 'R'){
            if( ch2 == 'R')
                printf("Rock Rock Draw");
            if( ch2 == 'P')
                printf("Rock Paper Puter");
            if( ch2 == 'S')
                printf("Rock Scissors Human");
        }

        if (ch1 == 'P'){
            if( ch2 == 'R')
                printf("Paper Paper Draw");
            if( ch2 == 'P')
                printf("Paper Rock Human");
            if( ch2 == 'S')
                printf("Paper Scissors Puter");
        }

        if (ch1 == 'S'){
            if( ch2 == 'R')
                printf("Scissors Scissors Draw");
            if( ch2 == 'P')
                printf("Scissors Paper Human");
            if( ch2 == 'S')
                printf("Scissor Rock Puter");
        }
        printf(" ]\n");
    } while (ch1 != 27);

    printf("\nProgram terminated\n");
}

【讨论】:

    猜你喜欢
    • 2014-12-15
    • 2015-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 2014-12-15
    • 2014-12-20
    相关资源
    最近更新 更多