Why can I not use string.length here? [closed] - c++

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 5 years ago.
Improve this question
OK, I am in a class now that I am taking in C++. It is basic and I am still new. I have a quick question about the string.length() function. Can you compare this for an integer value inside of a if statement? So, if I did
if(string.length() = 20)
{
cout << "IT VWERKS" << endl;
}
would I get an answer? I tried doing this for a program I was working on and it would not work. Could someone explain this to me?

You are using assigment operator = inside if instead of conditional == . so change your code as following .It will work.
if(string.length() == 20)
{
cout << "IT VWERKS" << endl;
}

The correct way to do this it to use the comparison operator (==). In this case, replace the first line with:
if (string.length() == 20)
By doing string.length() = 20 you are trying to assign the value 20 to the result of the function length(), and that is not possible. By replacing the operator = with == you are comparing both values. Once they match, the code inside the if statement is executed.

Related

cout macro value doesn't work as expected [closed]

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 months ago.
Improve this question
#include<iostream>
using namespace std;
#define C 1<<(8*1)
int main(){
if(C==256){
int a=C;
cout<<a;
}
}
My expectation is 256 but it print 18. What's wrong with it? Thanks!
I assume your question is about std::cout << C;, not about std::cout << a;.
Macros are simple copy-and-paste text replacement. When preprocessor encounters macro name, it replaces it with the definition as text without any analysis. So, what happens is that
std::cout << C;
is replaced with
std::cout << 1<<(8*1);
which should indeed print 18.
That's one very good reason to not use macros. Instead, use a constant (or constexpr) variable:
constexpr int C = 1 << (8 * 1);
This is type safe and will never surprise you when used in any context.
If you really have to use macro for some reason, make sure to wrap it in extra parantheses:
#define C (1 << (8 * 1)) // but seriously, don't use macros

How to use comparison operator correctly in this case? (string array[0][0] == "string") [closed]

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 1 year ago.
Improve this question
I need to compare a character in a string that is in an array with another string. This is a functioning but simple version of my problem:
#include <iostream>
using namespace std;
int main() {
string a_ray[1] = {"asd"};
if (a_ray[0][0] == "a") {
bool a;
}
return 0;
}
Error message: ISO C++ forbids comparison between pointer and integer [-fpermissive]|
What causes this? And how can I do what I want to do in the correct way?
Thank you in advance!
Since you are comparing against a character, your code should be
if (a_ray[0][0] == 'a')
You are trying to compare a character with a character array, hence the error message.

Why does this output "geeksforgeeks"? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
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.
Improve this question
#include <iostream>
using namespace std;
int main()
{
if (!(cout << "geeks"))
cout <<" geeks ";
else
cout << "forgeeks ";
return 0;
}
Why is cout << "geeks"; inside the if condition executed? I know that the if statement is false. I expected "forgeeks " only.
Why is cout << "geeks"; inside the if condition executed?
Because otherwise the computer won't know whether it was "true" or "false"?
Given if (foo()), the function foo must be called; this extends to any expression in general, which must be evaluated before their "result" can be known (though note that sub-expressions may be skipped due to short-circuiting).
Focus in below statement:
if (!(cout << "geeks"))
Here cout with << operator which is overloaded to print the stream as output i.e "geeks" and then it return this stream to the if statement.
if statement check the condition i.e if(!("geeks")), which if statement see as if(!(true)), results to false condition.
Hence else statement execute to print "forgeeks".

Elements in an array will not change c++ [closed]

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
void Remove(int x)//x is the number that i want to remove
{
for(int i=0;i<CAPACITY;i++)//loop is to find the first case of x
{
if(x==data[i])//if x is in data
{
cout<<data[i]<<endl;//for debugging
data[i]==0; //change x to 0
cout<<data[i]<<endl;
}
}
}
when i cout to see if it works the number that i wanted to delete is still there.
Here is the output before i run it when x=15:
12,15,20,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
I used cout to see if there was a problem with the condition however it runs if x is in the array.
Here is the output after, even if x is in the array:
12,15,20,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
In your If loop you use comparison operator '==' so put only one = which means you assign variable x to data array.
The problem in in the line data[i]==0; //change x to 0
== is an comparison operator. In order to assign a value use = instead. So:
data[i] = 0

error: 'listOfColors' was not declared in this scope [closed]

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 5 years ago.
Improve this question
I'm new to C++ and I don't understand why I'm getting a not declared error on this:
int main(){
string listOfColors[5] = {"red","blue","green","yellow","magenta"};
for(int i = 0;i < sizeof listofColors;i++){
cout << listofColors[i] << "\n";
}
return 0;
}
This is my first utilization of an array so far, so I may just not be declaring it correctly. I also had the array declaration before the main function beforehand.
You declared your variable as listOfColors (capital "O"), and then you use it as listofColors in your for loop. All you need to do is to capitalize the "O" when using your variable.