Reputation:
This is my code. I am getting some weird warning's when I run it and I'm confused on why. Warnings on the bottom. My code will still run and the output is correct, however I still want to understand and fix the warnings thanks. I tried to fix all the warnings, however when I do my code won't output correctly anymore. My program fails. So I am confused. Please help!
#include <stdio.h>
#include <stdlib.h>
char s1(char *random);
char s2(char *s2_input, int index);
char strfilter(char *random, char *s2_input, char replacement);
int main()
{
int s1_index = 41;
char s1_random[s1_index];
s1(s1_random);
printf("\ns1 = ");
puts(s1_random);
printf("s2 = ");
int s2_index = 21;
char s2_input[s2_index];
s2(s2_input, s2_index);
if(s2_input[1] == '\0')
{
printf("size too small");
exit(0);
}
printf("ch = ");
char replacement = getchar();
printf("\n");
int filter_index = 41;
strfilter(s1_random, s2_input, replacement);
printf("\ns1 filtered = ");
puts(s1_random);
}
char s1(char *random)
{
int limit = 0;
char characters;
while(characters = ('A' + (rand() % 26))) /* random generatro */
{
if(limit == 41)
{
*(random + 41 - 1) = '\0';
break;
}
*(random + limit) = characters;
limit++;
}
}
char s2(char *s2_input, int index)
{
char array[21] = "123456789012345678901"; /* populated array to make sure no random memory is made */
char input;
int count = 0;
int check = 0;
while(input = getchar() )
{
if(input == '\n')
{
*(s2_input + count) = '\0';
break;
}
else if(input < 65 || input > 90)
{
printf("invalid input");
exit(0);
}
*(s2_input + count) = input;
count++;
}
index = count;
}
char strfilter(char *random, char *s2_input, char replacement) /* replacement function */
{
while(*s2_input)
{
char *temp = random;
while(*temp)
{
if(*temp == *s2_input)
*temp = replacement;
temp++;
}
s2_input++;
}
}
** Error message: **
matthew.c:41:22: warning: using the result of an assignment as a condition without parentheses [-Wparentheses]
while(characters = ('A' + (rand() % 26))) /* random generatro */
~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
matthew.c:41:22: note: place parentheses around the assignment to silence this warning
while(characters = ('A' + (rand() % 26))) /* random generatro */
^
( )
matthew.c:41:22: note: use '==' to turn this assignment into an equality comparison
while(characters = ('A' + (rand() % 26))) /* random generatro */
^
==
matthew.c:51:1: warning: non-void function does not return a value [-Wreturn-type]
}
^
matthew.c:61:17: warning: using the result of an assignment as a condition without parentheses [-Wparentheses]
while(input = getchar() )
~~~~~~^~~~~~~~~~~
matthew.c:61:17: note: place parentheses around the assignment to silence this warning
while(input = getchar() )
^
( )
matthew.c:61:17: note: use '==' to turn this assignment into an equality comparison
while(input = getchar() )
^
==
matthew.c:80:1: warning: non-void function does not return a value in all control paths [-Wreturn-type]
}
^
matthew.c:96:1: warning: non-void function does not return a value [-Wreturn-type]
}
^
5 warnings generated.
I tried what the compiler says to do, such as ==, or adding the parenthesis but then my code won't run correctly if I do that. So I am confused.
Upvotes: 0
Views: 50
Reputation: 12404
I tried what the compiler says to do, such as ==, ...
You should not just try any suggestion without understanding it. Your code is basically correct. You want an assignment.
But assignments in conditions is a very common case of errors. In most cases, there are comparisons used in conditions. The compiler does just warns you that this might be wrong.
Making it a comparison does not make any sense for you. What you should do instead is using extra brackets:
while(characters = ('A' + (rand() % 26)))
should be changed into
while ((characters = ('A' + (rand() % 26))) != 0)
In the next message you have an additional problem:
while(input = getchar() )
Here the reason for the warning is the same: Might be a typo in a comparison.
But this time, you are missing the actual condition. This loop will iterate until input
will become 0. Normally, you will get \n
or EOF
to indicate you reached the end of line or end of file.
Also, getchar
returns an int
which means you must use an int
variable to store the result. Otherwise you could never distinguish value 255
from EOF
.
This line should be
while ((input = getchar()) != EOF )
Regarding your other type of warning:
matthew.c:51:1: warning: non-void function does not return a value [-Wreturn-type]
}
^
I think, that is self explaining. If you define a function with a return type, you are supposed to return something.
If you don't need any return value of that function, define it as void
function.
Upvotes: 1