Many errors from mixing function calls with functions
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm trying to create a program that counts the number of vowels and consonants in a word given by the user. I used for functions like strlen() to get the length of my user input array for iteration. I also used bits/stdc++.h, so I could call the count() function for any occurrence of a vowel in the user input.
To check for the occurrences of vowels, I tried a couple functions starting from count, to find_first_of, to find(), to count() again. My first errors started by saying that it doesn't recognize the call to strlen(). I checked to make sure that I included the right package to use strlen(), but that doesn't seem to be the problem. I'm running this program on my MacBook Pro with High Sierra.
#include <iostream>//std::cout
#include <string>//std::string
#include <cstdlib>
#include <set>// std::size_t
#include "/Users/richardlopez/Desktop/stdc++.h"
using namespace std;
int main(){
string input = "hello";
cout << " The input is " << input << endl;
std::size_t vowelsFound = 0;
/*cout << " Enter a word, and we will count the vowels and consonants: ";
cin >> input;
cout << " You entered " << input << endl;*/
char cons = {'b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','z'};
char vows = {'a','e','i','o','u','y'};
std::size_t n = strlen(input);
std::size_t v = strlen(vows);
for(int i = 0; i < v; i++){
if(count(input,input + n,vows[i]) != 0){
vowelsFound += count(input,input + n, vows[i]);//count of vowels in vows encountered in input
cout << " The vowel(s) found is " << vowelsFound << endl;
}
else{
cout << " The vowel " << vows[i] << " is not found in input " << endl;
}
}
cout << " The amount of vowels found is " << vowelsFound << endl;
cout << " The expected amount of vowels found is 2 " << endl;
}
I hardcoded the phrase "hello" for input, so when all is said and done the vowel count should be 2.
c++
add a comment |
I'm trying to create a program that counts the number of vowels and consonants in a word given by the user. I used for functions like strlen() to get the length of my user input array for iteration. I also used bits/stdc++.h, so I could call the count() function for any occurrence of a vowel in the user input.
To check for the occurrences of vowels, I tried a couple functions starting from count, to find_first_of, to find(), to count() again. My first errors started by saying that it doesn't recognize the call to strlen(). I checked to make sure that I included the right package to use strlen(), but that doesn't seem to be the problem. I'm running this program on my MacBook Pro with High Sierra.
#include <iostream>//std::cout
#include <string>//std::string
#include <cstdlib>
#include <set>// std::size_t
#include "/Users/richardlopez/Desktop/stdc++.h"
using namespace std;
int main(){
string input = "hello";
cout << " The input is " << input << endl;
std::size_t vowelsFound = 0;
/*cout << " Enter a word, and we will count the vowels and consonants: ";
cin >> input;
cout << " You entered " << input << endl;*/
char cons = {'b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','z'};
char vows = {'a','e','i','o','u','y'};
std::size_t n = strlen(input);
std::size_t v = strlen(vows);
for(int i = 0; i < v; i++){
if(count(input,input + n,vows[i]) != 0){
vowelsFound += count(input,input + n, vows[i]);//count of vowels in vows encountered in input
cout << " The vowel(s) found is " << vowelsFound << endl;
}
else{
cout << " The vowel " << vows[i] << " is not found in input " << endl;
}
}
cout << " The amount of vowels found is " << vowelsFound << endl;
cout << " The expected amount of vowels found is 2 " << endl;
}
I hardcoded the phrase "hello" for input, so when all is said and done the vowel count should be 2.
c++
6
That's very bad: "/Users/richardlopez/Desktop/stdc++.h"
– Matthieu Brucher
Jan 4 at 7:49
strlen(input)
This only works with 0-terminated strings, which you DON'T have. Use avector<char>
and get its size instead. There are probably other issues as well, so not an answer at this stage.
– Matthieu Brucher
Jan 4 at 7:50
I'm also not sure when I should be using std::size_t. I initially just defined n and v as type int, and thought that would suffice. I understand that std::size_t is for unsigned integers, but my integers will never be negative, so I'm not sure why it matters that I use std::size_t at all. I'm really not sure what its purpose is to begin with. I've never seen that format for defining a type.
– pancham2016
Jan 4 at 7:51
1
Really the simplest thing is to forget about char arrays and use std::string throughout. They are the safest and most flexible and you'll only have one thing to learn.
– john
Jan 4 at 8:05
add a comment |
I'm trying to create a program that counts the number of vowels and consonants in a word given by the user. I used for functions like strlen() to get the length of my user input array for iteration. I also used bits/stdc++.h, so I could call the count() function for any occurrence of a vowel in the user input.
To check for the occurrences of vowels, I tried a couple functions starting from count, to find_first_of, to find(), to count() again. My first errors started by saying that it doesn't recognize the call to strlen(). I checked to make sure that I included the right package to use strlen(), but that doesn't seem to be the problem. I'm running this program on my MacBook Pro with High Sierra.
#include <iostream>//std::cout
#include <string>//std::string
#include <cstdlib>
#include <set>// std::size_t
#include "/Users/richardlopez/Desktop/stdc++.h"
using namespace std;
int main(){
string input = "hello";
cout << " The input is " << input << endl;
std::size_t vowelsFound = 0;
/*cout << " Enter a word, and we will count the vowels and consonants: ";
cin >> input;
cout << " You entered " << input << endl;*/
char cons = {'b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','z'};
char vows = {'a','e','i','o','u','y'};
std::size_t n = strlen(input);
std::size_t v = strlen(vows);
for(int i = 0; i < v; i++){
if(count(input,input + n,vows[i]) != 0){
vowelsFound += count(input,input + n, vows[i]);//count of vowels in vows encountered in input
cout << " The vowel(s) found is " << vowelsFound << endl;
}
else{
cout << " The vowel " << vows[i] << " is not found in input " << endl;
}
}
cout << " The amount of vowels found is " << vowelsFound << endl;
cout << " The expected amount of vowels found is 2 " << endl;
}
I hardcoded the phrase "hello" for input, so when all is said and done the vowel count should be 2.
c++
I'm trying to create a program that counts the number of vowels and consonants in a word given by the user. I used for functions like strlen() to get the length of my user input array for iteration. I also used bits/stdc++.h, so I could call the count() function for any occurrence of a vowel in the user input.
To check for the occurrences of vowels, I tried a couple functions starting from count, to find_first_of, to find(), to count() again. My first errors started by saying that it doesn't recognize the call to strlen(). I checked to make sure that I included the right package to use strlen(), but that doesn't seem to be the problem. I'm running this program on my MacBook Pro with High Sierra.
#include <iostream>//std::cout
#include <string>//std::string
#include <cstdlib>
#include <set>// std::size_t
#include "/Users/richardlopez/Desktop/stdc++.h"
using namespace std;
int main(){
string input = "hello";
cout << " The input is " << input << endl;
std::size_t vowelsFound = 0;
/*cout << " Enter a word, and we will count the vowels and consonants: ";
cin >> input;
cout << " You entered " << input << endl;*/
char cons = {'b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','z'};
char vows = {'a','e','i','o','u','y'};
std::size_t n = strlen(input);
std::size_t v = strlen(vows);
for(int i = 0; i < v; i++){
if(count(input,input + n,vows[i]) != 0){
vowelsFound += count(input,input + n, vows[i]);//count of vowels in vows encountered in input
cout << " The vowel(s) found is " << vowelsFound << endl;
}
else{
cout << " The vowel " << vows[i] << " is not found in input " << endl;
}
}
cout << " The amount of vowels found is " << vowelsFound << endl;
cout << " The expected amount of vowels found is 2 " << endl;
}
I hardcoded the phrase "hello" for input, so when all is said and done the vowel count should be 2.
c++
c++
edited Jan 4 at 7:49
Matthieu Brucher
17.7k52445
17.7k52445
asked Jan 4 at 7:48
pancham2016pancham2016
11
11
6
That's very bad: "/Users/richardlopez/Desktop/stdc++.h"
– Matthieu Brucher
Jan 4 at 7:49
strlen(input)
This only works with 0-terminated strings, which you DON'T have. Use avector<char>
and get its size instead. There are probably other issues as well, so not an answer at this stage.
– Matthieu Brucher
Jan 4 at 7:50
I'm also not sure when I should be using std::size_t. I initially just defined n and v as type int, and thought that would suffice. I understand that std::size_t is for unsigned integers, but my integers will never be negative, so I'm not sure why it matters that I use std::size_t at all. I'm really not sure what its purpose is to begin with. I've never seen that format for defining a type.
– pancham2016
Jan 4 at 7:51
1
Really the simplest thing is to forget about char arrays and use std::string throughout. They are the safest and most flexible and you'll only have one thing to learn.
– john
Jan 4 at 8:05
add a comment |
6
That's very bad: "/Users/richardlopez/Desktop/stdc++.h"
– Matthieu Brucher
Jan 4 at 7:49
strlen(input)
This only works with 0-terminated strings, which you DON'T have. Use avector<char>
and get its size instead. There are probably other issues as well, so not an answer at this stage.
– Matthieu Brucher
Jan 4 at 7:50
I'm also not sure when I should be using std::size_t. I initially just defined n and v as type int, and thought that would suffice. I understand that std::size_t is for unsigned integers, but my integers will never be negative, so I'm not sure why it matters that I use std::size_t at all. I'm really not sure what its purpose is to begin with. I've never seen that format for defining a type.
– pancham2016
Jan 4 at 7:51
1
Really the simplest thing is to forget about char arrays and use std::string throughout. They are the safest and most flexible and you'll only have one thing to learn.
– john
Jan 4 at 8:05
6
6
That's very bad: "/Users/richardlopez/Desktop/stdc++.h"
– Matthieu Brucher
Jan 4 at 7:49
That's very bad: "/Users/richardlopez/Desktop/stdc++.h"
– Matthieu Brucher
Jan 4 at 7:49
strlen(input)
This only works with 0-terminated strings, which you DON'T have. Use a vector<char>
and get its size instead. There are probably other issues as well, so not an answer at this stage.– Matthieu Brucher
Jan 4 at 7:50
strlen(input)
This only works with 0-terminated strings, which you DON'T have. Use a vector<char>
and get its size instead. There are probably other issues as well, so not an answer at this stage.– Matthieu Brucher
Jan 4 at 7:50
I'm also not sure when I should be using std::size_t. I initially just defined n and v as type int, and thought that would suffice. I understand that std::size_t is for unsigned integers, but my integers will never be negative, so I'm not sure why it matters that I use std::size_t at all. I'm really not sure what its purpose is to begin with. I've never seen that format for defining a type.
– pancham2016
Jan 4 at 7:51
I'm also not sure when I should be using std::size_t. I initially just defined n and v as type int, and thought that would suffice. I understand that std::size_t is for unsigned integers, but my integers will never be negative, so I'm not sure why it matters that I use std::size_t at all. I'm really not sure what its purpose is to begin with. I've never seen that format for defining a type.
– pancham2016
Jan 4 at 7:51
1
1
Really the simplest thing is to forget about char arrays and use std::string throughout. They are the safest and most flexible and you'll only have one thing to learn.
– john
Jan 4 at 8:05
Really the simplest thing is to forget about char arrays and use std::string throughout. They are the safest and most flexible and you'll only have one thing to learn.
– john
Jan 4 at 8:05
add a comment |
3 Answers
3
active
oldest
votes
There are a number of issues in your code:
- I don't know what is in the
"/Users/richardlopez/Desktop/stdc++.h"
include but it is unlikely that it is a good thing to include. You probably want#include <cstring>
to getstrlen
- you can't use
strlen
on astd::string
, it only works on null terminated character arrays. You should just useinput.size()
instead. - you also shouldn't use
strlen
onvows
as although it is a character array so will compile it is not null terminated so the return value ofstrlen
is undefined. You can usesizeof(vows)
or just makevows
astd::string
orstd::vector<char>
instead.
count(input,input + n,vows[i])
is not correct.input + n
doesn't compile. What you presumably meant iscount(input.begin(),input.end(),vows[i])
Correcting the above issues and using modern c++ results in the simpilifed code:
#include <iostream>
#include <string>
#include <algorithm>
int main()
{
std::string input = "hello";
std::cout << " The input is " << input << "n";
std::size_t vowelsFound = 0;
std::string cons = "bcdfghjklmnpqrstvwxyz";
std::string vows = "aeiou";
for ( auto v : vows )
{
size_t c = std::count(input.begin(), input.end(), v);
if ( c != 0 )
{
vowelsFound += c;
std::cout << "found " << c << " " << v << "n";
}
else
{
std::cout << " The vowel " << v << " is not found in inputn";
}
}
std::cout << " The amount of vowels found is " << vowelsFound << "n";
std::cout << " The expected amount of vowels found is 2n";
}
If all you need is the total number of vowels you can just use:
std::cout << " The amount of vowels found is " << std::count_if( input.begin(), input.end(), [&](char c)
{
return vows.find(c) != std::string::npos;
}) << "n";
add a comment |
You could use the partition algorithm.
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
int main() {
std::string input = "hello";
std::cout << " The input is " << input << std::endl;
std::vector<char> vows = {'a','e','i','o','u','y'};
auto it_vows_end = std::partition(input.begin(), input.end(), [vows](char c){return find(vows.begin(), vows.end(), c) != vows.end();});
std::cout << " The amount of vowels found is " << std::distance(input.begin(), it_vows_end) << std::endl;
std::cout << " The expected amount of vowels found is 2 " << std::endl;
// If you also want to count consonants (for example if the input can also have digits or punctuation marks)
std::vector<char> cons = {'b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','z'};
auto it_cons_end = std::partition(it_vows_end, input.end(), [cons](char c){return find(cons.begin(), cons.end(), c) != cons.end();});
std::cout << " The amount of consonants found is " << std::distance(it_vows_end, it_cons_end) << std::endl;
}
Output:
The input is hello
The amount of vowels found is 2
The expected amount of vowels found is 2
The amount of consonants found is 3
std::size_t is the return type of sizeof. So it's only natural to use it for iterating over containers. Also, as it is a standard type used by some libraries, it makes code easier to read.
add a comment |
Several issues, on top of "/Users/richardlopez/Desktop/stdc++.h" . If you need a specific function, look the reference and include the appropriate official C++ header for the functionality.
First strlen
is for 0-terminated char*
. It doesn't work on input
. For that, just use:
auto n = input.size();
Then the same for vows
:
std::vector<char> vows{'a','e','i','o','u','y'};
auto v = vows.size();
Then count
is also different:
std::count(std::begin(input) ,std::end(input), vows[i])
If you have C++17, do:
if(auto count = std::count(std::begin(input), std::end(input), vows[i]); count > 0)
{
vowelsFound += count;
std::cout << count << std::endl;
}
2
SimplyvowelsFound += std::count(std::begin(input), std::end(input), vows[i]);
– john
Jan 4 at 8:08
OP had some debugging info to add in the condition that I didn't reproduce (I should have). That would be the proper refactoring indeed.
– Matthieu Brucher
Jan 4 at 9:27
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f54034945%2fmany-errors-from-mixing-cstdlib-function-calls-with-string-functions%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
There are a number of issues in your code:
- I don't know what is in the
"/Users/richardlopez/Desktop/stdc++.h"
include but it is unlikely that it is a good thing to include. You probably want#include <cstring>
to getstrlen
- you can't use
strlen
on astd::string
, it only works on null terminated character arrays. You should just useinput.size()
instead. - you also shouldn't use
strlen
onvows
as although it is a character array so will compile it is not null terminated so the return value ofstrlen
is undefined. You can usesizeof(vows)
or just makevows
astd::string
orstd::vector<char>
instead.
count(input,input + n,vows[i])
is not correct.input + n
doesn't compile. What you presumably meant iscount(input.begin(),input.end(),vows[i])
Correcting the above issues and using modern c++ results in the simpilifed code:
#include <iostream>
#include <string>
#include <algorithm>
int main()
{
std::string input = "hello";
std::cout << " The input is " << input << "n";
std::size_t vowelsFound = 0;
std::string cons = "bcdfghjklmnpqrstvwxyz";
std::string vows = "aeiou";
for ( auto v : vows )
{
size_t c = std::count(input.begin(), input.end(), v);
if ( c != 0 )
{
vowelsFound += c;
std::cout << "found " << c << " " << v << "n";
}
else
{
std::cout << " The vowel " << v << " is not found in inputn";
}
}
std::cout << " The amount of vowels found is " << vowelsFound << "n";
std::cout << " The expected amount of vowels found is 2n";
}
If all you need is the total number of vowels you can just use:
std::cout << " The amount of vowels found is " << std::count_if( input.begin(), input.end(), [&](char c)
{
return vows.find(c) != std::string::npos;
}) << "n";
add a comment |
There are a number of issues in your code:
- I don't know what is in the
"/Users/richardlopez/Desktop/stdc++.h"
include but it is unlikely that it is a good thing to include. You probably want#include <cstring>
to getstrlen
- you can't use
strlen
on astd::string
, it only works on null terminated character arrays. You should just useinput.size()
instead. - you also shouldn't use
strlen
onvows
as although it is a character array so will compile it is not null terminated so the return value ofstrlen
is undefined. You can usesizeof(vows)
or just makevows
astd::string
orstd::vector<char>
instead.
count(input,input + n,vows[i])
is not correct.input + n
doesn't compile. What you presumably meant iscount(input.begin(),input.end(),vows[i])
Correcting the above issues and using modern c++ results in the simpilifed code:
#include <iostream>
#include <string>
#include <algorithm>
int main()
{
std::string input = "hello";
std::cout << " The input is " << input << "n";
std::size_t vowelsFound = 0;
std::string cons = "bcdfghjklmnpqrstvwxyz";
std::string vows = "aeiou";
for ( auto v : vows )
{
size_t c = std::count(input.begin(), input.end(), v);
if ( c != 0 )
{
vowelsFound += c;
std::cout << "found " << c << " " << v << "n";
}
else
{
std::cout << " The vowel " << v << " is not found in inputn";
}
}
std::cout << " The amount of vowels found is " << vowelsFound << "n";
std::cout << " The expected amount of vowels found is 2n";
}
If all you need is the total number of vowels you can just use:
std::cout << " The amount of vowels found is " << std::count_if( input.begin(), input.end(), [&](char c)
{
return vows.find(c) != std::string::npos;
}) << "n";
add a comment |
There are a number of issues in your code:
- I don't know what is in the
"/Users/richardlopez/Desktop/stdc++.h"
include but it is unlikely that it is a good thing to include. You probably want#include <cstring>
to getstrlen
- you can't use
strlen
on astd::string
, it only works on null terminated character arrays. You should just useinput.size()
instead. - you also shouldn't use
strlen
onvows
as although it is a character array so will compile it is not null terminated so the return value ofstrlen
is undefined. You can usesizeof(vows)
or just makevows
astd::string
orstd::vector<char>
instead.
count(input,input + n,vows[i])
is not correct.input + n
doesn't compile. What you presumably meant iscount(input.begin(),input.end(),vows[i])
Correcting the above issues and using modern c++ results in the simpilifed code:
#include <iostream>
#include <string>
#include <algorithm>
int main()
{
std::string input = "hello";
std::cout << " The input is " << input << "n";
std::size_t vowelsFound = 0;
std::string cons = "bcdfghjklmnpqrstvwxyz";
std::string vows = "aeiou";
for ( auto v : vows )
{
size_t c = std::count(input.begin(), input.end(), v);
if ( c != 0 )
{
vowelsFound += c;
std::cout << "found " << c << " " << v << "n";
}
else
{
std::cout << " The vowel " << v << " is not found in inputn";
}
}
std::cout << " The amount of vowels found is " << vowelsFound << "n";
std::cout << " The expected amount of vowels found is 2n";
}
If all you need is the total number of vowels you can just use:
std::cout << " The amount of vowels found is " << std::count_if( input.begin(), input.end(), [&](char c)
{
return vows.find(c) != std::string::npos;
}) << "n";
There are a number of issues in your code:
- I don't know what is in the
"/Users/richardlopez/Desktop/stdc++.h"
include but it is unlikely that it is a good thing to include. You probably want#include <cstring>
to getstrlen
- you can't use
strlen
on astd::string
, it only works on null terminated character arrays. You should just useinput.size()
instead. - you also shouldn't use
strlen
onvows
as although it is a character array so will compile it is not null terminated so the return value ofstrlen
is undefined. You can usesizeof(vows)
or just makevows
astd::string
orstd::vector<char>
instead.
count(input,input + n,vows[i])
is not correct.input + n
doesn't compile. What you presumably meant iscount(input.begin(),input.end(),vows[i])
Correcting the above issues and using modern c++ results in the simpilifed code:
#include <iostream>
#include <string>
#include <algorithm>
int main()
{
std::string input = "hello";
std::cout << " The input is " << input << "n";
std::size_t vowelsFound = 0;
std::string cons = "bcdfghjklmnpqrstvwxyz";
std::string vows = "aeiou";
for ( auto v : vows )
{
size_t c = std::count(input.begin(), input.end(), v);
if ( c != 0 )
{
vowelsFound += c;
std::cout << "found " << c << " " << v << "n";
}
else
{
std::cout << " The vowel " << v << " is not found in inputn";
}
}
std::cout << " The amount of vowels found is " << vowelsFound << "n";
std::cout << " The expected amount of vowels found is 2n";
}
If all you need is the total number of vowels you can just use:
std::cout << " The amount of vowels found is " << std::count_if( input.begin(), input.end(), [&](char c)
{
return vows.find(c) != std::string::npos;
}) << "n";
edited Jan 4 at 8:20
answered Jan 4 at 8:13
Alan BirtlesAlan Birtles
9,92611135
9,92611135
add a comment |
add a comment |
You could use the partition algorithm.
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
int main() {
std::string input = "hello";
std::cout << " The input is " << input << std::endl;
std::vector<char> vows = {'a','e','i','o','u','y'};
auto it_vows_end = std::partition(input.begin(), input.end(), [vows](char c){return find(vows.begin(), vows.end(), c) != vows.end();});
std::cout << " The amount of vowels found is " << std::distance(input.begin(), it_vows_end) << std::endl;
std::cout << " The expected amount of vowels found is 2 " << std::endl;
// If you also want to count consonants (for example if the input can also have digits or punctuation marks)
std::vector<char> cons = {'b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','z'};
auto it_cons_end = std::partition(it_vows_end, input.end(), [cons](char c){return find(cons.begin(), cons.end(), c) != cons.end();});
std::cout << " The amount of consonants found is " << std::distance(it_vows_end, it_cons_end) << std::endl;
}
Output:
The input is hello
The amount of vowels found is 2
The expected amount of vowels found is 2
The amount of consonants found is 3
std::size_t is the return type of sizeof. So it's only natural to use it for iterating over containers. Also, as it is a standard type used by some libraries, it makes code easier to read.
add a comment |
You could use the partition algorithm.
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
int main() {
std::string input = "hello";
std::cout << " The input is " << input << std::endl;
std::vector<char> vows = {'a','e','i','o','u','y'};
auto it_vows_end = std::partition(input.begin(), input.end(), [vows](char c){return find(vows.begin(), vows.end(), c) != vows.end();});
std::cout << " The amount of vowels found is " << std::distance(input.begin(), it_vows_end) << std::endl;
std::cout << " The expected amount of vowels found is 2 " << std::endl;
// If you also want to count consonants (for example if the input can also have digits or punctuation marks)
std::vector<char> cons = {'b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','z'};
auto it_cons_end = std::partition(it_vows_end, input.end(), [cons](char c){return find(cons.begin(), cons.end(), c) != cons.end();});
std::cout << " The amount of consonants found is " << std::distance(it_vows_end, it_cons_end) << std::endl;
}
Output:
The input is hello
The amount of vowels found is 2
The expected amount of vowels found is 2
The amount of consonants found is 3
std::size_t is the return type of sizeof. So it's only natural to use it for iterating over containers. Also, as it is a standard type used by some libraries, it makes code easier to read.
add a comment |
You could use the partition algorithm.
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
int main() {
std::string input = "hello";
std::cout << " The input is " << input << std::endl;
std::vector<char> vows = {'a','e','i','o','u','y'};
auto it_vows_end = std::partition(input.begin(), input.end(), [vows](char c){return find(vows.begin(), vows.end(), c) != vows.end();});
std::cout << " The amount of vowels found is " << std::distance(input.begin(), it_vows_end) << std::endl;
std::cout << " The expected amount of vowels found is 2 " << std::endl;
// If you also want to count consonants (for example if the input can also have digits or punctuation marks)
std::vector<char> cons = {'b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','z'};
auto it_cons_end = std::partition(it_vows_end, input.end(), [cons](char c){return find(cons.begin(), cons.end(), c) != cons.end();});
std::cout << " The amount of consonants found is " << std::distance(it_vows_end, it_cons_end) << std::endl;
}
Output:
The input is hello
The amount of vowels found is 2
The expected amount of vowels found is 2
The amount of consonants found is 3
std::size_t is the return type of sizeof. So it's only natural to use it for iterating over containers. Also, as it is a standard type used by some libraries, it makes code easier to read.
You could use the partition algorithm.
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
int main() {
std::string input = "hello";
std::cout << " The input is " << input << std::endl;
std::vector<char> vows = {'a','e','i','o','u','y'};
auto it_vows_end = std::partition(input.begin(), input.end(), [vows](char c){return find(vows.begin(), vows.end(), c) != vows.end();});
std::cout << " The amount of vowels found is " << std::distance(input.begin(), it_vows_end) << std::endl;
std::cout << " The expected amount of vowels found is 2 " << std::endl;
// If you also want to count consonants (for example if the input can also have digits or punctuation marks)
std::vector<char> cons = {'b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','z'};
auto it_cons_end = std::partition(it_vows_end, input.end(), [cons](char c){return find(cons.begin(), cons.end(), c) != cons.end();});
std::cout << " The amount of consonants found is " << std::distance(it_vows_end, it_cons_end) << std::endl;
}
Output:
The input is hello
The amount of vowels found is 2
The expected amount of vowels found is 2
The amount of consonants found is 3
std::size_t is the return type of sizeof. So it's only natural to use it for iterating over containers. Also, as it is a standard type used by some libraries, it makes code easier to read.
edited Jan 4 at 8:30
answered Jan 4 at 8:24
mcabrebmcabreb
1544
1544
add a comment |
add a comment |
Several issues, on top of "/Users/richardlopez/Desktop/stdc++.h" . If you need a specific function, look the reference and include the appropriate official C++ header for the functionality.
First strlen
is for 0-terminated char*
. It doesn't work on input
. For that, just use:
auto n = input.size();
Then the same for vows
:
std::vector<char> vows{'a','e','i','o','u','y'};
auto v = vows.size();
Then count
is also different:
std::count(std::begin(input) ,std::end(input), vows[i])
If you have C++17, do:
if(auto count = std::count(std::begin(input), std::end(input), vows[i]); count > 0)
{
vowelsFound += count;
std::cout << count << std::endl;
}
2
SimplyvowelsFound += std::count(std::begin(input), std::end(input), vows[i]);
– john
Jan 4 at 8:08
OP had some debugging info to add in the condition that I didn't reproduce (I should have). That would be the proper refactoring indeed.
– Matthieu Brucher
Jan 4 at 9:27
add a comment |
Several issues, on top of "/Users/richardlopez/Desktop/stdc++.h" . If you need a specific function, look the reference and include the appropriate official C++ header for the functionality.
First strlen
is for 0-terminated char*
. It doesn't work on input
. For that, just use:
auto n = input.size();
Then the same for vows
:
std::vector<char> vows{'a','e','i','o','u','y'};
auto v = vows.size();
Then count
is also different:
std::count(std::begin(input) ,std::end(input), vows[i])
If you have C++17, do:
if(auto count = std::count(std::begin(input), std::end(input), vows[i]); count > 0)
{
vowelsFound += count;
std::cout << count << std::endl;
}
2
SimplyvowelsFound += std::count(std::begin(input), std::end(input), vows[i]);
– john
Jan 4 at 8:08
OP had some debugging info to add in the condition that I didn't reproduce (I should have). That would be the proper refactoring indeed.
– Matthieu Brucher
Jan 4 at 9:27
add a comment |
Several issues, on top of "/Users/richardlopez/Desktop/stdc++.h" . If you need a specific function, look the reference and include the appropriate official C++ header for the functionality.
First strlen
is for 0-terminated char*
. It doesn't work on input
. For that, just use:
auto n = input.size();
Then the same for vows
:
std::vector<char> vows{'a','e','i','o','u','y'};
auto v = vows.size();
Then count
is also different:
std::count(std::begin(input) ,std::end(input), vows[i])
If you have C++17, do:
if(auto count = std::count(std::begin(input), std::end(input), vows[i]); count > 0)
{
vowelsFound += count;
std::cout << count << std::endl;
}
Several issues, on top of "/Users/richardlopez/Desktop/stdc++.h" . If you need a specific function, look the reference and include the appropriate official C++ header for the functionality.
First strlen
is for 0-terminated char*
. It doesn't work on input
. For that, just use:
auto n = input.size();
Then the same for vows
:
std::vector<char> vows{'a','e','i','o','u','y'};
auto v = vows.size();
Then count
is also different:
std::count(std::begin(input) ,std::end(input), vows[i])
If you have C++17, do:
if(auto count = std::count(std::begin(input), std::end(input), vows[i]); count > 0)
{
vowelsFound += count;
std::cout << count << std::endl;
}
edited Jan 4 at 9:28
answered Jan 4 at 8:00
Matthieu BrucherMatthieu Brucher
17.7k52445
17.7k52445
2
SimplyvowelsFound += std::count(std::begin(input), std::end(input), vows[i]);
– john
Jan 4 at 8:08
OP had some debugging info to add in the condition that I didn't reproduce (I should have). That would be the proper refactoring indeed.
– Matthieu Brucher
Jan 4 at 9:27
add a comment |
2
SimplyvowelsFound += std::count(std::begin(input), std::end(input), vows[i]);
– john
Jan 4 at 8:08
OP had some debugging info to add in the condition that I didn't reproduce (I should have). That would be the proper refactoring indeed.
– Matthieu Brucher
Jan 4 at 9:27
2
2
Simply
vowelsFound += std::count(std::begin(input), std::end(input), vows[i]);
– john
Jan 4 at 8:08
Simply
vowelsFound += std::count(std::begin(input), std::end(input), vows[i]);
– john
Jan 4 at 8:08
OP had some debugging info to add in the condition that I didn't reproduce (I should have). That would be the proper refactoring indeed.
– Matthieu Brucher
Jan 4 at 9:27
OP had some debugging info to add in the condition that I didn't reproduce (I should have). That would be the proper refactoring indeed.
– Matthieu Brucher
Jan 4 at 9:27
add a comment |
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.
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%2f54034945%2fmany-errors-from-mixing-cstdlib-function-calls-with-string-functions%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
6
That's very bad: "/Users/richardlopez/Desktop/stdc++.h"
– Matthieu Brucher
Jan 4 at 7:49
strlen(input)
This only works with 0-terminated strings, which you DON'T have. Use avector<char>
and get its size instead. There are probably other issues as well, so not an answer at this stage.– Matthieu Brucher
Jan 4 at 7:50
I'm also not sure when I should be using std::size_t. I initially just defined n and v as type int, and thought that would suffice. I understand that std::size_t is for unsigned integers, but my integers will never be negative, so I'm not sure why it matters that I use std::size_t at all. I'm really not sure what its purpose is to begin with. I've never seen that format for defining a type.
– pancham2016
Jan 4 at 7:51
1
Really the simplest thing is to forget about char arrays and use std::string throughout. They are the safest and most flexible and you'll only have one thing to learn.
– john
Jan 4 at 8:05