-1

I am writing a code where user inputs starting value, maximum value and step value. I calculate the results properly and all the formulas are correct, but there is one problem. I am unable to stop the loop so that my answers are displayed properly. When I run, it just starts the loop and I get lots of answers spammed on screen.

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

float funqcia(float x, float y, float k);

int main()
{
    float y;
    float A;
    float B;
    float H;
    float x;
    float t;
    int e=0;
    int N=15;
    int k = 0;
    while(e==0)
    {
        printf("enter starting value here: ");
        scanf("%fl",&A);
        printf("enter maximum value: ");
        scanf("%fl", &B);
        printf("enter step: ");
        scanf("%fl", &H);
        if(B > A)
        {
            e=1;
        }
        else
        {
            printf("Sorry, stopping value should be higher than starting value.");
        }
    }
    while(e==1)
    {
        printf("%s","_________________________________________________");
        printf("%s","\n");
        printf("%s","|                                                |");
        printf("%s","\n");
        printf("%s","|       Results are shown below                  |");
        printf("%s","\n");
        printf("%s","|                                                |");
        printf("%s","\n");
        printf("%s","--------------------------------------------------");
        printf("%s","\n");
        t=0;
        /*for(int i=0;i<N;i++)
        {
            if(i>0)
            {
                if (B<=y)
                {
                break;
                }
                t=pow(2,i)*pow(H,i-1);
                x=x+t;
                printf("%s","| ");
                printf("%fl",x);
                printf("%s","         | ");
                funqcia(x,y);
                printf("%s","\n");
            }
            else
            {
                if (B<=y)
                {
                    break;
                }
                x=A;
                printf("%s","| ");
                printf("%fl",x);
                printf("%s","         | ");
                funqcia(x,y);
                printf("%s","\n");
            }
        }*/
        for (int i=0;i<N;i++)
        {
            t=pow(2,i)*pow(H,i-1);
            x=x+t;
            if (i == 0)
            {
                x=A;
            }
            y=funqcia(x,y,k);
            if (B<=k)
            {
                break;
            }
            else
            {
                printf("x=%fl\ty=%fl", x,y);
                //}
            }
        }
    }
}

float funqcia(float x, float y, float k)
{
    y=((3+exp(x-1))/(1+pow(x,2)*(3-tan(x))));
    if(isinf(y))
    {
        printf("this is infinity");
    }
    else
    {
        printf("y: %fl",y);
    }
    k=y;
    return k;
}
mch
  • 9,424
  • 2
  • 28
  • 42
Quto
  • 109
  • 7
  • 5
    Why would it stop if you are not modifying `e` in it? – Eugene Sh. Dec 09 '16 at 17:50
  • 1
    Compile with all warnings & debug info (`gcc -Wall -g`) then use the debugger (`gdb`). Read documentation of the functions that you are using (particularly of [printf](http://en.cppreference.com/w/c/io/fprintf) & [scanf](http://en.cppreference.com/w/c/io/fscanf)...). Read also about `fflush` (or end each `printf` control format string with a `\n`) – Basile Starynkevitch Dec 09 '16 at 17:51
  • 2
    Your fix-my-code question is off-topic here. – Basile Starynkevitch Dec 09 '16 at 17:52
  • It is not offtopic at all, I am begginer programmer asking for assistance. If you wish to assist, be my guest but you can just keep scrolling and skip my topic please @BasileStarynkevitch – Quto Dec 09 '16 at 18:09
  • @EugeneSh. What do you mean by modifying e in it? Can you please explain? Thanks. – Quto Dec 09 '16 at 18:09
  • @Quto You have a loop condition, right? Does it change within the loop anyhow? – Eugene Sh. Dec 09 '16 at 18:10
  • 1
    you should know that `float`s and [floating point math are not exact](http://stackoverflow.com/questions/588004/is-floating-point-math-broken), so when you operate on them, you are probably not getting the _exact_ answers you expect. I also don't like `if (B<=k)` since you're comparing an `int` and `float` there, but it may be ok based on conversion/promotion rules, which I don't know off the top of my head. The fact that nobody else has pointed it out may mean it's ok. But you don't change `e` in the loop, so you essentially have `while(true)`. Are you expecting to hit that `break`? – yano Dec 09 '16 at 19:29
  • `break` just breaks out of the loop it's in,, that won't break you out of the outer `while` loop. Since you're not changing `e` anywhere in the `while(e==1)` loop, you have an infinite loop. And for yours and everyone else's sake, your variables should have meaningful names. – yano Dec 09 '16 at 19:32
  • Have you thought about using meaningful variable names? – Ed Heal Dec 09 '16 at 19:56
  • 1
    You need to check the return values from`scanf` and take appropriate measures when you get unexpected input – Ed Heal Dec 09 '16 at 19:57

1 Answers1

0

You seem to have initialized variable e to 0 at the onset of your program. And you have a check while(e==0), somewhere when the B>A condition is true then e changes to 1 now. Next you have while(e==1) and some piece of code to display the calculations and results, here the value of e never changes from one to something else. As it is always 1,the while condition gives it a green signal to continue the loop till infinity and somehow messup your results on the screen.

robot_alien
  • 955
  • 2
  • 13
  • 30