i'm writing in c, why is my code not running? I drawn on the ss the error

45 Views Asked by At

sshot

I didnt understood why this message apeared in my terminal:

Please enter your 1st variable that you want to calculate the percentage of without any spaces: morangos PS C:\Users\TechnoStoreReparacoe\Desktop>

this is the full code:

#include <stdio.h>
#include <string.h>

int main (){
    
    char name1[25] ;
    char name2[25] ;
    char name3[25];
    char name4[25];
    char name5[25];

    int value1 ;
    int value2 ;
    int value3 ;
    int value4 ;
    int value5 ;
    
    
    printf ("Hello mortal, this program allows you to calculate percentages of five parts that are the only components of the whole.\n");
    
    printf ("\nPlease enter your 1st variable that you want to calculate the percentage of without any spaces:\n");
    fgets (name1, 25, stdin);
    printf ("\nPlease enter the value of %s:\n", name1[25]);
    scanf ("%i", &value1);
    
    printf ("\nPlease enter your 2nd variable that you want to calculate the percentage of without any spaces:\n");
    fgets (name2, 25, stdin);
    printf ("\nPlease enter the value of %s:\n", name2[25]);
    scanf ("%i", &value1);

    
    printf ("\nPlease enter your 3rd variable that you want to calculate the percentage of without any spaces:\n");
    fgets (name3, 25, stdin);
    printf ("\nPlease enter the value of %s:\n", name3[25]);
    scanf ("%i", &value1);


    printf ("\nPlease enter your 4th variable that you want to calculate the percentage of without any spaces:\n");
    fgets (name4, 25, stdin);
    printf ("\nPlease enter the value of %s:\n", name4[25]);
    scanf ("%i", &value1);


    
    printf ("\nPlease enter your 5th variable that you want to calculate the percentage of without any spaces:\n");
    fgets (name5, 25, stdin);
    printf ("\nPlease enter the value of %s:\n", name5[25]);
    scanf ("%i", &value1);


    return 0;
}

Im making a probability finder program, very simple. where the user inputs variables names and respective numerical values. Its my first program, and I dont understood why the code isnt running well

1

There are 1 best solutions below

0
chux - Reinstate Monica On

printf() expects a char *, not a char

name1[25] in the printf() is the 25th (staring from 0) element of array name1[]. That char is outside the array name[] and cannot be read.

"%s" expects a matching pointer, a char *.

char name1[25] ;
// printf ("\nPlease enter the value of %s:\n", name1[25]);
printf ("\nPlease enter the value of %s:\n", name1);