Hey guys I haven't been able to find the proper guidance on this specific Vowel counting program so any help would be appreciated.
This is my code so far, I'm not entirely sure where the error lies (probably in the for loop since I'm pretty green with them). The vowelCount variable is not counting properly either.
Thanks in advance.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main() {
char letter(10);
int vowelCount(0);
cout << "Enter a maximum of 10 characters: ";
cin >> letter;
for (int i = 0; i <= letter; i++) {
if (letter == 'a' || letter == 'A' ||
letter == 'e' || letter == 'E' ||
letter == 'i' || letter == 'I' ||
letter == 'o' || letter == 'O' ||
letter == 'u' || letter == 'U') {
vowelCount++;
}
}
cout << "\nTotal number of vowels: " << vowelCount;
cout << endl;
return 0;
}
There are a few mistakes with your code:
char letter(10) is not an array of char, but a single char.
Your for loop goes to the letter value (which is 10, granted) but will not be once you fix letter to be an array.
Also, when you read the variable letter, you should use the operator[] to access its elements.
The following is the correct code:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
char letter;
int vowelCount(0);
cout << "Enter a maximum of 10 characters: ";
for (int i = 0; i < 10; i++)
{
cin >> letter;
if (letter == 'a' || letter == 'A' ||
letter == 'e' || letter == 'E' ||
letter == 'i' || letter == 'I' ||
letter == 'o' || letter == 'O' ||
letter == 'u' || letter == 'U')
{
vowelCount++;
}
}
cout << "\nTotal number of vowels: " << vowelCount;
cout << endl;
return 0;
}
The problem is that you declare a single character (and initialize to 10), that you read into. Then you loop from zero to the ASCII value of that letter. You should either read into a string or an array of characters.
If you go for the latter (array of characters) remember that in C++ such strings need one extra character (to terminate the string). So if you want a string of 10 characters, you have to declare the array to be 11 characters big.
Related
I want my string AGRUMENTS to tokenize on "|" first and then tokenize the result with spaces.
for example:
if I write "mkdir abc|ls|grep", it should return
"mkdir
abc
ls
grep"
int main(){
//input command
char arguments[200];
cout << "Enter your command: ";
cin.getline(arguments, 200,'\n');
cout << "\nEntered command is: " << arguments << endl;
int i = 0;
char *arri1[100];
char *pipi[100];
int l=0;
pipi[l] = strtok(arguments, "|");
while (pipi[l] != NULL)
{
pipi[++l] = strtok(NULL, "|");
}
cout<<pipi[0]<<endl<<pipi[1]<<endl<<pipi[2]<<endl<<pipi[3]<<endl<<pipi[4]<<endl;
int m=0;
arri1[m] = strtok(*pipi, " "); //NOT WORKING
while (arri1[m] != NULL)
{
arri1[++m] = strtok(NULL, " "); //tokenize on spaces
}
cout<<endl<<arri1[0]<<" "<<arri1[1]<<" "<<arri1[2]<<" ";
}'''
#include<iostream>
#include<string>
using namespace std;
int main()
{
string name[] = {"Geeks ", "for", "Geeks"} ;
string v,s ;
v += name[0][0] + "." ;
cout << v << "\n" ;
s += name[0][0] ;
s += "." ;
cout << s << "\n" ;
}
On outputting:
string v outputs a garbage string while string s outputs G.
Output :
# // output of string v
G. // output of string s
Why does v give out a garbage value even though I'm appending '.' to the new string ?
Kindly explain both the cases.
Clang will complain about:
v += name[0][0] + "." ;
with a message like:
warning: adding 'std::...' (aka 'char') to a string does not append to the string [-Wstring-plus-int]
In other words, string + "chars" does not append the chars to the string, and so you need to create the string manually:
v += name[0][0] + string(".") ;
I've try this case but I got stuck in a trouble. I just want to to determine length of a string from user input or from console. But it doesn't work.
#include <iostream>
#include <string>
using namespace std;
int main(){
int N;
string S;
char vocal;
vocal = {'a'||'i'||'u'||'e'||'o'};
cout << "Length of Spaghetti Name: ";
cin >> N;
fflush(stdin);
cout << "Spaghetti Name: ";
cin >> S;
if (S[0] != vocal){
S.erase(0,1);
} else if (S[N] != vocal){
S.erase(N,1);
}
cout << S << endl;
cin.get();
return 0;
}
expected input
9
Carbonara
expected output
arbonara
This program will delete non vocal character in spaghetti names.
i'm extremely rusty with C and when trying to compile this i keep getting the error: "lvalue required as left operand of assignment" in each of the three nested ifs.
i'm pretty sure i'm doing some really dumb and basic mistake but i can't manage to fix it!
also the code is extremely naive, is there a better way to check the various strings in argv (they must be 6 charachters long, the first 2 charachters must be either IC, FR or RG and then the other 4 should be a numeric code) and print the correct strings?
int main(int argc, char *argv[]){
int i=1;
while(i < argc){
if(strlen(argv[i]) == 6){
if(argv[i][0] == 'I' && argv[i][1] = 'C' && argv[i][2] >= '0' && argv[i][2] <= '9' && argv[i][3] >= '0' && argv[i][3] <= '9' && argv[i][4] >= '0' && argv[i][4] <= '9' && argv[i][5] >= '0' && argv[i][5] <= '9'){
printf("%s", argv[i]);
}
if(argv[i][0] == 'F' && argv[i][1] = 'R' && argv[i][2] >= 0 && argv[i][2] <= 9 && argv[i][3] >= 0 && argv[i][3] <= 9 && argv[i][4] >= 0 && argv[i][4] <= 9 && argv[i][5] >= 0 && argv[i][5] <= 9){
printf("%s", argv[i]);
}
if(argv[i][0] == 'R' && argv[i][1] = 'G' && argv[i][2] >= 0 && argv[i][2] <= 9 && argv[i][3] >= 0 && argv[i][3] <= 9 && argv[i][4] >= 0 && argv[i][4] <= 9 && argv[i][5] >= 0 && argv[i][5] <= 9){
printf("%s", argv[i]);
}
}
i++;
}
return(0);
}
EDIT: Damn this was a pretty dumb question, wrote = instead of == in the second assignment. thanks for the help!
It's because you have a typo in your second tests in each if, having written = (assignment of a value) instead of == (test for equality).
The error message is correct but not intuitive. The compiler interprets your​ code as follows (note the extra parentheses I inserted):
if ((arg[0] == 'C' && arg[1]) = 'I') {}
This is because && binds more tightly than =. Replace the = with ==, and you're fine.
You should write a helper function:
static bool parse_number(const char *arg, char ch1, char ch2, int *pnum) {
if (arg[0] == ch1 && arg[1] == ch2 && '0' <= arg[2] && arg[2] <= '9') {
char *end;
unsigned long num = strtoul(arg + 2, &end, 10);
if (*end == '\0' && end - arg == 6) {
*pnum = (int)num;
return true;
}
}
return false;
}
Then you can use it like this:
int num;
if (parse_number(argv[i], 'I', 'C', &num)) {
printf("IC number %d\n", num);
}
I have a user input of this kind:
A+B*C;
A*(B+c);
C+F.
Now I want to parse this input and get all sub-strings until the semi-colons and stop scanning when I run into a period symbol.
What is the simplest way to achieve this ?
Also, I have freedom to take input from a file or from the console. Which one would be the easiest to implement ?
I have a method to read from the console as such :
cout << "Enter input: " << endl;
char delimiter = '.';
string inputStr;
getline(cin,inputStr,delimiter);
cout << inputStr;
But if I enter the above mentioned sample input I read only until before period symbol is recieved. So while finding sub strings, what stopping criteria or flag should I take ?
TIA
EDIT:1
Code so far:
#include <string>
#include <iostream>
using namespace std;
int main()
{
cout << "Enter input: " << endl;
char delimiter = '.';
string inputStr;
getline(cin,inputStr,delimiter);
cout << inputStr;
string deli = ';';
size_t pos = 0;
string token;
while ((pos = inputStr.find(deli)) != std::string::npos) {
token = inputStr.substr(0, pos);
std::cout << token << std::endl;
inputStr.erase(0, pos + deli.length());
}
std::cout << inputStr << std::endl;
};
ANSWER:
I was wrongly initializing the string deli. I had to do this :
string deli = ";". Instead of single-quotes, I had to use double-quotes, because it a string and not a character! Final working solution here: https://repl.it/EPyC/2
Use getline with delim ; for all lines. Check for dot(.) for last line.
string line;
while(getline(cin, line, ';')){
if(line.back() == '.')
line.pop_back();
cout << line <<endl;
}