Problem with comparing single character with character in string by a loop c++











up vote
0
down vote

favorite












I am trying to learn c++ and one of the assignments is to ask the user for a letter - then ask for a string of text and count how many times the 1st letter is repeated in the string of text.



I have written some code that successfully gets to the point of asking for the letter & the string of text - I can display both



I can traverse the string of text counting how many letters there are in the string. When I try to add an if check to compare the current letter in the string inside the loop with the letter 1st asked for - I get this compile error:



error: ISO C++ forbids comparison between pointer and integer [-fpermissive] 
if (textToCount[i] == letterToCount)


this is the full code I have written



char getLetterToCount(char letterToCount);
char getTextToCount(char textToCount);
int countLetters(char letterToCount, char textToCount);

int main()
{
char letterToCount[1];
getLetterToCount(letterToCount);
char textToCount[256];
cin.ignore();
getTextToCount(textToCount);
countLetters(letterToCount, textToCount);
return 0;
}

char getLetterToCount(char letterToCount)
{
cout << "Enter a letter: ";
cin >> letterToCount;
}

char getTextToCount(char textToCount)
{
cout << "Enter text: ";
cin.getline(textToCount, 256);
}

int countLetters(char letterToCount, char textToCount)
{
int numChrsInString = 0;
int numTimesChrtoCountrepeated = 0;
for (int i = 0; textToCount[i] != ''; i++)
{
if (textToCount[i] == letterToCount)
{
numTimesChrtoCountrepeated++;
}
}
cout << "num chrs in string: "
<< numChrsInString
<< "num times chr counted: "
<< numTimesChrtoCountrepeated
<< endl;
}


I did a fair bit of output to try and figure out what was wrong with these - I pulled the code for that out because it made it a bit more confusing.



BUT the compile error explains what is wrong, I just don't understand why it is wrong because the things I am trying to compare are BOTH text letters...



It would be great if someone who knows c++ can explain what I am doing wrong










share|improve this question


























    up vote
    0
    down vote

    favorite












    I am trying to learn c++ and one of the assignments is to ask the user for a letter - then ask for a string of text and count how many times the 1st letter is repeated in the string of text.



    I have written some code that successfully gets to the point of asking for the letter & the string of text - I can display both



    I can traverse the string of text counting how many letters there are in the string. When I try to add an if check to compare the current letter in the string inside the loop with the letter 1st asked for - I get this compile error:



    error: ISO C++ forbids comparison between pointer and integer [-fpermissive] 
    if (textToCount[i] == letterToCount)


    this is the full code I have written



    char getLetterToCount(char letterToCount);
    char getTextToCount(char textToCount);
    int countLetters(char letterToCount, char textToCount);

    int main()
    {
    char letterToCount[1];
    getLetterToCount(letterToCount);
    char textToCount[256];
    cin.ignore();
    getTextToCount(textToCount);
    countLetters(letterToCount, textToCount);
    return 0;
    }

    char getLetterToCount(char letterToCount)
    {
    cout << "Enter a letter: ";
    cin >> letterToCount;
    }

    char getTextToCount(char textToCount)
    {
    cout << "Enter text: ";
    cin.getline(textToCount, 256);
    }

    int countLetters(char letterToCount, char textToCount)
    {
    int numChrsInString = 0;
    int numTimesChrtoCountrepeated = 0;
    for (int i = 0; textToCount[i] != ''; i++)
    {
    if (textToCount[i] == letterToCount)
    {
    numTimesChrtoCountrepeated++;
    }
    }
    cout << "num chrs in string: "
    << numChrsInString
    << "num times chr counted: "
    << numTimesChrtoCountrepeated
    << endl;
    }


    I did a fair bit of output to try and figure out what was wrong with these - I pulled the code for that out because it made it a bit more confusing.



    BUT the compile error explains what is wrong, I just don't understand why it is wrong because the things I am trying to compare are BOTH text letters...



    It would be great if someone who knows c++ can explain what I am doing wrong










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am trying to learn c++ and one of the assignments is to ask the user for a letter - then ask for a string of text and count how many times the 1st letter is repeated in the string of text.



      I have written some code that successfully gets to the point of asking for the letter & the string of text - I can display both



      I can traverse the string of text counting how many letters there are in the string. When I try to add an if check to compare the current letter in the string inside the loop with the letter 1st asked for - I get this compile error:



      error: ISO C++ forbids comparison between pointer and integer [-fpermissive] 
      if (textToCount[i] == letterToCount)


      this is the full code I have written



      char getLetterToCount(char letterToCount);
      char getTextToCount(char textToCount);
      int countLetters(char letterToCount, char textToCount);

      int main()
      {
      char letterToCount[1];
      getLetterToCount(letterToCount);
      char textToCount[256];
      cin.ignore();
      getTextToCount(textToCount);
      countLetters(letterToCount, textToCount);
      return 0;
      }

      char getLetterToCount(char letterToCount)
      {
      cout << "Enter a letter: ";
      cin >> letterToCount;
      }

      char getTextToCount(char textToCount)
      {
      cout << "Enter text: ";
      cin.getline(textToCount, 256);
      }

      int countLetters(char letterToCount, char textToCount)
      {
      int numChrsInString = 0;
      int numTimesChrtoCountrepeated = 0;
      for (int i = 0; textToCount[i] != ''; i++)
      {
      if (textToCount[i] == letterToCount)
      {
      numTimesChrtoCountrepeated++;
      }
      }
      cout << "num chrs in string: "
      << numChrsInString
      << "num times chr counted: "
      << numTimesChrtoCountrepeated
      << endl;
      }


      I did a fair bit of output to try and figure out what was wrong with these - I pulled the code for that out because it made it a bit more confusing.



      BUT the compile error explains what is wrong, I just don't understand why it is wrong because the things I am trying to compare are BOTH text letters...



      It would be great if someone who knows c++ can explain what I am doing wrong










      share|improve this question













      I am trying to learn c++ and one of the assignments is to ask the user for a letter - then ask for a string of text and count how many times the 1st letter is repeated in the string of text.



      I have written some code that successfully gets to the point of asking for the letter & the string of text - I can display both



      I can traverse the string of text counting how many letters there are in the string. When I try to add an if check to compare the current letter in the string inside the loop with the letter 1st asked for - I get this compile error:



      error: ISO C++ forbids comparison between pointer and integer [-fpermissive] 
      if (textToCount[i] == letterToCount)


      this is the full code I have written



      char getLetterToCount(char letterToCount);
      char getTextToCount(char textToCount);
      int countLetters(char letterToCount, char textToCount);

      int main()
      {
      char letterToCount[1];
      getLetterToCount(letterToCount);
      char textToCount[256];
      cin.ignore();
      getTextToCount(textToCount);
      countLetters(letterToCount, textToCount);
      return 0;
      }

      char getLetterToCount(char letterToCount)
      {
      cout << "Enter a letter: ";
      cin >> letterToCount;
      }

      char getTextToCount(char textToCount)
      {
      cout << "Enter text: ";
      cin.getline(textToCount, 256);
      }

      int countLetters(char letterToCount, char textToCount)
      {
      int numChrsInString = 0;
      int numTimesChrtoCountrepeated = 0;
      for (int i = 0; textToCount[i] != ''; i++)
      {
      if (textToCount[i] == letterToCount)
      {
      numTimesChrtoCountrepeated++;
      }
      }
      cout << "num chrs in string: "
      << numChrsInString
      << "num times chr counted: "
      << numTimesChrtoCountrepeated
      << endl;
      }


      I did a fair bit of output to try and figure out what was wrong with these - I pulled the code for that out because it made it a bit more confusing.



      BUT the compile error explains what is wrong, I just don't understand why it is wrong because the things I am trying to compare are BOTH text letters...



      It would be great if someone who knows c++ can explain what I am doing wrong







      c++ compiler-errors






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 4:19









      kiltannen

      241213




      241213
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote













          You are comparing a char with a pointer to char
          Use:



          if (textToCount[i] == letterToCount[0])
          ~~~


          Note: There are few obvious nitpicking but above is the main compiler error cause






          share|improve this answer

















          • 1




            Brilliant thank you! this solved it much appreciated. I'm sure there are also other aspects of the code where my style and syntax could be better - I'm still very much a newbie in c++. Thank you for the help - and the pointers... [no pun intended... ;-) ]
            – kiltannen
            Nov 11 at 4:30




















          up vote
          1
          down vote













          In C++, arrays are pointers. C++ considers lettertocount to be a pointer because you're passing it as an array. You don't want to pass that; you want to pass just a character:



          int countLetters(char letterToCount, char textToCount)


          A bigger question to my mind is, why do you think you have to pass lettertocount as an array? Apparently it's just one letter. When you invoke this function do you eventually want to count multiple letters?






          share|improve this answer





















          • Hmmm... Probably the best response I can give is that I am not fully understanding what I am doing just yet. I will have a go at taking on board your feedback to improve this actual code. I think my thought process was that I needed to pass it as a parameter... BUT like I say - I will try to figure out what I was doing wrong based on this feedback.
            – kiltannen
            Nov 11 at 4:35












          • You do need to pass letterToCount as a parameter, but based on your description of the problem, it only needs to be a char, not a char .
            – John Perry
            Nov 11 at 4:40











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245798%2fproblem-with-comparing-single-character-with-character-in-string-by-a-loop-c%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote













          You are comparing a char with a pointer to char
          Use:



          if (textToCount[i] == letterToCount[0])
          ~~~


          Note: There are few obvious nitpicking but above is the main compiler error cause






          share|improve this answer

















          • 1




            Brilliant thank you! this solved it much appreciated. I'm sure there are also other aspects of the code where my style and syntax could be better - I'm still very much a newbie in c++. Thank you for the help - and the pointers... [no pun intended... ;-) ]
            – kiltannen
            Nov 11 at 4:30

















          up vote
          1
          down vote













          You are comparing a char with a pointer to char
          Use:



          if (textToCount[i] == letterToCount[0])
          ~~~


          Note: There are few obvious nitpicking but above is the main compiler error cause






          share|improve this answer

















          • 1




            Brilliant thank you! this solved it much appreciated. I'm sure there are also other aspects of the code where my style and syntax could be better - I'm still very much a newbie in c++. Thank you for the help - and the pointers... [no pun intended... ;-) ]
            – kiltannen
            Nov 11 at 4:30















          up vote
          1
          down vote










          up vote
          1
          down vote









          You are comparing a char with a pointer to char
          Use:



          if (textToCount[i] == letterToCount[0])
          ~~~


          Note: There are few obvious nitpicking but above is the main compiler error cause






          share|improve this answer












          You are comparing a char with a pointer to char
          Use:



          if (textToCount[i] == letterToCount[0])
          ~~~


          Note: There are few obvious nitpicking but above is the main compiler error cause







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 4:27









          P0W

          32.7k74793




          32.7k74793








          • 1




            Brilliant thank you! this solved it much appreciated. I'm sure there are also other aspects of the code where my style and syntax could be better - I'm still very much a newbie in c++. Thank you for the help - and the pointers... [no pun intended... ;-) ]
            – kiltannen
            Nov 11 at 4:30
















          • 1




            Brilliant thank you! this solved it much appreciated. I'm sure there are also other aspects of the code where my style and syntax could be better - I'm still very much a newbie in c++. Thank you for the help - and the pointers... [no pun intended... ;-) ]
            – kiltannen
            Nov 11 at 4:30










          1




          1




          Brilliant thank you! this solved it much appreciated. I'm sure there are also other aspects of the code where my style and syntax could be better - I'm still very much a newbie in c++. Thank you for the help - and the pointers... [no pun intended... ;-) ]
          – kiltannen
          Nov 11 at 4:30






          Brilliant thank you! this solved it much appreciated. I'm sure there are also other aspects of the code where my style and syntax could be better - I'm still very much a newbie in c++. Thank you for the help - and the pointers... [no pun intended... ;-) ]
          – kiltannen
          Nov 11 at 4:30














          up vote
          1
          down vote













          In C++, arrays are pointers. C++ considers lettertocount to be a pointer because you're passing it as an array. You don't want to pass that; you want to pass just a character:



          int countLetters(char letterToCount, char textToCount)


          A bigger question to my mind is, why do you think you have to pass lettertocount as an array? Apparently it's just one letter. When you invoke this function do you eventually want to count multiple letters?






          share|improve this answer





















          • Hmmm... Probably the best response I can give is that I am not fully understanding what I am doing just yet. I will have a go at taking on board your feedback to improve this actual code. I think my thought process was that I needed to pass it as a parameter... BUT like I say - I will try to figure out what I was doing wrong based on this feedback.
            – kiltannen
            Nov 11 at 4:35












          • You do need to pass letterToCount as a parameter, but based on your description of the problem, it only needs to be a char, not a char .
            – John Perry
            Nov 11 at 4:40















          up vote
          1
          down vote













          In C++, arrays are pointers. C++ considers lettertocount to be a pointer because you're passing it as an array. You don't want to pass that; you want to pass just a character:



          int countLetters(char letterToCount, char textToCount)


          A bigger question to my mind is, why do you think you have to pass lettertocount as an array? Apparently it's just one letter. When you invoke this function do you eventually want to count multiple letters?






          share|improve this answer





















          • Hmmm... Probably the best response I can give is that I am not fully understanding what I am doing just yet. I will have a go at taking on board your feedback to improve this actual code. I think my thought process was that I needed to pass it as a parameter... BUT like I say - I will try to figure out what I was doing wrong based on this feedback.
            – kiltannen
            Nov 11 at 4:35












          • You do need to pass letterToCount as a parameter, but based on your description of the problem, it only needs to be a char, not a char .
            – John Perry
            Nov 11 at 4:40













          up vote
          1
          down vote










          up vote
          1
          down vote









          In C++, arrays are pointers. C++ considers lettertocount to be a pointer because you're passing it as an array. You don't want to pass that; you want to pass just a character:



          int countLetters(char letterToCount, char textToCount)


          A bigger question to my mind is, why do you think you have to pass lettertocount as an array? Apparently it's just one letter. When you invoke this function do you eventually want to count multiple letters?






          share|improve this answer












          In C++, arrays are pointers. C++ considers lettertocount to be a pointer because you're passing it as an array. You don't want to pass that; you want to pass just a character:



          int countLetters(char letterToCount, char textToCount)


          A bigger question to my mind is, why do you think you have to pass lettertocount as an array? Apparently it's just one letter. When you invoke this function do you eventually want to count multiple letters?







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 4:28









          John Perry

          1,341718




          1,341718












          • Hmmm... Probably the best response I can give is that I am not fully understanding what I am doing just yet. I will have a go at taking on board your feedback to improve this actual code. I think my thought process was that I needed to pass it as a parameter... BUT like I say - I will try to figure out what I was doing wrong based on this feedback.
            – kiltannen
            Nov 11 at 4:35












          • You do need to pass letterToCount as a parameter, but based on your description of the problem, it only needs to be a char, not a char .
            – John Perry
            Nov 11 at 4:40


















          • Hmmm... Probably the best response I can give is that I am not fully understanding what I am doing just yet. I will have a go at taking on board your feedback to improve this actual code. I think my thought process was that I needed to pass it as a parameter... BUT like I say - I will try to figure out what I was doing wrong based on this feedback.
            – kiltannen
            Nov 11 at 4:35












          • You do need to pass letterToCount as a parameter, but based on your description of the problem, it only needs to be a char, not a char .
            – John Perry
            Nov 11 at 4:40
















          Hmmm... Probably the best response I can give is that I am not fully understanding what I am doing just yet. I will have a go at taking on board your feedback to improve this actual code. I think my thought process was that I needed to pass it as a parameter... BUT like I say - I will try to figure out what I was doing wrong based on this feedback.
          – kiltannen
          Nov 11 at 4:35






          Hmmm... Probably the best response I can give is that I am not fully understanding what I am doing just yet. I will have a go at taking on board your feedback to improve this actual code. I think my thought process was that I needed to pass it as a parameter... BUT like I say - I will try to figure out what I was doing wrong based on this feedback.
          – kiltannen
          Nov 11 at 4:35














          You do need to pass letterToCount as a parameter, but based on your description of the problem, it only needs to be a char, not a char .
          – John Perry
          Nov 11 at 4:40




          You do need to pass letterToCount as a parameter, but based on your description of the problem, it only needs to be a char, not a char .
          – John Perry
          Nov 11 at 4:40


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245798%2fproblem-with-comparing-single-character-with-character-in-string-by-a-loop-c%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Danny Elfman

          Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues