0

This code with C language is supposed to count all characters equal to A in a table ..but it doesn't work I need help to find the Problem !! ~~ I've started by reading the table's characters giving by the user using a for loop and then i've used another for loop to count the number of characters equal to A~~

ANY suggestions please

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char T[100],A;
    int i,N,b=0;
    
    printf("give the number of your table's columns \n");
    scanf("%d", &N);

    if (N > 0 && N <= 100) {
        for (i; i < N; i++) {
            scanf("%c", &T[i]);

            printf("give the character of the column number %d \n", i);
            scanf("%c",&T[i]);
        }
     
        for (i; i < N; i++) {
            if (T[i] = 'A') b++; 

            printf("the number of characters equal to A is %d",b);
        }
        return 0;
    }
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • 1
    1. Format your code properly. Add indentation and remove extra blank lines. 2. Initialize `i` before using that. 3. `if(T[i]='A') b++;` doing assignment here looks weird. – MikeCAT Mar 02 '21 at 12:47
  • you have to explicit better why it doesnt work: what error message etc? On top of MikeCAT comment you have to look at `if (N > 0 && N <= 100) ` : you will have some problem because you try to write in your array at index 100 but your array ends at index 99 (size - 1). You should compile with **`gcc -Wall -Werror -Wextra`** Also "printf("the number of characters equal to..." should be out of your loop (after } ) – Antonin GAVREL Mar 02 '21 at 12:59
  • @AntoninGAVREL `if (N > 0 && N <= 100)` won't be trouble because there are conditions `i < N` before accessing the array `T[i]`. – MikeCAT Mar 02 '21 at 13:02
  • Another problem is that your program may not behave as expected if your input consists of multiple lines. – MikeCAT Mar 02 '21 at 13:03
  • @MikeCAT indeed – Antonin GAVREL Mar 02 '21 at 13:08
  • Ah, using `scanf("%c",&T[i]);` twice is eliminating problem with multiple-line input. – MikeCAT Mar 02 '21 at 13:09
  • @MikeCAT, but using scanf twice like this is a terrible way to consume the unwanted newline! – William Pursell Mar 02 '21 at 13:54

3 Answers3

1

"supposed to count all characters equal to A in a table ..but it doesn't work I need help to find the Problem !!"

Compiler warnings will help you to find the problem.
If you are not seeing warnings when you compile, set your compiler to show them. (in GCC for example use -Wall)

When compiling with warnings turned on you should see information similar to this upon a compile:

Build Status (so.prj - Debug)
 s0_15.c - 3 warnings
  22, 22    warning: using the result of an assignment as a condition without parentheses 
  22, 22    note: place parentheses around the assignment to silence this warning
  22, 22    note: use '==' to turn this assignment into an equality comparison
  7, 17     warning: unused variable 'A' 
  14, 14    warning: variable 'i' is uninitialized when used here 
  8, 10     note: initialize the variable 'i' to silence this warning

Additionally, when dropping down into the second for() loop, i again is not initialized, but left with the value it had when finishing the first loop. It needs to be re-initialized to zero for that second loop to work... (Note, my compiler did not flag this, fortunately I was told at one point to learn how to set and use breakpoints which I did here, allowing me to discover the value of i during runtime.

By following the guidance offered by the warnings above, modifications were made to allow the code to run, and does what you describe it should.

See comments in-line for the response to the warnings above:

int main(void)//added void to match accepted prototype of main
{
    char T[100] = {0},A;//A is not used, should be deleted
                        //initialize array to all zeros.
    int i=0,N=0,b=0;//initialized i (and N)
    
    printf("give the number of your table's columns \n");
    scanf("%d", &N);

    if (N > 0 && N <= 100) {
        for (i; i < N; i++) {
            scanf("%c", &T[i]);

            printf("give the character of the column number %d \n", i);
            scanf("%c",&T[i]);
        }
        // `i` is already equal to N here.  it will never enter the loop
        //for (i; i < N; i++) {         
        for (i = 0; i < N; i++) {//initialized i to zero
          //if (T[i] = 'A') b++;//this is not what you want
            if (T[i] == 'A') b++; //replace assignment '=' 
                                  //with    comparison '==' operator
            //Note, optionally move the following printf() statement to below 
            //the loop so it is called only once after counting is done.
            printf("the number of characters equal to A is %d\n",b);//added '\n' for readability of output.
        }
        //return 0;// not here...
    }
    return 0;// ...but here
}
ryyker
  • 22,849
  • 3
  • 43
  • 87
1

C has a language feature (or flaw). You can use the assignment operator in contexts where it is typically not wanted.

 if (T[i] = 'A') b++;
 

is changing T[i] and not comparting it to A. This means the if statement is registering if the change was to a value that isn't 0.

Use == and you'll be comparing again.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
0

You have to be explicit in explaining why it doesnt work: what error message, how do you compile, Do you need number 100 etc?

On top of MikeCAT comment you have to look at if (N > 0 && N <= 100) : you will have some problem because you try to write in your array at index 100 but your array ends at index 99 (size - 1).

You probably don't need to scanf twice, so you can remove the first one: scanf("%c",&T[i])

You should compile with gcc -Wall -Werror -Wextra

Also printf("the number of characters equal to...") should be out of your loop (after } )

Your return 0; should also be out of your if condition and before the final }

Let me know if its helpful and start by amending your code.

ryyker
  • 22,849
  • 3
  • 43
  • 87
Antonin GAVREL
  • 9,682
  • 8
  • 54
  • 81
  • if I won't write scanf("%c",&T[i]) twice ,with what should I replace it ? becuase when writing it one time it does another thing unwanted actually i couldn't understand the reason why – Soraya Forward Mar 02 '21 at 14:54
  • you might want to read this: https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer – Antonin GAVREL Mar 02 '21 at 15:02