2, mình là newbie đang tự học visual c++. mình viết thử ct thì nó báo lỗi. mấy bạn chỉ cho mình chỗ sai, giải thich và sữa lại dùm nha! :)
CT1: nhập n và in ra kết quả phép tính 1/1-1/2+1/3-1/4.....(+/-)1/n
error: mình nhập n = bi nhiu thì kết quả cũng ra là 0
#include "stdafx.h"
#include "math.h"
int main(int argc, char* argv[])
{
int i, n;
float s, x;
printf("nhap n. n= ");
scanf("%d", &n);
s=0;
for (i=1; i<=n; i++)
{
if (i%2==0)
{
x=-1/i;
}
else
{
x=1/i;
}
s= s+x;
}
printf("\n tong la %5.2f", &s);
return 0;
}
CT2: nhập n và in ra kết quả: 1+2+3+4+......+n
error: mình nhập n là bi nhiu thì kết quả đều là 1245044
#include "stdafx.h"
#include "math.h"
int main(int argc, char* argv[])
{
int i, n, s;
printf("nhap n. n= ");
scanf("%d", &n);
s=0;
for (i=0; i<n; i++)
{
s= s+i;
}
printf("\n tong la %5.2d", &s);
return 0;
}
CT3: nhập vào danh sách n thí sinh và thông tin về tên, giới tinh, năm sinh, SBD, điểm văn, toán, anh.
in ra thông tin của những thí sinh có điển văn, toán và anh trên 5 điểm.
error: nó báo 34 lỗi:
#include "stdafx.h"
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
int main(int argc, char* argv[])
{
struct s
{
char ten;
int year;
char sex;
int sbd;
int lit;
int math;
int eng;
};
s a;
int i, n;
printf("so thi sinh la ");
scanf("%d", n);
for (i=0; i<n; i++)
{
printf("\n nhap thong tin thi sinh thu %d",i+1);
printf("\n ten: ");
gets(a.ten);
printf("\n nam sinh: ");
gets(a.year);
printf("\n gioi tinh: ");
gets(a.sex);
printf("\n so bao danh: ");
gets(a.sbd);
printf("\n diem van: ");
gets(a.lit);
printf("\n diem toan: ");
gets(a.math);
printf("\n diem anh van: ");
gets(a.eng);
}
for (i=0; i<n; i++)
if (a.lit>5)
{
if (a.math>5)
{
if (a.eng>5)
{
printf("thi sinh: %s", a.ten, "sinh nam: %d", a.year, "gioi tinh: %s", a.sex, "sbd: %s", a.sbd , "diem toan: %d", a.math, "diem van: %d", a.lit, "diem anh van: %d", a.eng);
}
}
}
return 0;
}
btw, bác nào có bảng báo lỗi của c++(có viettrans thì cho em xin)
thank trước
CT1: nhập n và in ra kết quả phép tính 1/1-1/2+1/3-1/4.....(+/-)1/n
error: mình nhập n = bi nhiu thì kết quả cũng ra là 0

#include "stdafx.h"
#include "math.h"
int main(int argc, char* argv[])
{
int i, n;
float s, x;
printf("nhap n. n= ");
scanf("%d", &n);
s=0;
for (i=1; i<=n; i++)
{
if (i%2==0)
{
x=-1/i;
}
else
{
x=1/i;
}
s= s+x;
}
printf("\n tong la %5.2f", &s);
return 0;
}
CT2: nhập n và in ra kết quả: 1+2+3+4+......+n
error: mình nhập n là bi nhiu thì kết quả đều là 1245044
#include "stdafx.h"
#include "math.h"
int main(int argc, char* argv[])
{
int i, n, s;
printf("nhap n. n= ");
scanf("%d", &n);
s=0;
for (i=0; i<n; i++)
{
s= s+i;
}
printf("\n tong la %5.2d", &s);
return 0;
}
CT3: nhập vào danh sách n thí sinh và thông tin về tên, giới tinh, năm sinh, SBD, điểm văn, toán, anh.
in ra thông tin của những thí sinh có điển văn, toán và anh trên 5 điểm.
error: nó báo 34 lỗi:
1.error C2676: binary '[' : 'struct main::s' does not define this operator or a conversion to a type acceptable to the predefined operator
2.error C2228: left of '.ten' must have class/struct/union type
3.error C2676: binary '[' : 'struct main::s' does not define this operator or a conversion to a type acceptable to the predefined operator
4. error C2228: left of '.year' must have class/struct/union type
5.error C2676: binary '[' : 'struct main::s' does not define this operator or a conversion to a type acceptable to the predefined operator
6. error C2228: left of '.sex' must have class/struct/union type
7.error C2676: binary '[' : 'struct main::s' does not define this operator or a conversion to a type acceptable to the predefined operator
8.error C2228: left of '.sbd' must have class/struct/union type
9. error C2676: binary '[' : 'struct main::s' does not define this operator or a conversion to a type acceptable to the predefined operator
10. error C2228: left of '.lit' must have class/struct/union type
11. error C2676: binary '[' : 'struct main::s' does not define this operator or a conversion to a type acceptable to the predefined operator
12. error C2228: left of '.math' must have class/struct/union type
13. error C2676: binary '[' : 'struct main::s' does not define this operator or a conversion to a type acceptable to the predefined operator
14. error C2228: left of '.eng' must have class/struct/union type
15. error C2676: binary '[' : 'struct main::s' does not define this operator or a conversion to a type acceptable to the predefined operator
16. error C2228: left of '.lit' must have class/struct/union type
17. error C2676: binary '[' : 'struct main::s' does not define this operator or a conversion to a type acceptable to the predefined operator
18. error C2228: left of '.math' must have class/struct/union type
19. error C2676: binary '[' : 'struct main::s' does not define this operator or a conversion to a type acceptable to the predefined operator
20.error C2228: left of '.eng' must have class/struct/union type
21. error C2676: binary '[' : 'struct main::s' does not define this operator or a conversion to a type acceptable to the predefined operator
22. error C2228: left of '.ten' must have class/struct/union type
23. error C2676: binary '[' : 'struct main::s' does not define this operator or a conversion to a type acceptable to the predefined operator
24. error C2228: left of '.year' must have class/struct/union type
25. error C2676: binary '[' : 'struct main::s' does not define this operator or a conversion to a type acceptable to the predefined operator
26. error C2228: left of '.sex' must have class/struct/union type
27. error C2676: binary '[' : 'struct main::s' does not define this operator or a conversion to a type acceptable to the predefined operator
28. error C2228: left of '.sbd' must have class/struct/union type
29. error C2676: binary '[' : 'struct main::s' does not define this operator or a conversion to a type acceptable to the predefined operator
30. error C2228: left of '.math' must have class/struct/union type
31. error C2676: binary '[' : 'struct main::s' does not define this operator or a conversion to a type acceptable to the predefined operator
32. error C2228: left of '.lit' must have class/struct/union type
33. error C2676: binary '[' : 'struct main::s' does not define this operator or a conversion to a type acceptable to the predefined operator
34. error C2228: left of '.eng' must have class/struct/union type
Error executing cl.exe.
#include "stdafx.h"
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
int main(int argc, char* argv[])
{
struct s
{
char ten;
int year;
char sex;
int sbd;
int lit;
int math;
int eng;
};
s a;
int i, n;
printf("so thi sinh la ");
scanf("%d", n);
for (i=0; i<n; i++)
{
printf("\n nhap thong tin thi sinh thu %d",i+1);
printf("\n ten: ");
gets(a.ten);
printf("\n nam sinh: ");
gets(a.year);
printf("\n gioi tinh: ");
gets(a.sex);
printf("\n so bao danh: ");
gets(a.sbd);
printf("\n diem van: ");
gets(a.lit);
printf("\n diem toan: ");
gets(a.math);
printf("\n diem anh van: ");
gets(a.eng);
}
for (i=0; i<n; i++)
if (a.lit>5)
{
if (a.math>5)
{
if (a.eng>5)
{
printf("thi sinh: %s", a.ten, "sinh nam: %d", a.year, "gioi tinh: %s", a.sex, "sbd: %s", a.sbd , "diem toan: %d", a.math, "diem van: %d", a.lit, "diem anh van: %d", a.eng);
}
}
}
return 0;
}
btw, bác nào có bảng báo lỗi của c++(có viettrans thì cho em xin)
thank trước
