私は基本的には困惑しているという問題があります。まず、trustArray []とfashionArray []の2つのグローバル配列があります。ここでtrustArray充填関数である:いくつかの理由グローバル配列の値を変更するとき
void getTrust()
{
string line;
int reachedTrust=0;
int numberOfTrustsRecorded=0;
ifstream myfile ("BuyingJeans.Hw5 (1).csv");
if (myfile.is_open())
{
while (myfile.good())
{
getline (myfile,line,',');
//int found=line.find("Like-Purchase");
if (line=="Trust-Like"){
reachedTrust=1;
getline (myfile,line,',');
}
if(reachedTrust==1){
if(numberOfTrustsRecorded <6){
double testValue = atof(line.c_str());
trustArray[numberOfTrustsRecorded] = testValue;
numberOfTrustsRecorded++;
}
}
}
myfile.close();
}
else
cout << "Unable to open file";
}
は、この機能にatof()
は[] fashionArrayの値のうちの2つを変更しています。 atof()
をatoi()
と変更すると、問題は解決されなくなりました。ここで(fashionArrayは[])に変更されているアレイ充填方法であって、ここで
void getFashion(){
string line;
int reachedFashion=0;
int numberOfFashionsRecorded=0;
ifstream myfile ("BuyingJeans.Hw5 (1).csv");
if (myfile.is_open())
{
while (myfile.good())
{
getline (myfile,line,',');
if (line=="Like-Fash -->"){
reachedFashion=1;
getline (myfile,line,',');
//cout<<line<<endl;
//getchar();
}
if(reachedFashion==1){
if(numberOfFashionsRecorded <6){
fashionArray[numberOfFashionsRecorded] = atoi(line.c_str());
numberOfFashionsRecorded++;
}
}
}
myfile.close();
}
else cout << "Unable to open file";
}
は、これら2つのメソッドを呼び出すmainメソッドである:
int main() {
getFashion();
getTrust();
for(int x=0; x<6;x++)
cout<<fashionArray[x]<<endl;
getchar();
return 0;
}
最初fashionArrayの2つの値は、ちょっとばかりに大きな負の整数と正の整数に変わります。興味深いことに、main()メソッドで2つのメソッドが呼び出される順序を逆にすると、問題は発生しなくなります。誰でもこれを引き起こしている可能性がある何か考えている?
trustArrayはどこで宣言され割り当てられていますか? – DRVic
Cの文字列を変換するのに失敗した 'atof()'のように、負の値と負の値が大きく変わる(値が範囲外であるため、+/- HUGE_VALが返されます)... – user268396
trustArrayは配列として宣言され、それは変更されています。私は範囲の説明からの値に同意しますが、なぜそれが異なる配列の値を変更させるのかについてはまだ混乱しています。 –