-5
#include <stdio.h>
#include <conio.h>
void main()
{
    clrscr();
    int lop, squr;
    for (lop=1; lop<=20; lop=lop+1);
    {
        squr = lop*lop;
        printf("%5d,%5d,\n",lop,squr);
    }
    getch();
}

my friend is saying is that this source code is working well.. but it is not working well at my side. what should I have to do to work well in C++ .

My Friend is saying to me that above mentioned code is working well in the version which he is using. I said, this code is incorrect and will give execution errors.... is above mentioned code is correct for any standard or version of C / C++.

and Also tell me that how many versions C++ are available...

Regards

4

2 に答える 2

6

for (lop=1; lop<=20; lop=lop+1); is the problem. Change to for (lop=1; lop<=20; lop=lop+1) (remove the semicolon will make this work).

This is your code, with problem fixed and optimized:

#include <stdio.h>
#include <conio.h> // Remove if you want

int main() {
    clrscr(); // Remove if you want
    int lop, squr;
    for (lop=1; lop<=20; ++lop) {
        squr = lop*lop;
        printf("%5d,%5d,\n", lop, squr);
    }
    getch(); // Remove if you want
    return 0;
}

Lines with // Remove if you want can be removed, but will change the behaviour. See @VinayakGarg's comment.

于 2012-09-22T16:02:07.910 に答える
6

This is how it should be -

#include <stdio.h>

int main()
{
    int lop, squr;
    for (lop = 1; lop <= 20; lop++)
    {
        squr = lop*lop;
        printf("%5d,%5d,\n", lop, squr);
    }
    return 0;
}

conio.h and hence clrscr() and getch() are not part of standard, you should not use them in your code.

EDIT-

and Also tell me that how many versions C++ are available...

C++ doesn't have versions exactly, there are standards

Year    C++ Standard                    Informal name
2011    ISO/IEC 14882:2011              C++11
2007    ISO/IEC TR 19768:2007           C++TR1
2003    ISO/IEC 14882:2003              C++03
1998    ISO/IEC 14882:1998              C++98

However there are versions of C++ compiler like gcc 4.7.2 etc.

于 2012-09-22T16:05:47.283 に答える