Calculate Discount c++

I have two price and discount variables.

How do I calculate the discount of this product with the variables I have.


#include <iostream>
#include <locale>
using namespace std;



int main()
{
setlocale( LC_ALL, "portuguese");



string client = "João";
int age = 27;
char sex = 'M';
string product = "Memória RAM 16 GB";
float price = 40.32F;
float disccount = 5.2F;
float final prince =


printf ("Nome do Cliente\t\t%s\nIdade\t\t\t%i\nSexo\t\t\t%c\nNome do Produto\t\t%s\nPreço\t\t\t%.2f\nDesconto\t\t%.2f\n", client.c_str(), age, sex, produt.c_str(), price, disccount);


system("pause");

}



Best Regards

Why do you want to calculate discount if you have it already ?
Looks more you want to calculate the final price.
final_price = price - discount
Assuming that disccount is the percentage discount, and that the TWO words "final prince" is meant to be the SINGLE word variable name "final_price", and assumign that you don't want to calculate the discount but actually want to calculate the final price:

float final_price = price * (1 - (disccount / 100.0));

If disccount isn't a percentage discount, then not like that.
Last edited on
Give me a error when i print

#include <iostream>
#include <locale>
using namespace std;



int main()
{
setlocale( LC_ALL, "portuguese");



string cliente = "João";
int idade = 27;
char sexo = 'M';
string produto = "Memória RAM 16 GB";
float preco = 40.32F;
float desconto = 5.2F;
float precoFinal =


printf ("Nome do Cliente\t\t%s\nIdade\t\t\t%i\nSexo\t\t\t%c\nNome do Produto\t\t%s\nPreço\t\t\t%.2f\nDesconto\t\t%.2f\nPreço Final\t\\t%.2f\n", cliente.c_str(), idade, sexo, produto.c_str(), preco, desconto, precoFinal = preco * (1 - (desconto / 100.0));

system("pause");

}

||=== Build: Debug in teste 3 (compiler: GNU GCC Compiler) ===|
/home/saulo/Documentos/teste 3/main.cpp||In function ‘int main()’:|
/home/saulo/Documentos/teste 3/main.cpp|22|error: expected ‘)’ before ‘;’ token|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 2 second(s)) ===|


Read the error.

expected ‘)’ before ‘;’ token


expected ‘)’ before ‘;’


cpp|22|error: expected ‘)’ before ‘;’ token|


22

Line 22:
printf ("Nome do Cliente\t\t%s\nIdade\t\t\t%i\nSexo\t\t\t%c\nNome do Produto\t\t%s\nPreço\t\t\t%.2f\nDesconto\t\t%.2f\nPreço Final\t\\t%.2f\n", cliente.c_str(), idade, sexo, produto.c_str(), preco, desconto, precoFinal = preco * (1 - (desconto / 100.0));

expected ‘)’ before ‘;’


You are missing a ) .

1
2
3
4
5
6
7
8
9
10
printf (
        "Nome do Cliente\t\t%s\nIdade\t\t\t%i\nSexo\t\t\t%c\nNome do Produto\t\t%s\nPreço\t\t\t%.2f\nDesconto\t\t%.2f\nPreço Final\t\\t%.2f\n", 
        cliente.c_str(), 
        idade, 
        sexo, 
        produto.c_str(), 
        preco, 
        desconto, 
        precoFinal = preco * (1 - (desconto / 100.0))
          ; 


Where is the ) to match the ( right after printf ?


Also, look at this line of code:
float precoFinal =

Do you see anything wrong with this line of code?

Last edited on
 
float precoFinal =


as you are not initialising precoFinal here, you don't need the = but do need the terminating;

 
float precoFinal;


Also the printf() statement is missing a final ) before the ; The number of ( and ) in a statement must be the same.

Note you are calculating precoFinal and setting its value in the printf() statement. Whilst this is legal syntax (after the missing final ) is inserted), it probably isn't the best practice. What would probably be expected is to initialise precoFinal when it is defined and just use the variable name in the printf() like the others.

1
2
3
float precoFinal = preco * (1 - (desconto / 100.0));
...
printf ("Nome do Cliente\t\t%s\nIdade\t\t\t%i\nSexo\t\t\t%c\nNome do Produto\t\t%s\nPreço\t\t\t%.2f\nDesconto\t\t%.2f\nPreço Final\t\\t%.2f\n", cliente.c_str(), idade, sexo, produto.c_str(), preco, desconto, precoFinal);


Last edited on
You guys managed to solve my problem. Thank you all.

Topic archived. No new replies allowed.