0
#include <stdio.h>
        #include <stdlib.h>
        #include <math.h>
        
        int main() {
          int numcand;
          char name[numcand];
          int numregion;
          int votes[numregion][numcand];
          
          printf("Enter the number of candidates : ");
          scanf("%d", &numcand);
        
          for(int i=1; i<=numcand; i++){
            printf("Enter the name of candidate #%d : ", i);
            scanf("%s", &name[i-1]);
          }
        
        
          printf("Enter the number of regions : ");
          scanf("%d", &numregion);
        
        
          for(int i=0; i<=numregion; i++){
            for(int j=0;j<=numcand; j++){
              printf("Number of people who voted for %s : ", &name[i]);
              scanf("%d", &votes[j][i]);
            }
          }
        
        
        
          return 0;
        }

in line 26 I used %s to print the the first array but it prints the first letter of the first name and the rest with it. for example if name #1 is dave, name#2 is sam, name#3 is juan. it will print dsjuan

  • 3
    What exactly are you trying to do with `int numcand; char name[numcand];`? `numcand` is uninitialized, god knows what the size of that array will be. – mediocrevegetable1 Apr 30 '21 at 11:50
  • 1
    `int votes[numregion][numcand];` same error as described in previous comment. Both `numregion` and `numcand` are unitialised and have indeterminate values at that point. – kaylum Apr 30 '21 at 11:52
  • 2
    `name` is a string. Not an array of strings. – klutt Apr 30 '21 at 11:52
  • Sorry im new to coding. What do I need to change? – Loay_qubbaj Apr 30 '21 at 11:53
  • @Loay_qubbaj from what I can tell, for starters you probably want to create `name` and `votes` *after* you get the values of `numcand` and `numregion`. And change `name` to an array of strings, something like `char name[numcand][50]` where 50 is the max size of the name (including \0). – mediocrevegetable1 Apr 30 '21 at 11:56
  • @mediocrevegetable1 Ok ill try it. Thank you for your help – Loay_qubbaj Apr 30 '21 at 11:58
  • read about why you should use constants in order to limit your size of the array in c https://stackoverflow.com/questions/18848537/can-a-const-variable-be-used-to-declare-the-size-of-an-array-in-c – Saifeddine Ben Salem Apr 30 '21 at 12:05
  • Welcome to Stack Overflow! It looks like you forgot to enable a good set of warnings. For GCC, I recommend `-Wall -Wextra` as a minimum; consider also `-Wpedantic -Warray-bounds` for identifying some other common mistakes. – Toby Speight Apr 30 '21 at 12:19
  • @SaifeddineBenSalem using a variable for length is fine for local variables. – Gerhardh Apr 30 '21 at 12:49

1 Answers1

0

The ampersand is unnecessary when printing as the name of the array is a pointer to the first element of the array. Therefore, try removing the ampersand when printing the array.
Secondly, if you're going to be storing a 'string' you need to use a two-dimensional array and then store each 'string' in each row of that array.
For eg. char x[1][4] ={"Jack"} => x --> 'J'|'a'|'c'|'k'|

Helpful link --> https://www.knowprogram.com/c-programming/2d-array-of-strings-in-c/

Patrick
  • 64
  • 1
  • 7
  • For "a string" there is no need for a 2dimensional array. Only if you want "many strings". And if you are talking about strings, you need to be aware that `x[0]` is not a string with that initialization. – Gerhardh Apr 30 '21 at 12:52
  • Yes I know I am talking in the context of the code he gave that required him to store many strings. – Patrick Apr 30 '21 at 16:44