-4

I was creating the following code which is deducing a student's stress level from their usage of negative coping strategies and agreement with low self-image associated statements. Below is my code, and bolded are the parts I had problems with :

void statements (void);
    void printweightsstatements(void);
void coping (void);
void printweightscoping(void);
void advice ();
int cumweightstatement();
int cumweightcoping();

void main()
{
 statements ();
 coping ();
**if (int cumweightstatement + int cumweightcoping>=20)** 

 printf ("You are very stressed and at a high risk for depression. Please 
see a doctor or psychiaqtrist as soon as possible\n");
 else if (cumweightstatement + cumweightcoping>=15 && int cumweightstatement + int cumweightcoping<20)
printf ("You are quite stressed and at a moderate risk for depression. Please see your school guidance counselor\n");
else if (cumweightstatement + cumweightcoping<=14)
printf ("You are mildly stressed and at a low risk for depression, but you could still benefit from using positive coping strategies\n")
}

void printweightsstatements()
{
 printf("1:Strongly disagree\n");
 printf("2:Disagree\n");
 printf("3:Neither agree nor disagree\n");
 printf("4:Agree\n");
printf("5:Strongly agree\n");

}


void statements(void)

{
 int weight1;
 int weight2;
 int weight3;
 int weight4;

 printf ("How much do you agree with the following statements?\n");

 printf ("Statement 1: I have trouble concentrating on my homework for 1 hour straight\n");
 printweightsstatements;
 printf("1:Strongly disagree\n");
 printf("2:Disagree\n");
 printf("3:Neither agree nor disagree\n");
 printf("4:Agree\n");
 printf("5:Strongly agree\n");

printf ("Statement 2: I do not have much control over events in my life\n");
printweightsstatements;
printf("1:Strongly disagree\n");
printf("2:Disagree\n");
printf("3:Neither agree nor disagree\n");
printf("4:Agree\n");
printf("5:Strongly agree\n");
scanf("%d", &weight2);
printf ("Statement 3: During this school year, I have been stressed a lot\n");
printweightsstatements;
printf("1:Strongly disagree\n");
printf("2:Disagree\n");
printf("3:Neither agree nor disagree\n");
printf("4:Agree\n");
printf("5:Strongly agree\n");
scanf("%d", &weight3);
printf ("Statement 4: Although I try very hard, there is always some schoolwork too difficult for me\n");
printf("1:Strongly disagree\n");
printf("2:Disagree\n");
printf("3:Neither agree nor disagree\n");
printf("4:Agree\n");
printf("5:Strongly agree\n");
scanf("%d", &weight4);
printweightsstatements;

int cumweightstatement=weight1+weight2+weight3+weight4;
}


void coping (void);
{
  int weight1;
  int weight2;
  int weight3;
  int weight4;
  int weight5;

printf ("How often do you use each of the following coping strategies?\n");
printf ("Strategy 1: Negative self-talk (for example, I can’t do this,this is too hard for me, I’ll never get good at this)\n");
printweightscoping;
scanf("%d", &weight1);
printf ("Strategy 2: Overusing Internet/social media/phone for non-school-related activities (over 1 hour/day)\n");
printweightscoping;
scanf("%d", &weight2);
printf ("Strategy 3: Extended sleep or naps (above 8-9 hours of regular sleep)\n");
printweightscoping;
scanf("%d", &weight3);
printf ("Strategy 4:Procrastination or avoiding the task (leaving your schoolwork to the last minute, or not doing it at all)\n");
printweightscoping;
scanf("%d", &weight4);
printf ("Strategy 5:Denial (ignoring your problems and keep doing things the same way)\n");
printweightscoping;
scanf("%d", &weight5);
int cumweightcoping=weight1+weight2+weight3+weight4;
}

void printweightscoping(void);
{
 printf("1:Never\n");
 printf("2:Rarely(less than weekly) \n");
 printf("3:Sometimes(once a week)\n");
 printf("4:Often (several times a week)\n");
 printf("5:Always (about daily")
}

This is the log. Do you have any idea how to fix these errors?

main.c: In function 'main':
main.c:23:9: error: expected expression before 'int'

main.c:25:33: error: invalid operands to binary + (have 'int (*)()' and 'int (*)
main.c:25:58: error: expected expression before 'int'
                                                      ^
main.c:27:33: error: invalid operands to binary + (have 'int (*)()' and 'int (*)
 expected identifier or '(' before '{' token
jsheeran
  • 2,912
  • 2
  • 17
  • 32
SK _
  • 1
  • 1
  • 1
    your `int` should be `(int)`. The first is a type name (which you cannot have in an executable statement), the second is a type cast. And you don't need the casts because the functions already return `int`. However, you do need `()` after the function names to make the function calls. – Paul Ogilvie Apr 25 '18 at 14:15
  • 2
    What are you trying to do with `if (int cumweightstatement + int cumweightcoping>=20)`? That statement is nowhere near correct C code. If you're trying to call them as functions, just how do you call `printf()`? – Andrew Henle Apr 25 '18 at 14:15
  • `else if (cumweightstatement + cumweightcoping>=15 && int cumweightstatement + int cumweightcoping<20)`. This doesn't make any sense; `cumweightstatement` and `cumweightcoping` are functions (did you mean to call each function like `cumweightstatement()`?) and you can't declare a new `int` inside the condition (did you mean to cast another variable like `(int)x`?). – 0x5453 Apr 25 '18 at 14:15
  • You should edit your code to provide [mcve] - make some cleaning, but what I noticed `void printweightscoping(void); {` if that should be function then it should be like `void printweightscoping(){...}` – xxxvodnikxxx Apr 26 '18 at 08:24
  • `void main()` is incorrect, See: [C11 Standard §5.1.2.2.1 Program startup p1 (draft n1570)](http://port70.net/~nsz/c/c11/n1570.html#5.1.2.2.1p1). See also: [See What should main() return in C and C++?](http://stackoverflow.com/questions/204476/) – David C. Rankin Apr 26 '18 at 08:24

1 Answers1

-1

a ';' is missing at the end of the line

    printf ("You are mildly stressed and at a low risk for depression, but you could still benefit from using positive coping strategies\n")

and as said in the comments, you have some strange declaration in some lines

    else if (cumweightstatement + cumweightcoping>=15 && int cumweightstatement + int cumweightcoping<20)

if you want to cast your variables as int, use

    (int)your_variable

But you declare cumweightstatement and cumweightcoping as function, not simple variable...

    int cumweightstatement();
    int cumweightcoping();

The name of a function is its address in memory, that explain why your compiler complain about adding some pointer (invalid operands to binary + (have 'int (*)()' and 'int (*)()')

EDIT: You have many mistakes in your code, confusion between variables you declare as functions: int cumweightstatement(); should be int cumweightstatement; and some functions calls that are not correctly done, like printweightsstatements; which should be printweightsstatements();

Declaring variables outside a function make it globale: you can reach it from any function in the file. Declaring a variable in a function makes it locale: you can NOT reach it out of its scope. i.e.: when you declare, for example in void statements(void):

    int cumweightstatement=weight1+weight2+weight3+weight4;

you declare a locale variable cumweightstatement and you affect to it the sum of each weight. But you will lose it as soon as you get out of the function.

The easiest, starting from the code you have, would be to use globale variables, what you probably try to do with the declaration

    int cumweightstatement();

but, once again, this creates a function, not a globale variable.

This link https://www.codingunit.com/c-tutorial-functions-and-global-local-variables will give you some bases to declare functions and locale/globale variables

CimCim
  • 48
  • 5
  • I tried that but it said "'cumweightstatement' undeclared (first use in this function)" – SK _ Apr 26 '18 at 00:49
  • My function printweightscoping isn't working though printweightsstatements is: – SK _ Apr 26 '18 at 21:10
  • void printweightsstatements() { printf("1:Strongly disagree\n"); printf("2:Disagree\n"); printf("3:Neither agree nor disagree\n"); printf("4:Agree\n"); printf("5:Strongly agree\n"); } – SK _ Apr 26 '18 at 21:12
  • and void printweightscoping() { printf("1:Never\n"); printf("2:Rarely(less than weekly) \n"); printf("3:Sometimes(once a week)\n"); printf("4:Often (several times a week)\n"); printf("5:Always (about daily)\n"); } – SK _ Apr 26 '18 at 21:13
  • I've changed how I declare cumweightstatement and cumweightcoping but it doesn't make a difference – SK _ Apr 26 '18 at 21:48
  • @SK_ `void coping (void);` and `void printweightscoping(void);` does not work because you have a **";"** at the end of the function declaration. – CimCim Apr 27 '18 at 08:05