【问题标题】:I'm trying to make a virtual store (I just started learning c++)我正在尝试做一个虚拟商店(我刚开始学习 C++)
【发布时间】:2014-12-15 13:12:37
【问题描述】:

所以我尝试制作某种虚拟商店只是为了测试我能做什么,我使用了以下字符串:sProdus(sProduct),sPretFinal(sFinalPrice),sCantitate(sQuantity)

问题是我不能用字符串做数学运算,当我将它们更改为 int 变量时,if 语句将不起作用。我尝试删除 " ,将 == 更改为 =。

我不知道我还能做什么,你能告诉我吗?

(当我运行以下代码并输入数量时,它总是会说“最终价格”,然后什么都不显示)

#include <string>
#include <iostream>

using namespace std;

int main()
{

 int a=29999,b=2,c=2.5,

string sProdus;    // (sProduct)
string sCantitate;   // (sQuantity)
string sPretFinal;   // (sFinalPrice)

sPretFinal=sProdus*sCantitate;

cout <<"Bun venit in magazinul lui Bogdan, ce doriti sa cumparati?";
cout<<endl;
cout<<endl;
cout <<"Puteti cumpara:televizor Phillips 1920x1280 (a), mere(b), banane(b)";
cout<<endl;
cout<<endl;
cout <<"Apasati litera corespunzatoare produsului pentru a vedea pretul acestuia";
cout<<endl;
cout<<endl;

cout<<"Produsul ales: ";cin>> sProdus;




if(sProdus=a){cout<<"Pretul televizorului Phillips 1920x1280: 29,999 RON";
cout<<endl;
cout<<endl;
cout<<"Cantitatea: ";cin>>sCantitate;

cout<<"Pretul final este:"<<sPretFinal;
}


if(sProdus=b){cout<<"Pretul unui mar: 2 RON";
cout<<endl;
cout<<endl;
cout<<"Cantitatea: ";cin>>sCantitate;

cout<<"Pretul final este:";sPretFinal;
}


if(sProdus=c){cout<<"Pretul unei banane:2.50 RON";
cout<<endl;
cout<<endl;
cout<<"Cantitatea: ";cin;sCantitate;

cout<<"Pretul final este:";sPretFinal;
}
if(sProdus !=c && sProdus !=a && sProdus !=b)
{
   cout<<"Ne cerem scuze, dar acest produs nu exista in magazin";
   cout<<endl;
}
}

如果有人能在这方面帮助我,我真的很高兴 :) 我希望我足够具体。 :) 顺便说一句,我正在使用 CodeBlocks。

【问题讨论】:

    标签: c++ codeblocks


    【解决方案1】:

    您的代码中有很多错误...

    1) CodeBlocks 是IDE,而不是编程语言。您发布的程序在c++。

    2) = 是赋值运算符,== 是关系运算符(请参阅c++ operators)。在您的情况下,在if 语句中,您必须使用==。

    3) 当您声明多个变量时,您还必须像其他人一样以; 结束您的行(参见declare multiple variable)。

    4) 如果要在变量中包含小数,则必须使用 double 或 float 类型(请参阅 here 了解区别),而不是 int。在您的情况下,您可以使用float。

    5) 不要将变量命名为 a、b 或 c,而是使用明确的名称。请参阅this example。

    6) 你声明了string 变量并用它进行计算。 string 类用于存储多个字符,而不是用于计算的数字。

    7) 你声明你的字符串变量没有值:

    string sProdus;    // (sProduct)
    string sCantitate;   // (sQuantity)
    string sPretFinal;   // (sFinalPrice)
    

    在您使用这些变量进行计算之后:

    sPretFinal=sProdus*sCantitate;
    

    在赋值之前,不能使用变量的内容。

    8) 最后,请缩进您的代码。如果您想自动执行此操作,Artistic style 可以为您提供帮助。

    所以我认为在做这个程序之前,你必须学习c++ 的基础知识。网上有很多免费教程可以帮助你学习c++。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-26
      • 2023-01-20
      • 1970-01-01
      • 2023-02-21
      • 1970-01-01
      • 2018-07-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多