0

I have this code, the problem is no matter what value I give to "m" the while keeps looping and I don't know why

Here is the code

#include <stdio.h>
#include <math.h>
#include <conio.h>

int main(void){
    float ur, N, h1, h2, h3, l1, l2, l3, uo, w, V, i, lc1, lc2, A1, A2, A3, A4, R1, R2, R3, R4, Req, fl, P;
    int m;
    uo = 4*M_PI*(pow(10, -7));
    printf("\n-- Inicio del Problema --");
    printf("\n-- Para conocer las variables revise la imagen en la parte trasera de la portada del disco --");
    while (m != 1){
        printf("\n-- Introduzca Permeabilidad magnetica relativa\t");
        scanf("%f", &ur);
        printf("\n-- Introduzca voltaje en volts\t");
        scanf("%f", &V);
        printf("\n-- Introduzca corriente en amperes\t");
        scanf("%f", &i);
        printf("\n-- Introduzca el número de espiras\t");
        scanf("%f", &N);
        printf("\n-- Introduzca las alturas en metros (h1, h2 y h3 separados por espacio)\t");
        scanf("%f %f %f", &h1, &h2, &h3);
        printf("\n-- Introduzca los largos en metros (l1, l2, y l3 separados por espacio)\t");
        scanf("%f %f %f", &l1, &l2, &l3);
        printf("\n-- Introduzca la anchura en metros (w)\t");
        scanf("%f", &w);
        printf("\nur = %f \t V = %f V \t I = %f A \t N = %f espiras \nh1 = %f m \t h2 = %f m \t h3 = %f m \nl1 = %f m \t l2 = %f m \t l3 = %f m \t w = %f m", ur, V, i, N, h1, h2, h3, l1, l2, l3, w);
        printf("\nHa introducido correctamente los datos (si = 1, no = 2)? \t");
        scanf("%d", m);     
    }
    lc1 = l2+(l1/2)+(l3/2);
    lc2 = h2+(h1/2)+(h3/2);
    A1 = h1*w;
    A2 = l3*w;
    A3 = h3*w;
    A4 = l1*w;
    R1 = lc1/(ur*uo*A1);
    R2 = lc2/(ur*uo*A2);
    R3 = lc1/(ur*uo*A3);
    R4 = lc2/(ur*uo*A4);
    Req = R1+R2+R3+R4;
    fl = (N*i)/Req;
    P = V*i;
    printf("\n-- Las áreas son: \nA1 = %f m^2 \nA2 = %f m^2 \nA3 = %f m^2 \nA4 = %f m^2", A1, A2, A3, A4);
    printf("\n-- Las reluctancias son: \nR1 = %f A*V/wb \nR2 = %f A*V/wb \nR3 = %f A*V/wb \nR4 = %f A*V/wb", R1, R2, R3, R4);
    printf("\n-- La reluctancia equivalente es: \nReq = %f A*V/wb", Req);
    printf("\n-- El flujo magnetomotriz es: \nF = %f wb", fl);
    printf("\n-- La potencia del sistema es: \nP = %f watts", P);
    getch(); 
    return 0;
}

I've tried changing to "m == 2", doing a do-while. No matter what I do is either breaks with any answer or doesn't brake with any answer.

I've also tried putting an if/break inside the loop, both while and do-while, but still has the same problems

If you point me to the problem I'd really appreciate it

Patricio
  • 9
  • 1
  • 6
    `scanf("%d", m);` --> `scanf("%d", &m);` also initialize `m` E.g `int m=0;` – BLUEPIXY Mar 16 '16 at 07:16
  • Use a `do...while` loop instead of a `while` loop. – Spikatrix Mar 16 '16 at 07:19
  • Voting to close as simple typo, as yet another "forgot & in scanf" question doesn't add anything of value to the site. I'd close as duplicate but can't be bothered to look for one. – Lundin Mar 16 '16 at 07:25

4 Answers4

5

Change:

scanf("%d", m);

to:

scanf("%d", &m);

The way it was, m wasn't changing (and it was writing to some unsafe address in memory).

Your compiler should have warning about this, so make sure you have enabled compiler warnings.

Also, you need to assign an initial value to m, probably zero, to force the loop to be entered the first time.

Tom Karzes
  • 22,815
  • 2
  • 22
  • 41
4

Try the following corrections :

1) Initialize m (to something different than 1, if you want the while loop to execute).

2) Change scanf("%d", m); to scanf("%d", &m); in order to read in the same variable you use in the condition.

Marievi
  • 4,951
  • 1
  • 16
  • 33
4

Two problems there:

  • You are using m uninitialized.
  • Last statement scanf("%d", m); should be scanf("%d", &m);. Note the & before m.

Any one of them alone will cause undefined behavior. But, a possible reason for the loop to be infinite is scanf("%d", m); storing the input to the address m not at the address of m. So, m can have indeterminate value which may not be equal to 1 and causing the expression m != 1 to be true always.


Suggested reading: What will happen if '&' is not put in a 'scanf' statement in C?.

Community
  • 1
  • 1
haccks
  • 104,019
  • 25
  • 176
  • 264
3

I think this line has problem

scanf("%d", m);

This will be set the value into address with value m instead of set value to m.

The correct should be

scanf("%d", &m);
Minh
  • 424
  • 3
  • 12