简体   繁体   English

我正在尝试建立虚拟商店(我刚刚开始学习c ++)

[英]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)如果要在变量中使用小数,则必须使用doublefloat类型(请参见此处的区别),而不是int In your case you can use float . 您可以使用float

5) Don't name your variables a , b or c , but use explicit names. 5)不要为变量abc命名,而应使用显式名称。 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.

相关问题 刚开始学习C++ - Just started learning C++ 我刚刚开始使用 c++,但我在hackerRank 中遇到此代码错误 - I have just started c++ but I'm getting error in hackerRank for this code 我对 C++ 很陌生,而且我一直在尝试制作循环 - I'm very new to C++ and I'm stuck trying to make a loop 我刚刚开始学习使用 C++ 的链表。 我正在尝试使用向量创建一个链表。 为什么这段代码有问题? - I have just started learning about Linked list using C++. I am trying to create a linked list using a vector. Why is the problem with this code? 我正在尝试在 c++ 中制作菜单,但我不明白为什么它不循环 - I'm trying to make a menu in c++ and I don't understand why it doesn't loop 如果我试图在C ++中创建队列,如何制作一定大小的队列? - If I'm trying to create a Queue in C++, how do I make the Queue of a certain size? 我将从学习 C/C++ 到学习 Java:这到底是什么? - I'm going from learning C/C++ to learning Java: What the heck is this? 我开始学习DSA,在c++写stack DS的代码时,遇到了这个错误 - I started learning DSA ,while writing code for stack DS in c++ , I came across this error 我正在尝试制作正则表达式阅读器 function。 C++ - I`m trying to make regular expression reader function. C++ 我正在尝试制作一个函数来初始化C ++中的2D数组 - I'm trying to make a function that initializes a 2D array in C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM