Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I just want to input string and int to some specific array but i got some errors

So i wanna to make a program that if I :

Input : 1 & 2 & 3

Output : & 1

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#include <stdio.h>

int main()
{
   char array[5];
   int arr[5];
   for (int i = 0; i < 5; i++){
       if (i%2 == 0){
           scanf("%d",arr[i]);
       } else {
           scanf(" %s ",array[i]);
       }
   }
   printf("%s",array[1]);
   printf(" %d",arr[0]);
}

>Solution :

You are using scanf incorrectly.

Write

   if (i%2 == 0){
       scanf( "%d", &arr[i]);
   } else {
       scanf( " %c", &array[i]);
   }

or

   if (i%2 == 0){
       scanf( "%d", arr + i );
   } else {
       scanf( " %c", array + i );
   }

Also in the call of printf write

printf("%c",array[1]);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading