注册  找回密码

JBTALKS.CC


查看: 3331|回复: 13
打印 上一主题 下一主题

[C] error C2106: '=' : left operand must be l-value 如何解决?

[复制链接]

13

主题

0

好友

8388

积分

金鼎名嘴

Rank: 14Rank: 14Rank: 14Rank: 14Rank: 14

跳转到指定楼层
1#
发表于 2009-11-15 04:29 PM |只看该作者 |倒序浏览
各位好,我在做个assignment遇到这个问题error C2106: '=' : left operand must be l-value,以下是我的code。
我已经把有问题的Bold起来了,希望各位高手帮忙找出解决方案。
谢谢!
#include <stdio.h>
#include <time.h>

int main()
{
        int tbnum, pznum, drknum, pzq, drkq;
        char custname[175], pz[12], drk[10];
        const double pzpr = 8.50, drkpr = 3.20;
        double psub, dsub, ttch, ttpay, tax, paid, chg;
        struct tm * timeinfo;
        time_t lt;
       
        printf("\t--- Welcome to Pizza House Restaurant ---\n\n");
        // Show Pizza and Drinks Menu
        printf("PIZZA MENU\n 1.Pepperoni\n 2.Seafood\n 3.Vegetable\n 4.Hawaiian\n 5.Mixed Combo\n");
        printf("\nDRINKS MENU\n 1.Juice\n 2.Chocolate\n 3.Cocktail\n 4.Latte\n");
        // Prompt User Input Order Information
        printf("\nPlease enter table number\t: ");
        scanf("%d", &tbnum);
        fflush(stdin);
        printf("Please enter your name\t\t: ");
        gets(custname);
        printf("Enter choice of pizza\t\t: ");
        scanf("%d", &pznum);
        fflush(stdin);
        // If Else Statement limiting the pizza choice
        if (pznum > 5) {
                printf("ERROR\nWe do not have that on the menu, please re-enter your choice!\n");
                printf("Enter choice of pizza\t\t: ");
                scanf("%d", &pznum);
                fflush(stdin);
        }
        else { // Do Nothing
        }
        printf("Enter quantity of the pizza\t: ");
        scanf("%d", &pzq);
        fflush(stdin);
        printf("\nEnter choice of drink\t\t: ");
        scanf("%d", &drknum);
        fflush(stdin);
        // If Else Statement limiting the drink choice
        if (drknum > 4) {
                printf("ERROR\nWe do not have that on the menu, please re-enter your choice!\n");
                printf("Enter choice of drink\t\t: ");
                scanf("%d", &drknum);
                fflush(stdin);
        }
        else { // Do Nothing
        }
        printf("Enter quantity of the drink\t: ");
        scanf("%d", &drkq);
        fflush(stdin);
        // Show Order Summary
        printf("\nOrder Summary:\n--------------\n");
        time(&lt); // Get Date/Time
        timeinfo = localtime(&lt);
        printf("Date/Time\t: %s", asctime(timeinfo));
        printf("Table Number\t: %d\n", tbnum);
        printf("Customer Name\t: %s\n", custname);
        printf("\nItem\t\tUnit Price (RM)\t\tQuantity\tSub-Total (RM)\n");
        printf("-----\t\t---------------\t\t--------\t--------------\n");
        psub = pzpr * pzq;
        if (pznum == 1) {
                pz = "Pepperoni";
        }
        else { // Do Nothing
        }
        printf("%-s\t\t%.2lf\t\t   %d\t\t\t%.2lf\n", pz, pzpr, pzq, psub);
        dsub = drkpr * drkq;
        printf("%-s\t\t%.2lf\t\t   %d\t\t\t %.2lf\n", drk, drkpr, drkq, dsub);
        printf("\t\t\t\t\t\t\t--------------\n");
        ttch = psub + dsub;
        printf("\t\t\t\t\tTotal Charge:\t\t%.2lf\n", ttch);
        tax = ttch * 0.1;
        printf("\t\t\t\t\tService Tax(10%):\t %.2lf\n", tax);
        ttpay = ttch + tax;
        printf("\t\t\t\t\t\t\t==============\n\t\t\t\t\tTotal Payment:\t\t%.2lf\n\t\t\t\t\t\t\t==============\n", ttpay);
        // Prompt User Input on Cash Paid
        printf("\t\t\t\t\tPaid Cash:\tRM\t");
        scanf("%lf", &paid);
        fflush(stdin);
        chg = paid - ttpay;
        printf("\t\t\t\t\tCHANGE:\t\tRM\t%.2lf\n", chg);
        printf("\nALWAYS THE PREFERRED CHOICE!\n");
        printf("\tPizza House Restaurant (482123-0)\n\t53300 Setapak, Kuala Lumpur.\n");
        printf("\n%s, thank you for visiting Pizza House Restaurant (482123-0)!!\nHave a nice day. Please come again ~~!", custname);
       
        return 0;
}






收藏收藏0

13

主题

0

好友

8388

积分

金鼎名嘴

Rank: 14Rank: 14Rank: 14Rank: 14Rank: 14

2#
发表于 2009-11-15 04:35 PM |只看该作者
抱歉我的code没有用code包起来,因为我要把有问题的那行字Bold起来...






回复

使用道具 举报

7

主题

1

好友

5108

积分

一流名嘴

Rank: 12Rank: 12Rank: 12

3#
发表于 2009-11-15 08:03 PM |只看该作者
原帖由 ~Kai 于 2009-11-15 04:35 PM 发表
抱歉我的code没有用code包起来,因为我要把有问题的那行字Bold起来...


C 的 characters 和 String 不一樣, 不能直接 assign, 所以必須用 copy 的方式

strcpy(pz, " Pepperoni");






回复

使用道具 举报

13

主题

0

好友

8388

积分

金鼎名嘴

Rank: 14Rank: 14Rank: 14Rank: 14Rank: 14

4#
发表于 2009-11-15 08:34 PM |只看该作者
原帖由 Super-Tomato 于 2009-11-15 08:03 PM 发表


C 的 characters 和 String 不一樣, 不能直接 assign, 所以必須用 copy 的方式

strcpy(pz, " Pepperoni");

谢谢你的帮忙。

我还有一个问题,能不能Declare一个Array是没有限制的?
比如说 char sumthing[]; 这样的

我刚刚才发现另一个问题...
printf("\t\t\t\t\tService Tax(10%):\t %.2lf\n", tax);
这里面的 10% 那个 % 不会显示出来... 要怎样呢?

[ 本帖最后由 ~Kai 于 2009-11-15 09:11 PM 编辑 ]






回复

使用道具 举报

7

主题

1

好友

5108

积分

一流名嘴

Rank: 12Rank: 12Rank: 12

5#
发表于 2009-11-15 09:27 PM |只看该作者
原帖由 ~Kai 于 2009-11-15 08:34 PM 发表

谢谢你的帮忙。

我还有一个问题,能不能Declare一个Array是没有限制的?
比如说 char sumthing[]; 这样的

我刚刚才发现另一个问题...
printf("\t\t\t\t\tService Tax(10%):\t %.2lf\n", tax);
这里面 ...


1. char sumthing[] = "here is your text";


2. printf("\t\t\t\t\tService Tax(10%%):\t %.2lf\n", tax);






回复

使用道具 举报

13

主题

0

好友

8388

积分

金鼎名嘴

Rank: 14Rank: 14Rank: 14Rank: 14Rank: 14

6#
发表于 2009-11-15 09:41 PM |只看该作者
原帖由 Super-Tomato 于 2009-11-15 09:27 PM 发表


1. char sumthing[] = "here is your text";


2. printf("\t\t\t\t\tService Tax(10%%):\t %.2lf\n", tax);

哦... 明白了...
谢谢你哦!






回复

使用道具 举报

13

主题

0

好友

8388

积分

金鼎名嘴

Rank: 14Rank: 14Rank: 14Rank: 14Rank: 14

7#
发表于 2009-11-28 03:04 PM |只看该作者
  1. #include <stdio.h>
  2. #include <time.h>

  3. int main()
  4. {
  5.         int tbnum, pznum, drknum, pzq, drkq;
  6.         char custname[175], pz[12], drk[10]; // custname array based on World's Longest Name of a Person which has 174 characters
  7.         const double pzpr = 8.50, drkpr = 3.20;
  8.         double psub, dsub, ttch, ttpay, tax, paid, chg;
  9.         struct tm * timeinfo;
  10.         time_t lt;
  11.        
  12.         printf("\t--- Welcome to Pizza House Restaurant ---\n\n");
  13.         // Show Pizza and Drinks Menu
  14.         printf("PIZZA MENU\n 1.Pepperoni\n 2.Seafood\n 3.Vegetable\n 4.Hawaiian\n 5.Mixed Combo\n");
  15.         printf("\nDRINKS MENU\n 1.Juice\n 2.Chocolate\n 3.Cocktail\n 4.Latte\n");
  16.         // Prompt User Input on Order Information
  17.         printf("\nPlease enter table number\t: ");
  18.         scanf("%d", &tbnum);
  19.         fflush(stdin);
  20.         printf("Please enter your name\t\t: ");
  21.         gets(custname);
  22.         printf("Enter choice of pizza\t\t: ");
  23.         scanf("%d", &pznum);
  24.         fflush(stdin);
  25.         // If Statement limiting the pizza choice
  26.         if (pznum > 5) {
  27.                 printf("ERROR\nWe do not have that on the menu, please re-enter your choice!\n");
  28.                 printf("Enter choice of pizza\t\t: ");
  29.                 scanf("%d", &pznum);
  30.                 fflush(stdin);
  31.         }
  32.         else if (pznum < 1) {
  33.                 printf("ERROR\nWe do not have that on the menu, please re-enter your choice!\n");
  34.                 printf("Enter choice of pizza\t\t: ");
  35.                 scanf("%d", &pznum);
  36.                 fflush(stdin);
  37.         }
  38.         printf("Enter quantity of the pizza\t: ");
  39.         scanf("%d", &pzq);
  40.         fflush(stdin);
  41.         printf("\nEnter choice of drink\t\t: ");
  42.         scanf("%d", &drknum);
  43.         fflush(stdin);
  44.         // If Statement limiting the drink choice
  45.         if (drknum > 4 ) {
  46.                 printf("ERROR\nWe do not have that on the menu, please re-enter your choice!\n");
  47.                 printf("Enter choice of drink\t\t: ");
  48.                 scanf("%d", &drknum);
  49.                 fflush(stdin);
  50.         }
  51.         else if (drknum < 1 ) {
  52.                 printf("ERROR\nWe do not have that on the menu, please re-enter your choice!\n");
  53.                 printf("Enter choice of drink\t\t: ");
  54.                 scanf("%d", &drknum);
  55.                 fflush(stdin);
  56.         }
  57.         printf("Enter quantity of the drink\t: ");
  58.         scanf("%d", &drkq);
  59.         fflush(stdin);
  60.         // Show Order Summary
  61.         printf("\nOrder Summary:\n--------------\n");
  62.         time(&lt); // Get Date/Time
  63.         timeinfo = localtime(&lt);
  64.         printf("Date/Time\t: %s", asctime(timeinfo));
  65.         printf("Table Number\t: %d\n", tbnum);
  66.         printf("Customer Name\t: %s\n", custname);
  67.         printf("\nItem\t\tUnit Price (RM)\t\tQuantity\tSub-Total (RM)\n");
  68.         printf("-----\t\t---------------\t\t--------\t--------------\n");
  69.         psub = pzpr * pzq;
  70.         // If Statement identifying pizza type
  71.         if (pznum == 1) {
  72.                 strcpy(pz, "Pepperoni");
  73.         }
  74.         if (pznum == 2) {
  75.                 strcpy(pz, "Seafood");
  76.         }
  77.         if (pznum == 3) {
  78.                 strcpy(pz, "Vegetable");
  79.         }
  80.         if (pznum == 4) {
  81.                 strcpy(pz, "Hawaiian");
  82.         }
  83.         if (pznum == 5) {
  84.                 strcpy(pz, "Mixed Combo");
  85.         }
  86.         printf("%-s\t\t%.2lf\t\t   %d\t\t\t%.2lf\n", pz, pzpr, pzq, psub);
  87.         dsub = drkpr * drkq;
  88.         // If Statement identifying drink type
  89.         if (drknum == 1) {
  90.                 strcpy(drk, "Juice");
  91.         }
  92.         if (drknum == 2) {
  93.                 strcpy(drk, "Chocolate");
  94.         }
  95.         if (drknum == 3) {
  96.                 strcpy(drk, "Cocktail");
  97.         }
  98.         if (drknum == 4) {
  99.                 strcpy(drk, "Latte");
  100.         }
  101.         printf("%-s\t\t%.2lf\t\t   %d\t\t\t %.2lf\n", drk, drkpr, drkq, dsub);
  102.         printf("\t\t\t\t\t\t\t--------------\n");
  103.         ttch = psub + dsub;
  104.         printf("\t\t\t\t\tTotal Charge:\t\t%.2lf\n", ttch);
  105.         tax = ttch * 0.1;
  106.         printf("\t\t\t\t\tService Tax(10%%):\t %.2lf\n", tax);
  107.         ttpay = ttch + tax;
  108.         printf("\t\t\t\t\t\t\t==============\n\t\t\t\t\tTotal Payment:\t\t%.2lf\n\t\t\t\t\t\t\t==============\n", ttpay);
  109.         // Prompt User Input on Cash Paid
  110.         printf("\t\t\t\t\tPaid Cash:\tRM\t");
  111.         scanf("%lf", &paid);
  112.         fflush(stdin);
  113.         // Check Paid Amount, ensuring amount paid is more than total payment.
  114.         if (paid < ttpay) {
  115.         printf("ERROR\nAmount Paid is less than Total Payment, Please re-enter Paid Cash!\n");
  116.         printf("\t\t\t\t\tPaid Cash:\tRM\t");
  117.         scanf("%lf", &paid);
  118.         fflush(stdin);
  119.         }
  120.         chg = paid - ttpay;
  121.         printf("\t\t\t\t\tCHANGE:\t\tRM\t%.2lf\n", chg);
  122.         printf("\nALWAYS THE PREFERRED CHOICE!\n");
  123.         printf("\tPizza House Restaurant (482123-0)\n\t53300 Setapak, Kuala Lumpur.\n");
  124.         printf("\n%s, thank you for visiting Pizza House Restaurant (482123-0)!!\nHave a nice day. Please come again ~~!", custname);
  125.        
  126.         return 0;
  127. }
复制代码


这是我新update的code,在if那里我发现如果触发那个情况的时候只能发生一次。比如说:
  1. if (paid < ttpay) {
  2.         printf("ERROR\nAmount Paid is less than Total Payment, Please re-enter Paid Cash!\n");
  3.         printf("\t\t\t\t\tPaid Cash:\tRM\t");
  4.         scanf("%lf", &paid);
  5.         fflush(stdin);
  6.         }
复制代码

这段输入一个比Total还要少的数字就只能触发一次,第二次打少过Total的就照跑下去了。
我去Google了一下,发现可以用Loop,但又不怎么明白while的用法,能不能帮忙小弟指点迷津?
先谢谢了...






回复

使用道具 举报

7

主题

1

好友

5108

积分

一流名嘴

Rank: 12Rank: 12Rank: 12

8#
发表于 2009-11-28 05:15 PM |只看该作者
原帖由 ~Kai 于 2009-11-28 03:04 PM 发表
#include
#include

int main()
{
        int tbnum, pznum, drknum, pzq, drkq;
        char custname[175], pz[12], drk[10]; // custname array based on World's Longest Name of a Person which has 174 chara ...



while 就和 if 一樣只是需要條件值



  1. #include <stdio.h>

  2. int main()
  3. {
  4.         double ttpay, paid;

  5.         ttpay = 100;

  6.         while(paid < ttpay) {
  7.             printf("ERROR\nAmount Paid is less than Total Payment, Please re-enter Paid Cash!\n");
  8.             printf("\t\t\t\t\tPaid Cash:\tRM\t");
  9.             scanf("%lf", &paid);
  10.             fflush(stdin);
  11.         }

  12.         return 0;
  13. }
复制代码






回复

使用道具 举报

46

主题

6

好友

6456

积分

百变名嘴

Rank: 13Rank: 13Rank: 13Rank: 13

9#
发表于 2009-11-28 07:05 PM |只看该作者
原帖由 Super-Tomato 于 2009-11-15 09:27 PM 发表


1. char sumthing[] = "here is your text";


2. printf("\t\t\t\t\tService Tax(10%%):\t %.2lf\n", tax);



C 可以不必限制的哦
我以为它 规矩多多的。






回复

使用道具 举报

7

主题

1

好友

5108

积分

一流名嘴

Rank: 12Rank: 12Rank: 12

10#
发表于 2009-11-28 10:08 PM |只看该作者
原帖由 宅男-兜着走 于 2009-11-28 07:05 PM 发表



C 可以不必限制的哦
我以为它 规矩多多的。



你是指甚麼限制呢??






回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

JBTALKS.CC |联系我们 |隐私政策 |Share

GMT+8, 2024-4-29 02:59 AM , Processed in 0.123113 second(s), 27 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

Thanks to ImageShack for Free Image Hosting
Ultra High-speed web hosting powered by iCore Technology Sdn. Bhd.
本论坛言论纯属发表者个人意见,与本论坛立场无关
Copyright © 2003-2012 JBTALKS.CC All Rights Reserved
合作联盟网站:
JBTALKS 马来西亚中文论坛 | JBTALKS我的空间 | JBTALKS部落 | ICORE TECHNOLOGY SDN. BHD. | GXUnlimited-ECSHOP | TM UniFi | TM Streamyx | TM Fixed Line
www.jbtalks.cc | mobile.jbtalks.cc | www.jbtalks.com | www.jbtalks.my | www.icore.com.my | www.icorehosting.com | www.icorehosting.net | www.cttsite.com | www.icore.my | www.lpohchin.com
bbs.jbtalks.cc | bbs.jbtalks.com | bbs.jbtalks.my | bbs.jbtalks.com
回顶部