c++ - Problems with strcmp() -
i'm not sure if it's strcmp or function isn't working. here's code in main function.
double findlow(const char* date, const weather *data, int datasize) { int i, o; (i = 0; < datasize; i++) { o = strcmp(data[i].date(), date); //testing cout << o << ' ' << data[i].date() << date << endl; //testing if (strcmp(data[i].date(),date) == 0) return data[i].low(); } return 0.0; }
here's code date() function(public member function).
const char* weather::date() const { return _datedescription; }
for reason strcmp function return 1 if strings match. datedescription c-style string of 7 characters , data[i].date() attempts find date in same format datedescription.
also cout not show data[i].date()
edit: when run full code looks this:
days of weather: 3 enter date: oct/1 enter high: 15 enter low : 10 enter date: nov/13 enter high: 10 enter low : 1.1 enter date: dec/15 enter high: 5.5 enter low : -6.5 weather report: date high low ====================== oct/1_______15.0__10.0 nov/13______10.0___1.1 dec/15_______5.5__-6.5 enter date looking for: nov/13 1 oct/15 1 nov/13 1 dec/15 low temperature: 0.0(meant show low temp of date requested)
the 1 isn't showing date variable tested. paste of full code
here's problem:
cin >> query; //(in example stored in char query[7]) // , display found low temprature. cin.getline(query, 7, '\n');
you read in query
standard input...and read in again. second time, there's nothing left on line '\n'
-- reads in empty string.
i removed cin.getline
, got sensible result.
the way detect step through using debugger, , note query
""
instead of date typed in.
Comments
Post a Comment