想实现运行次数计数,但下面代码总是出现问题,努力但找不到解决的方法,请教高手!
#include <fstream>
#include <string>
using namespace std;
int str_int(string str);
void main()
{
ofstream out("count.txt"); //count.txt里有一个0
ifstream in("count.txt");
string s;
in>>s;
in.close();
int n=str_int(s);
++n;
out<<n;
out.close();
}
int str_int(string str)
{
int i,len = str.length(),num=0;
i=0;
if (str[0]=='-')
i=1;
while(i<len)
{
num=num*10+(int)(str[i]-'0');
i++;
}
if(str[0]=='-')
num*=-1;
return num;
}
#include <fstream>
#include <string>
using namespace std;
int str_int(string str);
void main()
{
ofstream out("count.txt"); //count.txt里有一个0
ifstream in("count.txt");
string s;
in>>s;
in.close();
int n=str_int(s);
++n;
out<<n;
out.close();
}
int str_int(string str)
{
int i,len = str.length(),num=0;
i=0;
if (str[0]=='-')
i=1;
while(i<len)
{
num=num*10+(int)(str[i]-'0');
i++;
}
if(str[0]=='-')
num*=-1;
return num;
}