[英]I'm trying to make a virtual store (I just started learning c++)
so I'm trying to make some kind of virtual store just to test what I can do, I used the following strings: sProdus(sProduct),sPretFinal(sFinalPrice),sCantitate(sQuantity) 所以我试图建立某种虚拟商店只是为了测试我可以做什么,我使用了以下字符串:sProdus(sProduct),sPretFinal(sFinalPrice),sCantitate(sQuantity)
The problem is that I can't do math with strings, and when I change them to int variables the if statements will not work. 问题是我无法使用字符串进行数学运算,而当我将它们更改为int变量时,if语句将不起作用。 I tried removing the " , changing the == into =.
我尝试删除“”,将==更改为=。
I don't know what else I could do, could you tell me? 我不知道还能做什么,你能告诉我吗?
(When I run the following code and enter the quantity, it will always say 'Final Price' and then it shows nothing) (当我运行以下代码并输入数量时,它将始终显示“最终价格”,然后什么也不显示)
#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;
}
}
I would really love if someone could help me on this :) I hope I was specific enough. 如果有人可以帮助我,我真的很乐意:)我希望我足够具体。 :) I am using CodeBlocks btw.
:)我正在使用CodeBlocks btw。
You have many errors in your code... 您的代码中有很多错误...
1) CodeBlocks is an IDE , not a programming language. 1)CodeBlocks是一种IDE ,而不是一种编程语言。 The program you post is in c++ .
您发布的程序是c ++ 。
2) =
is an assignment operator and ==
is a relational operator (see c++ operators ). 2)
=
是赋值运算符,而==
是关系运算符(请参阅c ++运算符 )。 In your case, in the if
statement, you have to use ==
. 对于您的情况,在
if
语句中,必须使用==
。
3) When you declare multiple variables, you also have to end your line with a ;
3)声明多个变量时,还必须在行末尾添加
;
like the others (see declare multiple variable ). 像其他的一样(请参阅声明多个变量 )。
4) If you want to have decimals in your variable, you have to use the double
or float
types (see here for the difference), not int
. 4)如果要在变量中使用小数,则必须使用
double
或float
类型(请参见此处的区别),而不是int
。 In your case you can use float
. 您可以使用
float
。
5) Don't name your variables a
, b
or c
, but use explicit names. 5)不要为变量
a
, b
或c
命名,而应使用显式名称。 Please see this example . 请看这个例子 。
6) You declare string
variables and you do calculations with that. 6)您声明
string
变量,并以此进行计算。 The string class is used for store multiple characters, not number to do calculations. 字符串类用于存储多个字符,而不是数字来进行计算。
7) You declare your string variables without value : 7)您声明没有值的字符串变量:
string sProdus; // (sProduct)
string sCantitate; // (sQuantity)
string sPretFinal; // (sFinalPrice)
And just after you do calculations with these variables : 在使用这些变量进行计算之后:
sPretFinal=sProdus*sCantitate;
You can't use the content of a variable before you assign it a value. 在给变量赋值之前,不能使用变量的内容。
8) Finally, please indent your code. 8)最后,请缩进您的代码。 Artistic style can help you if you want to do this automatically.
如果您要自动执行此操作, 艺术风格可以为您提供帮助。
So I think before doing this program, you have to learn the basics of c++
. 因此,我认为在执行此程序之前,您必须学习
c++
的基础知识。 You have a lot of free tutorial on the net that can help you to learn c++. 您在网上有很多免费的教程,可以帮助您学习c ++。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.