vitor horta
vitor horta

Reputation: 1

How do I make a C program restart with one key-press in case i want to run it again or if i put the wrong input

I am writing a c program that calculates the root of a function and sometimes I put the wrong numbers ( like 3.333 instead of 33.333) and I don't want to run or exit the program if I can just restart/reload from the top. It's also a way to run the program multiple times. kill 2 birds with one stone. I am Portuguese, so sorry for the not-so-good English.

#include <stdio.h>
#include <math.h>
float main()
{



    char let = 'a';
    int b;
    float x0, erro, gx, x1,dif, gxlinha, absgxlinha;
    int it,val,i, exp, numero, e;
    it=0;
    int num = 0;

    printf("Valor de x0 = ");
    scanf("%f", &x0);

    printf("Valor do erro = ");
    scanf("%f", &erro);

    printf("Qual o valor do expoente maior =  ");
    scanf("%d", &exp);
    printf("\n");
    val = (exp + 1);


    printf("funcao do tipo:  \n\n    ");

    e = exp - 1;
    numero = exp;
 for(b=0;b<=e;b++)
 {
      printf("%cx^%d + ",let,numero);
      let++;
      numero--;
 }

 printf("%cx^%d",let,numero);
 printf("\n\n");


    float a[val];
    printf("Introduza os %d numeros da funcao, de acordo com os valores pedidos: \n",val); // ex.: ax^2 + bx^1 + cx^0 itroduzir pela ordem c, b, a
    printf("\n");
  for( i=0; i<val; i++ )
  {
  printf ("%c = ",let);
  let--;
  scanf("%f", &a[i] );
  gx = gx + (a[i])*(pow((x0),num));
  num++;
  }
  num = 1;

  for (i=1; i<val; i++)
  {
    gxlinha = gxlinha + ((a[i]*num)*(pow((x0),num)));
    num ++;
  }
  absgxlinha = (sqrt(pow(gxlinha,2)));
  if ( absgxlinha < 1)

   {
       printf ("\ng(x0)' < 1 logo a funcao tem pelo menos uma raiz\n");
       x1=gx;
    dif = (sqrt(pow((x1-x0),2)));
    printf("\n| Iter.| Val de x0| g(x0)=x1 |   x1-x0  |\n|      |          |          |          |\n|  %d   | %f | %f | %f |  ",it,x0,x1,dif);

 while (sqrt(pow((x1-x0),2))>=erro)
   {
      x0 = x1;
      gx = 0;
      num = 0;

      for( i=0; i<val; i++ )
  {
  gx = gx + (a[i])*(pow((x0),num));
  num++;
  }

      x1=gx;
      dif = (sqrt(pow((x1-x0),2)));
      it++;
      printf(">= %f\n|  %d   | %f | %f | %f |  ",erro,it,x0,x1,dif);

   }


   printf("<  %f \n\n",erro);
   printf("A raiz com erro inferior a %f = %f",erro,x1);
   printf("\n\n\n\n");
   }

else
{
    printf ("\ng(x0)' > 1 logo a funcao nao tem raizes\n");
}


}


where are some inputs in order

0.8 , 0.000005 , 3 , 0.333333 , 0 , 0 , -0.333333

Upvotes: 0

Views: 96

Answers (2)

user12025770
user12025770

Reputation: 97

You need a loop. For example:

for (;;) {
if (// some condition) {
    break;
} else {
    //your code
  }
}

Upvotes: 0

Jack Lilhammers
Jack Lilhammers

Reputation: 1247

As others have already said, you need a loop, maybe inside a dedicated function.
This is just an example:

while(1)
{
    float x;
    scanf(" %f", &x);

    if (x < 0) // or any other condtion you want
        break;

    // your code
}

Upvotes: 1

Related Questions