简体   繁体   中英

equation solver in C segmentation fault

#include <stdio.h>

int main()
{
  printf("choose number");
  c();
}

c()
{
  printf("1. ax+b=0\n\n");
  printf("2. ax+by+c=0\n   dx+ey+f=0\n\n");
  int n;

  scanf("%d", &n);

  if (n > 3)
    wrong();
  if (n == 1)
    formula1();
  if (n == 2)
    formula2();
  if (n == 3)
    ;
  formula3();
}

wrong()
{
  printf("Please choose a number between 1 and 3.\n\n");
  c();
}

formula1()
{
  printf("ax+b=0\n");
  printf("Enter your values for a and b respectively, seperated by commas\n");
  float a, b, x;
  scanf("%f,%f,%f", &a, &b);
  x = -b / a;
  printf("x=-b/a\n");
  printf("=>x=%f", x);
  question();
}

formula2()
{
  printf("ax+by+c=0\n\ndx+ey+f=0\n");
  printf(
      "Enter your values for a, b, c, d ,e and f respectively, seperated by commas\n");
  float a, b, c, d, e, f, x, y;
  scanf("%f,%f,%f,%f,%f,%f", &a, &b, &c, &d, &e, &f);
  x = ((f * b) - (c * e)) / ((a * e) - (d * b));
  y = ((c * d) - (f * a)) / ((e * a) - (d * b));
  printf("=>x=%f", x);
  printf("\n\n");
  printf("=>y=%f", y);
  question();
}

question()
{
  char t;
  printf("\n\nanother equation?\ny/n?\n");
  if (t == 'y')
  {
    printf("\n\n\n\n\n");
    c();
  }
  else if (t != 'n')
    question();
}

I have this code, which in short solves 3 equations. When you select any choice it seems to run the question method multiple times then quits due to a segmentation fault: 11

Could someone please point out where I am going wrong. Any other help with my code would be greatly appreciated

Here is one problem:

scanf("%f,%f,%f",&a, &b);

Only two arguments are supplied for the three values.

没有question() scanf()类的输入函数,因此如果t偶然不是'y'或'n',您将得到无限递归,直到超过堆栈大小为止。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM