0
我創建了一個函數,它將數字轉換爲給定基數的等價物並將其打印到字符串中。它看起來像無瑕疵,但給出了荒謬的結果。下面的代碼應該將100轉換爲9,並給出「121」。字符串指針中有趣的錯誤
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void doldur(string *s,int u,int base){
*s="";
while(u!=0){
*s=""+u%base+*s;
u/=base;
}
return;
}
int main() {
ofstream fout ("dualpal.out");
ifstream fin ("dualpal.in");
int i;
string hey;
doldur(&hey,100,9);
cout<<hey;
cin>>i;
return 0;
}
但可笑的是,它打印dualpal.outualpal.outdualpal.out。(對於不同的基地也給其他有趣的結果)
哪裏缺陷?
''''不是'std :: string'類型。 – chris
如果你從不使用它們,你爲什麼要聲明'fout'和'fin'? – jwodder
具體而言,它在這一行很重要:'* s =「」+ u%base + * s;' – wjl