-1

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)

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. 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.

deltonio2
  • 1,719
  • 1
  • 23
  • 37
Blaze26
  • 201
  • 1
  • 3
  • 8

1 Answers1

5

You have many errors in your code...

1) CodeBlocks is an IDE, not a programming language. The program you post is in c++.

2) = is an assignment operator and == is a relational operator (see c++ operators). In your case, in the if statement, you have to use ==.

3) When you declare multiple variables, you also have to end your line with a ; 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. In your case you can use float.

5) Don't name your variables a, b or c, but use explicit names. Please see this example.

6) You declare string variables and you do calculations with that. The string class is used for store multiple characters, not number to do calculations.

7) You declare your string variables without value :

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. 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++. You have a lot of free tutorial on the net that can help you to learn c++.

Community
  • 1
  • 1
deltonio2
  • 1,719
  • 1
  • 23
  • 37