- Mng giúp em sửa
[Warning] deprecated conversion from string constant to 'char*' [-Wwrite-strings]
với ạ.
- Khi e đặt lệnh in 3 dòng các số chẵn, lẻ, âm thì chương trình bị lỗi. Nhưng khi in riêng lẻ thì chương trình lại k báo lỗi ạ. Mng giúp e giải thích tại sao với ạ?
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void LuuFile(int number);
void DocFile();
int main(int argc, char** argv) {
while(true)
{
int number;
cout<<"Hay nhap so:";
cin>>number;
LuuFile(number);
char h;
cout<<"Nhap tiep khong(c/k):";
cin>>h;
if(h=='k')
break;
}
cout<<"Ban da nhap xong."<<endl;
FILE *file=fopen("csdl.txt","r");
int Mchan[1000], iChan=0;
int Mle[1000], iLe=0;
int Mam[1000], iAm=0;
while(true)
{
char *line="\n";
char abc[10];
line=fgets(abc,10,file);
if(line!=NULL)
{
int number=atoi(line);
if(number%2==0 &&number>=0)
{
Mchan[iChan++]=number;
}
if(number%2!=0 &&number>=0)
{
Mle[iLe++]=number;
}
if(number<0)
{
Mam[iAm++]=number;
}
}
else
{
break;
}
}
fclose(file);
cout<<"\nCac so chan:";
for(int i=0;i<iChan;i++)
{
cout<<Mchan[i]<<"\t";
}
cout<<"\nCac so le:";
for(int i=0;i<iLe;i++)
{
cout<<Mle[i]<<"\t";
}
cout<<"\nCac so am:";
for(int i=0;i<iAm;i++)
{
cout<<Mam[i]<<"\t";
}
return 0;
}
void LuuFile(int number)
{
FILE *file=fopen("csdl.txt","a");
char *str;
itoa(number,str,10); //10-> thap phan, 16->hexa, 2 binary, 8-> octal
fputs(str, file);
fputs("\n", file);
fclose(file);
}
``