A simple program in C ++ is locked in the third variable

1

I try to create a program in C ++ to calculate a profit with certain taxes , and for some reason the program breaks when it reads the third variable. How do I solve this problem?

#include <stdio.h>
#include <windows.h>

using namespace std;

int NG, PG, PCe, Ganancia;

int main ()
{
    printf("Digite la cantidad que tiene: \n");
    scanf("%s", &NG);
    printf("Digite a que precio esta: \n");
    scanf("%s", &PG);
    printf("Digita a que precio esta cada unidad del producido: \n");
    scanf("%s", &PCe);
    Ganancia=((NG*5)*PC)-((NG*500)+(NG*PG));
    printf("Se tienen :\n\n\t %s unidades\n\n\t",NG);
    printf("El precio es: %s \n\n\t",PG);
    printf("Y el precio de la unidad de producido es: %s \n\n\t",PCe); 
    printf("Por lo que la ganancia sera de: %s",Ganancia);

    system("PAUSE");
    return 0;
}
    
asked by Shiro 17.05.2017 в 00:43
source

2 answers

3
  

For some reason the program breaks when it reads the third variable.

The weird thing is that it works even with the first one. You are using scanf (which is a utility from the libraries of , not

answered by 17.05.2017 в 10:10
1

You have several warnings in your code, which do not mean an error in itself.

First, '% s' expects an argument of type 'char *', but you pass it an 'int *'.

What is wrong is that you use PCs without having declared the variable; I understand that it is PG. And if it is not, you have to replace it with whatever it is, for example PCe, which is where you say it is locked. It's not that it "gets stuck", it's badly written.

I show you the compiled code correctly:

And the code here:

#include <stdio.h>

int NG, PG, PCe, Ganancia;

int main ()
{
    printf("Digite la cantidad que tiene: \n");
    scanf("%d", &NG);
    printf("Digite a que precio esta: \n");
    scanf("%d", &PG);
    printf("Digita a que precio esta cada unidad del producido: \n");
    scanf("%d", &PCe);
    Ganancia=((NG*5)*PG)-((NG*500)+(NG*PG));
    printf("Se tienen :\n\n\t %d unidades\n\n\t",NG);
    printf("El precio es: %d \n\n\t",PG);
    printf("Y el precio de la unidad de producido es: %d \n\n\t",PCe);
    printf("Por lo que la ganancia sera de: %d",Ganancia);
    return 0;
}

I do not enter to evaluate if the code itself is better or worse, but in this way it does not give you any error. Of course, if the code is c ++ you should use it, because as you have it you use C.

So that you can see the aspect of the program that you have made running, I add another capture:

    
answered by 17.05.2017 в 01:08