Need help with warning message

I get this warning
||=== Build: Debug in menu_1 (compiler: GNU GCC Compiler) ===|
C:\Users\Desktop\Projects\menu_1\main.c||In function 'menu':|
C:\Users\Desktop\Projects\menu_1\main.c|43|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|
||=== Build finished: 0 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
while I try to compile?
The program works fine, but I wanna know why do I get this warning and how to fix it..

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>

#define KEY_UP 72
#define KEY_DOWN 80
#define KEY_ENTER 13

#define ARRAYSIZE(sel) (sizeof(sel) / sizeof(sel[0]))

/** SELECT_END is the last highlighted choice in selections. */
#define SELECT_END 12

void clear();
void menu();
void selector(unsigned int select);
void hideCursor();

int main()
{
    hideCursor();
    menu();
    return 0;
}

void hideCursor()
{
    HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO info;
    info.dwSize = 100;
    info.bVisible = FALSE;
    SetConsoleCursorInfo(consoleHandle, &info);
}

void menu()
{
  int select = 0;
  int x;

  selector(select);

  while(x = getch())
  {
    if(x == 72)
    {
      select -= 2;
      if(select < 0)
        select = 0;
      selector(select);
    }
    else if(x == 80)
    {
      select += 2;
      if (select > SELECT_END)
        select = SELECT_END;
      selector(select);
    }
  }
}

void clear()
{
  system("cls");
}

void selector(unsigned int select)
{
  const char *selections[] =
  {
    "\n\n\n\n\n\t 1.   Input info          <",
    "\n\n\n\n\n\t 1. Input info",
    "\t 2.   Retrieve info       <",
    "\t 2. Retrieve info",
    "\t 3.   Modify info         <",
    "\t 3. Modify info",
    "\t 4.   View info list      <",
    "\t 4. View info list",
    "\t 5.   Delete info         <",
    "\t 5. Delete info",
    "\t 6.   About the program   <",
    "\t 6. About the program",
    "\t 7.   Exit Data Base      <",
    "\t 7. Exit Data Base",
  };
  unsigned int i;

  clear();

  for(i = 0; i < ARRAYSIZE(selections); i += 2)
  {
    if(i == select)
      printf ("%s\n", selections[i]);
    else
      printf ("%s\n", selections[i + 1]);
  }
}
1
2

while(x = getch())


Probably bc the above isn't a conditional statement.
In this context, the code is OK. The compiler is seeing = in a conditional and thinks you're made a mistake and that it should be == - which is not right in this case.
its hard to say if its a mistake or not bc as far as i can tell its just an endless loop. The only question I have is in which scenario does x = getch() become false? from what i gather from research that function pauses the program waiting for input similar to cin so either you'll enter a key or sit there forever. once a key is entered it displays the menu either way. VS won't even let me use that function. It complains and says use something else.
Ohh.. so if the code is Okay and I was thinking the same thing, if I put the "==" it gives me another warning at the next line that should be this if(x == 72), and if I change it to this if(x = 72) it says comparison between pointer and integer.

Now I am really confused :| Is there a way to write the code without the warning ?
Or it is normal that the compiler act like that, cause I'm not use to see these warnings, I like to write a code without errors or any warnings, when compiles.
Last edited on
For Windows it should be _getch(). To terminate, Alt-Gr then a character.

Last edited on
Ohh.. so if the code is Okay and I was thinking the same thing, if I put the "==" it gives me another warning at the next line that should be this if(x == 72), and if I change it to this if(x = 72) it says comparison between pointer and integer.


OK, first things first. What do you want it to be? An assignment? Or a comparison? The fact that you changed it from one to the other suggests that you're not sure what you even want here.

From context, it looks like you want:

- line 43 to be an assignment, in which case = is correct

- lines 45 and 52 to be comparisons, so == is correct

EDIT: You haven't answered markyrocks' question:

in which scenario does x = getch() become false?


Last edited on
No Mike.. I do really know what I want... in my case YES is an assignment, and my question was why the compiler give me the warning
suggest parentheses around assignment used as truth value
and the fact is I did not change it yet, I only said.. if it was a comparison
==
it will give me another warning like
comparison between pointer and integer


I only want to know how can I modify this to NOT show me the warning.
Last edited on

I only want to know how can I modify this to NOT show me the warning.


1
2
3
4
5
  while (1)
    {
        x = getch();
  //do stuff...
    }


or do what Mikey said. Either way its an endless loop.
Last edited on
seeplus has already explained why you're getting a warning.

To remove it, enclose the assignment in a second pair of parentheses.
Ahh okay okay... got it. Something like this.

 
while((x = getch()))


And I got no warnings.. :)

Thanks again guys
Last edited on
And I got no warnings.

Maybe not from your compiler, but you seem to have ignored the ones from posters here...
You mean seeplus ? Nope I didn't.. But he didn't say anything about:

MikeyBoy
To remove it, enclose the assignment in a second pair of parentheses.


He mention
For Windows it should be _getch(). To terminate, Alt-Gr then a character.
, and I use even _getch and doesn't remove the warning.
Sorry markyrocks, if I didn't give you an answer, I didn't do this intentional , I guess I was posting only a part of code so you can't see when x become false.

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
55
56
57
58
59
60
61
62
63
...
...
#Define KEY_ENTER 13

void menu()
{
    int select = 0;
    int x;

    selector(select);

    while((x = getch()))
    {
        if(x == 72)
        {
            select -= 2;
            if(select < 0)
                select = SELECT_END - 1;
            selector(select);
        }
        else if(x == 80)
        {
            select += 2;
            if (select > SELECT_END)
                select = 0;
            selector(select);
        }
        else if(x == 13) // ENTER KEY change the 'x'
        {
            if(select <= 1)
            {
                inputData();
            }
            else if(select <= 2)
            {
                retrieveData();
            }
            else if(select <= 4)
            {
                modifyData();
            }
            else if(select <= 6)
            {
                viewInfo();
            }
            else if(select <= 8)
            {
                deleteData();
            }
            else if(select <= 10)
            {
                about();
            }
            else if(select <= 12)
            {
                printf("\n\n\n\n\t\t\t    Exiting Data Base");
                Sleep(1000);
                system("cls");
                exit(0);
            }
        }
    }
}
Pressing Up and Down arrows only yes it's an endless loop, after you stop on one of the menu elements, and if you press Enter Key 'x' become false.
its your program i'm not that worried about it. I realize some programs need to run until the user does a specific action to end it. The point i'm trying to make is that if the condition never has the possibility to be false then what is the point of it even being in that spot? something like that is imo just a bad habit and will just make it harder to read and debug later. By all means do it however you want. I remember having a similar scenario years ago so i definitely understand how someone gets into that situation. The way i made sure i never ran into that particular problem again was to never assign variable values inside of a conditional statement. Part of my habit was coming from other languages that = could be a conditional operator and it would be interpreted that way if it was in a position where a conditional statement was supposed to be. In that scenario going

1
2
int x=NULL;
while(x=somefunc())  //would be false every time. 

obviously this doesn't apply to c++
Topic archived. No new replies allowed.