1

I am new to C programming. I have to write a program, that

  1. asks the user to enter a number, then
  2. performs two mathematical operations and compares the results, then
  3. performs another operation and outputs the end result. After that,
  4. it needs to ask the user whether to continue, if user answers with "y" then the program needs to start over at step (1).

Whatever I try, the program either goes into an infinite loop, or it doesn't loop at all.

Here is one of my attempts:

#include <stdlib.h>
#include <stdio.h>

void main()
{
    double x;
    float a, b, Y;
    char z='y';
    char v;
    printf("My Name\n");
    while (z == 'y')
    {
        z = 'u';
        printf("Enter a number :\n");
            scanf_s(" %lf", &x);

            a = abs(x) / (1 - x * x);
            b = 3 * x - x / (1 + x * x);

            printf("a= %lf \n", a);
            printf("b= %lf \n", b);

            if (a <= b)
            {
                Y = (1 / a)*(b / 2 - 1 / 10);
            }
            else
            {
                Y = a * a + b * b * b;
            }
            printf("Y= %.3lf\n", Y);

            printf("If you want to continue enter y :\n");
            scanf_s(" %c", &z);
    }

}

And here is a block diagram for the program.

How must I change the program to make it loop as required?

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
Kalvis
  • 15
  • 6
  • Post all code in the question and format it as code so it's readable. And Wray specifically is the problem? – Carcigenicate Sep 08 '18 at 19:58
  • @Kalvis please [edit] your post to include the code instead of pasting it in the comments. It's very hard to read and follow like that. – Mureinik Sep 08 '18 at 19:59
  • @Mureinik i just uploaded the code. – Kalvis Sep 08 '18 at 20:06
  • 2
    [*Unlike scanf and wscanf, scanf_s and wscanf_s require the buffer size to be specified for all input parameters of type c, C, s, S, or [. The buffer size in characters is passed as an additional parameter immediately following the pointer to the buffer or variable.*](https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2008/w40768et(v=vs.90)) – r3mainer Sep 08 '18 at 20:11
  • https://stackoverflow.com/a/41199726/4454124 – mnistic Sep 08 '18 at 20:12
  • @user3121023 Thank you very much, that wast the problem!! :) – Kalvis Sep 08 '18 at 20:19
  • @squeamishossifrage Thank you very much, that wast the problem!! :) – Kalvis Sep 08 '18 at 20:19
  • @mnistic Thank you very much, that wast the problem!! :) – Kalvis Sep 08 '18 at 20:19
  • Please look at compiler warnings, would have told you. – Weather Vane Sep 08 '18 at 20:25
  • Asides: a) `abs(x)` should be `fabs(x)` and b) why are you mixing `float` and `double`? ***Never*** use `float` in 21st century, unless there are restrictions which say you must. Using `float` is like saying "I'll use my shopping car to drive to Antarctica". – Weather Vane Sep 08 '18 at 20:28

0 Answers0