Thread: In 2nd and 3rd for loop it is not taking and displaying values for j=1,3..pls help.

  1. #1
    Registered User
    Join Date
    Aug 2017
    Posts
    4

    In 2nd and 3rd for loop it is not taking and displaying values for j=1,3..pls help.

    insert
    Code:
    #include<stdio.h>
    void main()
    {
     int i,j;
     char name[]="PRANAY";
     char n2[60];
     clrscr();
     for(i=0;i<5;i=i+1)
     {
      printf("Your name is:%c\n",name[i]);
     }
     for(j=0;j<5;j++)  //<---prblm here
     {
      printf("char at index %d",j);
      scanf("%c",&n2[j]);
     }
     for(j=0;j<5;j++)  //<---prblm here
     {
      printf("Your name is:%c\n",n2[j]);
     }
     getch();
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Do you realize that the "%c" scanf() format specifier doesn't skip leading whitespace characters and that it leaves the end of line character in the input buffer? You can make scanf() skip leading whitespace by inserting a space in the format specifier " %c".

    You also need to #include the proper include file for that non-standard function (getch()) before you try to use it. I really suggest you use a standard function instead, getchar() perhaps.

    And main() should be defined to return an int, and you should return an int from that function:
    Code:
    int main(void)
    {
        return 0;
    }

    Jim

  3. #3
    Registered User
    Join Date
    Aug 2017
    Posts
    4
    thank you so much jimblumberg.....adding space like " %c" it worked...keep up doing the good work.....thanks again.

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    A single space indentation is not enough. You need to use at least 2.

    Also, the 5 should be a 6 if you want to print the whole name, which is 6 letters long. Better yet, use strlen to determine the length:
    Code:
      int len = strlen(name);
      for (i = 0; i < len; i++)
    You need to include string.h to use strlen.

    Also note the spacing within the above for loop head. There's no point squishing everything together.

    We would usually use putchar() instead of printf("%c",...) to output a single character:
    Code:
        putchar(name[i]);
    You need to include the non-standard header conio.h to use the non-standard functions clrscr() and getch() (which don't work for me, by the way).

    And remember that main should return an int. void main is an extension that the MS compiler allows, but most others don't (since it's basically pointless).

  5. #5
    Registered User
    Join Date
    Aug 2017
    Posts
    4
    thank you so much +algorism for giving suggestion.....keep doing the good work....!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stdin: Loop taking values - bug with new line character
    By deathmetal in forum C Programming
    Replies: 2
    Last Post: 08-13-2015, 11:22 AM
  2. array not displaying values
    By gunitinug in forum C++ Programming
    Replies: 2
    Last Post: 12-02-2012, 11:31 PM
  3. Replies: 1
    Last Post: 11-07-2011, 11:11 PM
  4. Displaying Values...
    By vailant in forum C Programming
    Replies: 35
    Last Post: 01-21-2010, 06:59 PM
  5. Int Array - Stop taking values at [zero]
    By bunko in forum C Programming
    Replies: 3
    Last Post: 12-04-2008, 12:54 AM

Tags for this Thread