Bracket error in c "error: expected identifier or '(' {" [closed] - c

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 months ago.
Improve this question
I'm working on a problem set for CS50 in C but I keep getting an error readability.c:11:1: error: expected identifier or '(' { and I'm not sure what's wrong. Any help would be much appreciated.
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int count_letters(string text);
int main(void);
{
string paragraph = get_string("Input text: ");
count_letters(paragraph);
int L = 0;
int S = 0;
int index = 0.0588 * L - 0.296 * S - 15.8;
printf("%i", index);
}
int count_letters(string text)
{
int letters = 0;
for (int i = 0, length = strlen(text); i < length; i++)
if isalpha(text[i]) {
letters + 1
}
return letters;
}

You have a semicolon at
int main(void);
and you need one here:
for (int i = 0, length = strlen(text); i < length; i++){ //added {
if(!(isalpha(text[i]) == 0)){ // is alpha in brackets too and I usually prefer to check result with proper types
letters + 1; //addded ;
}
}

Related

Why in C without a semicolon gives an error? [closed]

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 4 months ago.
Improve this question
Why does it throw an error in the seventh round without semicolons (;). When do you need to put such semicolons in the code?
#include <stdio.h>
#define S 10
void print_arr(int *arr, int size) {
int *p = arr + S;
for(;arr < p; arr++) {
printf("%d ", *arr);
}
}
int main() {
int arr[S] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
print_arr(arr, S);
return 0;
}
if you try without a semicolon (;) then it gives the following error
main.c:7:23: error: expected ';' before ')' token
7 | for(arr < p; arr++) {
| ^
| ;
A for loop consists of four parts:
for(init;condition;iteration) { body }
For the compiler to know what is used as init, condition or iteration, you separate it via semicolon. if you want to keep one of the parts empty, you simply write a semicolon.
for(;i < 10;i++) is a loop that has no initialisation. only a condition and an iteration statement.
for(int i= 0;; i++) has no condition and
for(int i=0;i < 10;) has no iteration statement.
writing for(i < 10; i++) is simply not valid syntax.

C simple question of getting average value from array each components addition [closed]

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
This is a c code for getting the average value of the addition of array components.
But once I run this which is not outputting anything.
Can anyone help me out where I got the code wrong?
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
double solution(int arr[], size_t arr_len);
int main()
{
int array[10] = { 1,2,3,4,5,6,7,8,9,10 };
int length = sizeof(array[10]);
double out = solution(array, length);
printf("solution is %f\n", out);
return 0;
}
double solution(int arr[], size_t arr_len) {
double answer = 0;
int total = 0;
for (int i = 0; i < arr_len;){
total += arr[i];
}
answer = total / arr_len;
return answer;
}
You are not incrementing the loop counter in solution so its stuck in an infinite loop.
for (int i = 0; i < arr_len;){
needs to be
for (int i = 0; i < arr_len; i++) {
Edit:
sizeof is also wrong. It returns the total memory used by the array. So you need to do
int length = sizeof(array) / sizeof(array[0])
which divides the total memory by the size of one element to give you the total number of elements.

Why does this program result in an infinite loop output? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
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.
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.
Improve this question
This code is an exercise from a daily programming mailing list. I am trying to print out a list of given words in reverse order. The words are delimited by spaces. When running the code below, it enters into an infinite loop just printing the (new) first word. I have looked through the conditions and everything looks OK to me. I think it may take a fresh set of eyes to point out a simple mistake, but I can't find anything. Thanks to anyone who can lend a hand.
*Note: I am planning on adding back in the spaces to the output after this is figured out. I am aware the output will just be one long string without spaces so far.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void)
{
char *words;
words = "here world hello spam foo bar baz";
int space_indices[10];
int counter = 0;
// Find the spaces and keep track of the index numbers in the space_indices array
for (int i = 0; i < strlen(words); i++) {
if (words[i] == ' ') {
space_indices[counter] = i;
counter++;
}
}
for (int i = counter - 1; i >= 0; i--) {
if ( i = counter - 1) {
// Print the first word
for (int j = space_indices[i] + 1; j < strlen(words); j++) {
printf("%c", words[j]);
}
} else if (i >= 0) {
// Print the other words except for the last
for (int j = space_indices[i] + 1; j < space_indices[i + 1]; j++) {
printf("%c", words[j]);
}
} else {
// Print the last word
for (int j = 0; j < space_indices[0]; j++) {
printf("%c", words[j]);
}
}
}
}
As Havenard explained, the problem was that I was not using a comparison operation, I was using an assignment operator.
This:
if ( i = counter - 1)
should be:
if ( i == counter - 1)

rand() doesn't give me random numbers [closed]

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 3 years ago.
Improve this question
I want to print 1000 random numbers saved in a array. Numbers have to be between 1 and 10000.
I put srand(time(NULL)) in my main function and the array have to be filled with random numbers in my init function. The ausgabe function is for formatted output.
But rand fills my array with numbers all in row.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ARR_SIZE 1000
void init(int * ptr, int size){
for (int i = 0; i < size; i++){
*(ptr + i) = (rand() %10000+1);
}
}
void ausgabe(int * ptr, int size){
for (int i = 0; i < size; i++){
printf("%5i\t", * ptr + i);
if ((i + 1) %10 == 0){
printf("\n");
}
}
printf("\n\n");
}
int main(){
int zufall[ARR_SIZE];
srand(time(NULL));
init(zufall, ARR_SIZE);
printf("\n\t\t\t---unsortierte Ausgabe---\n\n");
ausgabe(zufall, ARR_SIZE);
return 0;
}
* ptr + i is (*ptr)+i, not *(ptr+i). You need to be more careful with operator precedence. (And to learn to use your debugger: 30 seconds in your debugger would have clearly revealed that the problem was the printing, not the initialization.)

store the rest of a successive division in a array [closed]

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 4 years ago.
Improve this question
I want to create a program who store the rest of successive division in the array.But unfortunately the array i created refuse store the remain correctly for example 5/2 the remain is suppose to be 1 but the array store another value
#include <stdio.h>
#include <stdlib.h>
int divise(int n){
int i=0;
int remain[20];
int rest;
while(n!=0){
rest = n%2;
remain[i] = rest;
n = n/2;
i++;
printf("%d\n",remain[i]);
}
}
int main(){
divise(10);
}
The mistake is with your i++ statement . It should be after printf("%d\n",remain[i]);.
Modified code :-
#include <stdio.h>
#include <stdlib.h>
int divise(int n)
{
int i = 0;
int remain[20];
int rest;
while (n != 0)
{
rest = n % 2;
remain[i] = rest;
n = n / 2;
printf("%d\n", remain[i]);
i++; // repositioned
}
}
int main()
{
divise(10);
return 0;
}
Output :-
0
1
0
1
Your function int divise(int n) do not return any int values . So better make it void divise(int n) .Also int main() should have a return 0 .

Resources