Comparing Help!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • midknight5
    New Member
    • Oct 2007
    • 25

    Comparing Help!

    I am writing a program to where after a file is open, a temperature is put into a variable char temp[4000]; wile the program continues I need to check to see if there is a - in temp so that when I have all the temperatures it wont count -. what would be a way for me to check to see if there is a - in temp.

    Attempted code:
    if(temp.c_str() = "-")

    If needed I can post the full code.
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by midknight5
    I am writing a program to where after a file is open, a temperature is put into a variable char temp[4000]; wile the program continues I need to check to see if there is a - in temp so that when I have all the temperatures it wont count -. what would be a way for me to check to see if there is a - in temp.

    Attempted code:
    if(temp.c_str() = "-")

    If needed I can post the full code.
    If u want to search for acharacter inside a character array then the code u have posted wont help.
    Use strchr function to check whether it contains a - in it.

    Raghuram

    Comment

    • midknight5
      New Member
      • Oct 2007
      • 25

      #3
      Im afraid im not familiar with that function, how does it work and what exactly does it do?

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        You can check out the strchr() function here.

        Comment

        • midknight5
          New Member
          • Oct 2007
          • 25

          #5
          I dont think I did it correctly, sorry that website is a tad bit confusing to me. I've only been working with C++ for about two months now. (CompSci Freshman) Here is what I gathered though:

          (strchr(temp, '-' ))

          That says to check inside temp for a - right?

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            Well, that correctly calls the function. Remember, though, that you are trying to see if a '-' is in it, so you need to capture the return value. Or do you?

            Note that strchr() returns a pointer to the first occurrence of the character passed, and a NULL pointer if it is not found. Since you don't care were in the string it is, only that it is present, you can merely check if the return value is NULL (no '-' found), or not ('-' found).

            Comment

            • midknight5
              New Member
              • Oct 2007
              • 25

              #7
              When you say returns the pointer to the first occurrence, does that mean no matter where it is in the file that the pointers moves back to that character unless it is not found?
              If so that isnt what I need because my program checks a multitude of tempuratures, and I just need to be able to check to see if the temp is - because if it is then I dont want to take count of it when determining the average of the tempuratures.

              Code:
              if(strchr(temp, '-' ))
              {
               count++;
               sum+=atof(temp);
               cout << temp << " " << sum << endl;
              }

              Comment

              • Ganon11
                Recognized Expert Specialist
                • Oct 2006
                • 3651

                #8
                It will return a char* pointer to the first occurrence of '-' in temp. temp, I assume, is the temperature you are trying to find, right? So using strchr() does not affect where your FILE* (or ifstream, or whatever you are using) is pointing to in the file - it generates a new pointer that points somewhere inside temp, a cstring.

                Your code is actually doing the opposite of what you think (if it's working at all). I'd assume that a NULL return value of strchr would be counted as false, and anything else as true, so that snippet of code is only executing when a '-' was found (i.e. the temperature was negative). You should reverse that, so if '-' was NOT found (i.e. a positive temperature), that code snippet executes.

                Comment

                • midknight5
                  New Member
                  • Oct 2007
                  • 25

                  #9
                  Oh man it worked! Im getting the correct information now! Thank you so much!

                  Comment

                  • oler1s
                    Recognized Expert Contributor
                    • Aug 2007
                    • 671

                    #10
                    Question folks, if we have a C++ string on hand, why does no one think temp[0]=='-' is a good idea?

                    Comment

                    • Ganon11
                      Recognized Expert Specialist
                      • Oct 2006
                      • 3651

                      #11
                      That's something I was wondering, myself :-\

                      But the most important part of any code solution is that it works, and it appears the OP got his/her code working. Using std::string is probably a better/easier option, but will end up giving the same results.

                      Comment

                      • midknight5
                        New Member
                        • Oct 2007
                        • 25

                        #12
                        Originally posted by oler1s
                        Question folks, if we have a C++ string on hand, why does no one think temp[0]=='-' is a good idea?
                        I actually tried something like that but received an error about conversion or the like, my proffessor said that it wasnt a good way to try and solve the problem so I just threw it out.

                        Comment

                        • weaknessforcats
                          Recognized Expert Expert
                          • Mar 2007
                          • 9214

                          #13
                          Originally posted by miodnight5
                          Attempted code:
                          if(temp.c_str() = "-")
                          This code tells me you are using a C++ string object. strchr is an old C function that works with C-strings. A C++ string object may not be in the sam format as a C-string hence the need for the c_str() method.

                          To find a character in a string object, you use the find method():
                          [code=c]
                          size_t result = temp.find('-');
                          if (result != string::npos)
                          {
                          cout << "character not found" << endl;
                          //etc...
                          }
                          cout << "character found at position " << result << endl;
                          //etc...
                          [/code]

                          Try to avoid the C string library in C++. It's not safe and many functions have been deprecated for security reasons.

                          Comment

                          Working...