Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
I'm trying to write a c++ function to find if the array has capital, but somehow it kept returning true.
Here is my function:
bool hasNoCapitals (const string array[], int n)
{
bool result = true;
for (int i = 0; i<0; i++)
{
for (size_t k = 0; k < array[i].size(); k++)
{
char word = array[i][k];
if (word == tolower(word))
{
result = true;
}
else{
result = false;
k = array[i].size();
return result;
}
}
}
return result;
}
int main()
{
string arr[] = {"sa","DDDD","DDDDDDDDD","ffdd","sa"};
cout << hasNoCapitals(arr, 5);
return 0;
}
The function always returns true because of a typo: you have for (int i = 0; i<0; i++), which should be for (int i = 0; i<n; i++) instead.
So your loop is always empty.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
So I am a newbie in C++, I do code in JS, for some reason I can't figure out the mistake here, please help me out. thanks!
#include <iostream>
using namespace std;
int search(char input, char sentence[]){
for(int i = 2; i != '\0'; i++){
if(sentence[i] == input){
return i;
}else{ return -1; }
}
}
int main()
{
char key[20] = "hey my name is sid!";
char ser = 'm';
cout << search(ser,key);
return 0;
}
Your condition in the for loop is wrong, you are not checking the string only the index. Also if your character did not match, you do not want to exit immediately.
The correct code would be:
for(int i = 0; sentence[i] != '\0'; i++)
{
if(sentence[i] == input)
{
return i;
}
}
return -1;
If you want to start the search at the third character you should first ensure that your string has at least three elements:
if(strlen(sentence)>=3)
{
for(int i = 2; sentence[i] != '\0'; i++)
{
...
}
...
}
For your search function, you are only really checking the first character in the loop, then returning. You should move "return -1;" outside of the for loop as then it will only be called after the entire string was checked for the value and it was not found.
int search(char input, char sentence[]) {
for (int i = 2; sentence[i] != '\0'; i++) {
if (sentence[i] == input) {
return i;
}
}
return -1;
}
in addition to changing the conditions of the for loop as other users mentioned.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
My code counts the frequency of letters in a file. My code:
void incCount (int i, vector<CCount> &chars)
{
int n;
n = chars[i].i;
n++;
chars[i].i = n;
}
void procWord(string word, vector<CCount> &chars)
{
for (int i=0; i<word.length(); i++)
{
bool found = false;
char a = word[i];
for (int j=0; j<chars.size(); j++)
{
if (a == chars[j].c)
{
bool found = true;
incCount(j, chars);
}
}
if (found == false)
{
CCount c; //CCount is a class with a char and int data type.
c.c = a;
c.i = 1;
chars.push_back(c);
}
}
}
int main ()
{
vector<CCount> chars;
string word;
//opening file code here
while (fin >> word) {
procWord(word, chars);
}
return 0;
}
class CCount
{
public:
char c;
int i;
};
the code does accumulate the letter count however when i print the the vector elements I get this. I use "this is a test data" as the test input in the file
You are defining found again. See commented line.
You are defining variable with same name and most local variable will be preferred.
bool found = false;
char a = word[i];
for (int j=0; j<chars.size(); j++)
{
if (a == chars[j].c)
{
bool found = true; //this is wrong, just make it found = true;
incCount(j, chars);
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm writing a program that would make a power of a matrix.
as you see, I'm trying to ask at the for (int n...) loop if n==0,
but when I'm debugging - I see that the program just skips the condition and doesn't even enter it. I mean it doesn't even "ask" the question if n==0...
What is the problem?
void Matrix::pow(int power, Matrix & result)
{
for (int i = 0; i < power-1; i++)
{
for (int j = 0; j < rows; j++)
{
for (int k = 0; k < cols; k++)
{
for (int n = 0; n < cols; n++)
{
if (n==0)
{
(&result)->_array[i][j] == 0; //Reset result's array.
}
(&result)->_array[i][j] += this->_array[i][n] * this->_array[n][j];
}
}
}
}
}
This is a boolean expression, not an assignment.
(&result)->_array[i][j] == 0; //Reset result's array.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I fixed the typos that I had in my code, and now it runs perfect
int rollDice(int diceRoll[], int numberRolling) // Random dice rolls
{
int values = 0;
for (int i = 0; i < numberRolling; i++)
{
diceRoll[i] = 1 + rand() % 6;
}
for (int i = 0; i < numberRolling; i++)
{
values = values + diceRoll[i];
}
return values;
}
You have a typo in your first for
for (int i = 0; i < numberRolling, i++)
Should be
for (int i = 0; i < numberRolling; i++)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I couldn't figure out why but this is my code :
char expression[256] = {};
cin >> expression;
cout << endl;
int** variableTable = NULL;
int numOfVals = getNumberOfVariables(expression);
variableTable = static_cast<int**>(calloc(numOfVals, sizeof(int*)));
for (int i = 0; i < numOfVals; i++)
{
variableTable[i] = static_cast<int*>(calloc(2, sizeof(int)));
}
fillPromoterTable(expression, variableTable, numOfVals);
this is the fillPromoterTable
void fillPromoterTable(const char* expression, int** variableTable, int numOfVals)
{
char promoter[15] = {};
char *token;
char* expCpy = pcstrdup(expression);
for (int i = 0; numOfVals; i++)
{
token = strtok(expCpy, "+-*/");
int nLen = istrlen(token);
for (int j = 0; j < nLen; j++)
{
if (isdigit(token[j]))
promoter[j] = token[j];
if (isalpha(token[j]))
break;
}
variableTable[i][0] = atoi(promoter);
memset(promoter, '\0', 15);
token = strtok(NULL, "+-*/");
}
free(token);
free(expCpy);
}
in this line :
variableTable[i][0] = atoi(promoter);
I get an error saying I'm trying to write to address 0xFDFDFDFD
I couldn't figure out why it happens I could use some help.
Change the for loop condition in the function fillPromoterTable
for (int i = 0; numOfVals; i++)
To:
for (int i = 0; i < numOfVals; i++)