Thread: What is wrong;

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    24
    Rep Power
    0

    What is wrong;


    hi to all,
    can someone help me
    to find out what is wrong into this code and i cannot print the x;

    Code:
    # include <stdio.h> # include <stdlib.h> # include <iostream> # include<conio.h>   main() {     int p[6];       int i,c;       int x;        printf("Give me 6 number\n");       for(i=0;i<6;i++);       scanf("%d",&p[i]);       printf("%d\n",p[i]);       x=p[1]+p[2];       printf("%d\n ",x);       system("pause");       }

    Comments on this post

    • salem disagrees : Pay attention to formatting before you press 'submit'. No one cares to look at code all on one line
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    71
    Rep Power
    3
    Take a close look at this line

    Code:
    for(i=0;i<6;i++);
    Please note the semi-colon.

    Also a note on proper syntax. The main function returns an integer.

    Code:
    int main()
    {
    /*C++ implies return 0 while C requires a return 0*/
    }
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    24
    Rep Power
    0
    Originally Posted by G4143
    Take a close look at this line

    Code:
    for(i=0;i<6;i++);
    Please note the semi-colon.
    it must be i<5 right ? but the problem is the x.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    71
    Rep Power
    3
    No. The problem is the semi-colon at the end of the for statement which creates an empty block. Here what your for statement equals...

    Code:
    for(i=0;i<6;i++);
    Which is the same as

    Code:
    for(i=0;i<6;i++){}/*Note the empty block*/
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    24
    Rep Power
    0
    Originally Posted by G4143
    No. The problem is the semi-colon at the end of the for statement which creates an empty block.
    thank you very much!!!!!!

IMN logo majestic logo threadwatch logo seochat tools logo