error: return-statement with no value, in function returning ‘int’

Sep 7, 2019 at 4:29am
Hi everyone, I'm working on this code but I get an error. could someone help me how to fix it? I also put 0 after the return but I don't get any answer.


#define _USE_MATH_DEFINES
using namespace std;

#include <cmath>
#include <vector>
#include <stdlib.h>
#include <iostream>
#include <functional>
#include <math.h>
#include <stdio.h>



int main()
{
float mu;
int N;
double NF;
double phi;
double Force;
double a;
double b;
double c;
double tol;
double fa;
double fb;
double fc;


mu = .5;
N = 1;
phi = M_PI / 6;
Force = .448018475;
NF = .672027713;
a = 1.18;
b = 1.34;
c = (a + b) / 2;
tol = 10 ^ -4;
fa = NF * cos(a) + mu * NF * sin(a) - .5;
fb = NF * cos(b) + mu * NF * sin(b) - .5;
fc = NF * cos(c) + mu * NF * sin(c) - .5;
while (tol > abs(fc))
{
fa = NF * cos(a) + mu * NF * sin(a) - .5;
fb = NF * cos(b) + mu * NF * sin(b) - .5;
fc = NF * cos(c) + mu * NF * sin(c) - .5;
if ((fa * fc) < 0)
b = c;
else
a = c;
return
c = (a + b) / 2;
cout << fc << endl;
}
return;
};
Sep 7, 2019 at 5:39am
First, read this -> http://www.cplusplus.com/articles/jEywvCM9/

Second, your code with code tags.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#define _USE_MATH_DEFINES
using namespace std;

#include <cmath>
#include <vector>
#include <stdlib.h>
#include <iostream>
#include <functional>
#include <math.h>
#include <stdio.h>



int main()
{
  float mu;
  int N;
  double NF;
  double phi;
  double Force;
  double a;
  double b;
  double c;
  double tol;
  double fa;
  double fb;
  double fc;


  mu = .5;
  N = 1;
  phi = M_PI / 6;
  Force = .448018475;
  NF = .672027713;
  a = 1.18;
  b = 1.34;
  c = (a + b) / 2;
  tol = 10 ^ -4;
  fa = NF * cos(a) + mu * NF * sin(a) - .5;
  fb = NF * cos(b) + mu * NF * sin(b) - .5;
  fc = NF * cos(c) + mu * NF * sin(c) - .5;
  while (tol > abs(fc)) {
    fa = NF * cos(a) + mu * NF * sin(a) - .5;
    fb = NF * cos(b) + mu * NF * sin(b) - .5;
    fc = NF * cos(c) + mu * NF * sin(c) - .5;
    if ((fa * fc) < 0)
      b = c;
    else
      a = c;
    return c = (a + b) / 2;
    cout << fc << endl;
  }
  return 0;
};


Third, don't mix your C and C++ header files.
#include <cmath> and #include <math.h> is entirely pointless.

Fourth, the return 0 thing has NOTHING to do with the fact that the rest of your program doesn't produce output.
> return c = (a + b) / 2;
This return statement will always prevent the cout line on the next line from being seen.

Fifth, compile with more warnings.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ g++ -Wall -Wextra foo.cpp
foo.cpp: In function ‘int main()’:
foo.cpp:17:7: warning: variable ‘N’ set but not used [-Wunused-but-set-variable]
   int N;
       ^
foo.cpp:19:10: warning: variable ‘phi’ set but not used [-Wunused-but-set-variable]
   double phi;
          ^
foo.cpp:20:10: warning: variable ‘Force’ set but not used [-Wunused-but-set-variable]
   double Force;
          ^
foo.cpp:26:10: warning: variable ‘fb’ set but not used [-Wunused-but-set-variable]
   double fb;
          ^

It's for you to decide whether you actually intended to use those variables, or whether they're just useless in this program. Use them, or delete them.
Sep 7, 2019 at 9:49am
sixth, ^ is not exponentiation
tol = 1e-4;//10 ^ -4;
Topic archived. No new replies allowed.