所以我正在爲一個學校項目編寫一個程序,其中一部分需要用戶在命令行中輸入一個隨機數。該程序然後使用atof將該數字轉換爲浮點數,以便我可以對它進行一些數學運算。該計劃的一部分是這樣的:Atof無法正常工作?
#include <iostream>
#include <cstdlib>
#include "bmplib.h" //this is just something the prof. gave us to help read the image
#include <cmath> //i might use this later in the program
#define nummethods 2
using namespace std;
unsigned char input[256][256][3];
unsigned char bg [256][256][3];
unsigned char output[256][256][3];
unsigned char m[2][256][256];
int main(int argc, char *argv[])
{
int h,i,j,k;
double x,y,z;
//code to read img here and make sure user puts correct number of arguments in
//command line
for (i=0;i<256;i++){
for(k=0;k<3;k++){
y = y + input[i][0][k];
}
}
cout >> y >> endl; //this is giving me 36,164,75 which in RGB is green
x = atof(argv[3]); //the number was the 3rd thing typed in at the command line
cout << x << endl;
z = x + y; //this isn't the exact program, but you get the idea
cout << z << endl;
//there's some more code after this written by the prof. that manipulates the image,
//but i don't think its relevant since it does not use the argv[3] value
}
程序編譯,但它沒有工作的權利。我通過添加一個cout來測試它;這表明atof給了我錯誤的號碼。例如,當將5.0作爲我的號碼放入命令行時,它顯示我的x是379.7465。任何想法有什麼不對?
你能把它縮小到最小的情況嗎?例如,執行'atof(argv [1])'並使用'./program 5.0'來試用它,看看你得到了什麼 –
你把頭文件放到atof中了嗎?測試用例會有幫助 – eran
也可以嘗試'cout << argv [3]'。它可能不是你想要的。 – stardt