Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
if(puzzle[i][j] >= '0' && puzzle[i][j] <= '8' && puzzle[i][j] = '.')
I have that code and when I compile it, it gets Lvalue required error. Help. Thanks!
here is the full code. What I'm trying to do is to check the input of the user with the guidelines:
1) The input should be 0 to 8 and '.'
2) In the corners of the table( since it is a 2d array, it should be less than 3.
int main()
{
char puzzle[7][7];
int i;
int j;
printf("Enter your Tentaizu Puzzle:\n");
for(i = 0; i < 7; i++){
for(j = 0; j <7; j++){
scanf("%s", &puzzle[i][j]);
}
}
if(puzzle[i][j] >= '0' && puzzle[i][j] <= '8' && puzzle[i][j] == '.'){
if((puzzle[0][0] <= '3' && puzzle[0][6] <= '3') || (puzzle[6][0] <='3' && puzzle[6][6] <= '3')){
printf("Tentaizu Puzzle\n");
for(i = 0; i < 7; i++){
for(j = 0; j <7; j++){
printf("%s\n", puzzle[i][j]);
}
}
}
}
getch();
}
You typed = where you meant ==. As a result, C is trying to assign '.' to the result of puzzle[i][j] >= '0' && puzzle[i][j] <= '8' && puzzle[i][j] -- and you can't assign a value to a (boolean, in this case) value.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am working though the C programming book. I have a question in the arrays section (1.6) that says "Let us write a program to count the number of occurrences of each digit, of white space characters (blank, tab, newline) and all other characters.
#include <stdio.h>
/* count digits, white space, others */
int main()
{
int c, i, nwhite, nother;
int ndigit[10];
nwhite = nother = 0;
for (i = 0; i < 10; i++)
ndigit[i] = 0;
while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-0];
else if (c == ' ' || c == '\n' || c == '\t')
nwhite++;
else
nother++;
printf("digits =");
for (i = 0; i < 10; i++);
printf(" %d", ndigit[i]);
printf(", white space = %d, other = %d\n", nwhite, nother);
}
This is the compiling code. If I type 23, enter, 23, enter then ctrl+d it returns "digits = 1044509184, white space = 2, other = 0". Why? What is digits doing?
if (c >= '0' && c <= '9')
++ndigit[c-0];
This block of code is problematic as it is accessing the array out of bounds (and will cause undefined behaviours).
It should be
if (c >= '0' && c <= '9')
++ndigit[c-'0'];
Notice the difference between 0 and '0'. The latter piece of code works as it is guaranteed that number characters are encoded consecutively.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I was writing a simple code to change from lowercase to uppercase but its showing expression syntax error on a line 12 in the if() statement. The code runs fine without equality sign but does not work with equality. Any help to rectify it is appreciated.
The code is as follows:
#include<stdio.h>
#include<string.h>
#include<conio.h>
main()
{
char s[]="Computer";
int i;
clrscr();
for (i=0;i<strlen(s);i++)
{
if (s[i] > = 'a' && s[i] < = 'z')
s[i]+='A'-'a';
}
puts(s);
getch();
return 0;
}
This > = is not valid syntax because of the space between > and =. Delete the space :
if (s[i] >= 'a' && s[i] <= 'z')
The syntax error you are getting is due to the space between > and = operators. Likewise for < and =. Also, this would be a better version of your code:
int main()
{
char s[] = "Computer";
size_t i, len = strlen(s);
for (i = 0; i < len; i++) {
if (s[i] >= 'a' && s[i] <= 'z')
s[i] -= 32;
}
puts(s);
getch();
return 0;
}
Please look your code in if(s[i] > = 'a' && s[i] < = 'z')
as if(s[i] >= 'a' && s[i] <= 'z')
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am reading The C Programming Language 2nd Edition. I am doing the 2.10 in a tutorial introduction. I have to write a program about arrays. It is supposed to count digits, white spaces and others. This is the program:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int c, i, nwhite, nother;
int ndigit[10];
nwhite = nother = 0;
for (i = 0; i < 10; ++i)
ndigit[i] = 0;
while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c - '0'];
else if (c == ' ' || c == '\n' || c == '\t')
++nwhite;
else
++nother;
printf("digits =");
for (i = 0; i < 10; ++i)
printf(" %d ", ndigit[i]);
printf(", white space = %d, other = %d\n", nwhite, nother);
return 0;
}
According to the book, the output of the program by itself is
digits = 9 3 0 0 0 0 0 0 0 1, white space = 123, other = 345
I have 2 questions:
How will the program output by itself without me doing the CTRL+Z?
When I do it manually the output is not right. Please see if I made a mistake in the code.
The output I get is
digits =, white space = 0, other = 0
This:
printf("digits =");
for (i = 0; i < 0; ++i)
printf(" %d ", ndigit[i]);
has a broken middle part in the for loop's header; i < 0 will not be true (ever!) so the loop will not run.
for (i = 0; i < 0; ++i)
This is wrong.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
The task is to be sure that phrases are can be readed vise versa or not (without spaces ' ').
So I got the point and made some code. But when I made
printf("%s", str_1)
I found a lot of same russian letters.
For example my inputs are: '123' '321', but displayed rubbish
My str_1[i] is going to be like 123321 and then checked first and last and so on members.
After that I added in if condition if(str[i] != 'М') and now everything nice.
Please tell me what I did wrong and what is going on with "M".
I hope my question is clear.
#include "stdio.h"
#include "locale.h"
int main(){
char str[100];
char str_1[100];
int i, j, k, l;
j = 0;
l = 0;
fgets(str, 100, stdin); //we got array
//how many chars before '\0'
for(i = 0; i < 100; i++)
{
if(str[i] != ' ' && str[i] != '\n' && str[i] != 'М')
{
str_1[j] = str[i]; //made new array without spaces between words or whatever
j++;
}
}
j--;
for(k = 0; k < j; k++)
{
if(str_1[k] == str_1[j - 1 - k])
{
l++;
}
}
if( l == k)
{
printf(";) YES! good job!\n");
}
else
{
printf(";) NOT! I'm sorry!\n");
}
return 0;
}
As it seems, you skip over the end of the string, as you never check where it ends.
At least, this is my suspicion; I am not sure if it is the right reason.
You should check for str[i] == '\0' as well and then break the whole loop.
As I see, you even made this consideration in
//how many chars before '\0'
but didn't include it in code.
A
for(i = 0; i < 100 && str[i] != '\0'; i++)
should do the trick if you omit the j-- afterwards.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
well this program is supposed to convert a binary number to decimal. im new to c and im not very confident in coding C. tried and coded this program but doesnt run properly. and I have no idea what part is wrong here. need some help to complete this program.
thanks
binaryToDecimal(char str[]) {
for(int i = strlen(str)-i; i>=0; i--) {
if(str[i] == 48 || str[i] == 49) {
int tmp = pow(2, counter);
int sum= str[i]*tmp;
counter++;
answer += sum;
} else if(str[i]>49 || str[i]<48) {
printf("error");
}
}
printf("%d", &answer);
}
This phrase doesn't make sense:
int i = strlen(str)-i;
i hasn't been initialized yet, but you're using it in the expression!
I believe you meant:
int i = strlen(str)-1; // ONE ... not I
str[i] is a character, either '1' or '0' which as you have it is equal to 48 and 49 as integers. So you want to convert them to 1 and 0 them do the multiplication.
sum = (str[i] - 48) * tmp;
Assuming all variables have been initialised to sensible values and str holds the input. The following code should work, although it is not particularly efficient.
You can use '0' and '1' instead of more obscure 48 and 49.
for (i = strlen(str)-1, j = 0; i >= 0; --i, ++j) {
if (str[i] != '0' && str[i] != '1') {
printf("ERROR: unxepected input\n");
exit(EXIT_FAILURE);
}
if (str[i] == '1')
sum += pow(2, j);
}
when printing the result provide the value and not it's address, e.g.
printf("%d\n", sum); // (!) not &sum