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
c++ compiler-errors
add a comment |
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
c++ compiler-errors
add a comment |
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
c++ compiler-errors
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
c++ compiler-errors
asked Nov 11 at 4:19
kiltannen
241113
241113
add a comment |
add a comment |
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
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
add a comment |
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?
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 passletterToCount
as a parameter, but based on your description of the problem, it only needs to be achar
, not achar
.
– John Perry
Nov 11 at 4:40
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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?
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 passletterToCount
as a parameter, but based on your description of the problem, it only needs to be achar
, not achar
.
– John Perry
Nov 11 at 4:40
add a comment |
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?
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 passletterToCount
as a parameter, but based on your description of the problem, it only needs to be achar
, not achar
.
– John Perry
Nov 11 at 4:40
add a comment |
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?
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?
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 passletterToCount
as a parameter, but based on your description of the problem, it only needs to be achar
, not achar
.
– John Perry
Nov 11 at 4:40
add a comment |
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 passletterToCount
as a parameter, but based on your description of the problem, it only needs to be achar
, not achar
.
– 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
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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