-3

I'm not sure if it's strcmp or just the function itself but something isn't working. Here's the code in the main function.

double findLow(const char* date, const Weather *data, int dataSize) {
    int i, o;
    for (i = 0; i < 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 the code for the date() function(public member function).

const char* Weather::date() const {
    return _dateDescription;
}

For some reason the strcmp function will return 1 even if the strings match. dateDescription is a C-style string of 7 characters and the data[i].date() attempts to find the date in the same format as dateDescription.

Also the cout will not show the data[i].date()

EDIT: When I run the full code it looks like 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 the date you are looking for: Nov/13
1 Oct/15
1 Nov/13
1 Dec/15
Low temperature: 0.0(meant to show the low temp of the date requested)

The one that isn't showing is the date variable from what I tested. Paste of the full code

  • 2
    *For some reason the strcmp function will return 1 even if the strings match* -- Well evidently, the strings *don't* match. Either you're wrong, or you have one heck of a buggy compiler. – PaulMcKenzie Feb 08 '16 at 03:45
  • 3
    "Also the cout will not show the data[i].date()", then your problem isn't with the `strcmp`. It is probably something to do with how `_dateDescription` is set up, or possibly even `data`. Can you show that code? – The Dark Feb 08 '16 at 03:46
  • Why are you comparing dates as strings? You should be comparing them as dates. You need to show some sample input that is failing. I suggest you also output both dates being compared in your trace message. – user207421 Feb 08 '16 at 03:46
  • for debugging purpose, try to show the first couple of characters in data[i].date() e.g. `data[i].date()[0]` and `data[i].date()[1]`, so on and see what is happening there. Is the first character a `'\0'` – Bishoy Feb 08 '16 at 03:49
  • Consider representing dates in some more precise way than as strings. – Cheers and hth. - Alf Feb 08 '16 at 03:51
  • Cannot duplicate: http://ideone.com/D5q33i Convince yourself that there is nothing wrong by hard-coding the strings, just like the example at the link. Obviously the strings you're claiming are equal are *not* equal. Maybe one of the strings has a space at the end, a hidden control character (like a `\n` which cannot be seen, etc.). – PaulMcKenzie Feb 08 '16 at 03:56
  • @TheDark dateDescription is set up like this: ` class Weather { char _dateDescription[7]; double _highTemp; double _lowTemp; } ` – TheHobospider Feb 08 '16 at 04:03
  • That is how it is declared - how are you putting data into it? – The Dark Feb 08 '16 at 04:09
  • `The one that isn't showing is the date variable from what I tested.` how can you tell which one isn't being printed? Please either post an SSCCE that demonstrates the problem, or post your code in its entirety. – kfsone Feb 08 '16 at 04:17
  • 2
    A common problem is a trailing newline in one of the strings being compared. That depends on where the data came from, but you haven't shown us that. – Keith Thompson Feb 08 '16 at 04:19
  • @TheDark it seems like the _dateDescription isn't a problem from testing. It seems like the problem comes from the date variable. This variable is stored by ` cin >> query; cin.getline(query, 7, '\n'); ` I think it's something from here that I don't entirely understand. – TheHobospider Feb 08 '16 at 04:21
  • 1
    Have you considered just stepping through the code in a debugger and reviewing the strings being compared? – Michael Petch Feb 08 '16 at 04:24
  • @kfsone I removed one and left the other in and when it was just date it would only print 1s. – TheHobospider Feb 08 '16 at 04:26
  • Can you give us an MCVE? It'll be way easier to find the problem if we can duplicate it. – Topological Sort Feb 08 '16 at 04:31
  • @WillBriggs Added a pastebin of the code – TheHobospider Feb 08 '16 at 04:42
  • @TheHobospider `cout << o << ' ' << data[i].date() << date << endl;` The output becomes hard to read without a space between the dates you're trying to view. Also, this doesn't prove what others (and myself has mentioned), and that is the strings are not the same length. Why not print out `strlen()` of both of those values. You won't see invisible control characters (if they exist) using `cout`. A `strlen` will tell you right away if the strings have something in them that you can't see. – PaulMcKenzie Feb 08 '16 at 04:49
  • @PaulMcKenzie I did a strlen and it showed me that the date variable has nothing in it. I assume this means there is a problem when the string is being put into the variable. – TheHobospider Feb 08 '16 at 04:58
  • 1
    This problem should have been obvious if you simply stepped through the code using the debugger. You would have seen that your input was not correct. – PaulMcKenzie Feb 08 '16 at 05:00

1 Answers1

1

Here's your problem:

cin >> query;
//(in this example stored in char query[7])
// and display the found low temprature.

cin.getline(query, 7, '\n');

You read in query from standard input...and then you read it in again. The second time, there's nothing left on that line but the '\n' -- so it reads in an empty string.

I removed the cin.getline and got a sensible result.

The way to detect this is to step through using a debugger, and note that query was "" instead of the date typed in.

Topological Sort
  • 2,733
  • 2
  • 27
  • 54