I want a cycle to save a N number of clients and print the total that they have spent

0

I want this cycle to be able to store the number of clients and also give discounts according to the amount of money spent by the client to give a discount of 30 percent if I spend more than 500 pesos As I'm starting to program in C, I do not know what I'm wrong about I hope you can help me thanks

#include<stdio.h>

int i=0,n;
float gc,ca;
int main(){
    printf("Escriba la cantidad de clientes");
    scanf("%d",&n);
    if (n>i){
        for(i=1;i>0;i++){
        printf("Escriba el monto gastado por cliente");
            scanf("%f",gc);
        ca = (gc * .30);
        printf(" total a pagar: .2f%",ca);


        }
    }return 1;
}
    
asked by TysonGolovkin 30.09.2017 в 06:21
source

1 answer

0

I have not finished understanding the reasons why @David has deleted his answer. I agree with practically everything he has said and what I have not let him know (I say it in case he reconsiders and decides to relive his response).

  

I want this for cycle to be able to store the number of clients

The first rule you should follow is to avoid the use of global variables ... they will not help you to better program or better understand what you are doing.

Good. Your code for this part is this:

int main(){
    printf("Escriba la cantidad de clientes");
    scanf("%d",&n);
    if (n>i){
        for(i=1;i>0;i++){

Which is too complicated for what you have to do:

  • Predict the number of clients
  • For each client to do something
  • If there are no clients, do nothing

That is:

int main(){
    int n;

    printf("Escriba la cantidad de clientes");
    scanf("%d",&n);
    for(int i=0;i<n;i++){ // Iteramos en el rango 0..n

And do not you need the if ? Absolutely. for already has a conditional that allows you to leave the loop and is evaluated from the first iteration. In the example I've put you, if n==0 and i==0 , then i<n will not be met and no iteration will be executed.

  

also can give discounts according to the amount of money spent by the client to grant a discount of 30 percent if you spend more than 500 pesos

Your code for this part is this:

printf("Escriba el monto gastado por cliente");
scanf("%f",gc);
ca = (gc * .30);
printf(" total a pagar: .2f%",ca);

Here you are applying the discount . In addition, the discount you apply is not 30% but 70%. x*0.3 gives you the amount to discount and you are using it as the final price.

Another error you have is that you are misconfiguring the last printf :

printf(" total a pagar: .2f%",ca);
//                         ^

That percentage must be at the beginning of the field ... not at the end. The percentage symbol is what printf uses to know that a substitution starts there.

The solution should come closer to this:

float gc = 0;
printf("Escriba el monto gastado por cliente");
scanf("%f",gc);
if( gc > 500.0 ) // Si se gasta más de 500 pesos...
  gc *= .7;      // ... se descuenta el 30% (100% - 30% = 70%)

printf(" total a pagar: %.2f", gc);

Note that the variable gc does not exist now. It is useless. It would only have reason to be if you had to indicate somewhere the discount applied:

float ga = 0, gc = 0;
printf("Escriba el monto gastado por cliente");
scanf("%f",gc);
if( gc > 500.0 ) // Si se gasta más de 500 pesos
  ga = gc * .3;  // Descuento del 50%

printf("A pagar: %.2f. Descuento: %.2f. Total %.2f", gc, ga, gc-ga);
    
answered by 02.10.2017 в 10:36