Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
hi I have this simple code to print out book word when i is equal 2
int i;
for (i=0;i!=5;i++)
{
if(i=2)
{
cout << "book";
};
}
i dont know why this code always looping even though I limit i to 5.
if I print i before if condition its printing this
0book3book3book3book3book3book3book3book3book3book3boo....
im using online compiler to compile this.
anyone know how to solve this?
if(i=2)
should be
if(i==2)
Classic mistake
if(i=2) --> assignment operator
if(i==2) --> comparing. Very different.
if(i=2)
is equivalent to
i = 2;
if(i != 0)
you need
if(i == 2)
Yeah as Roger said what you have done is do an assignment within the if statement instead of a comparison.
if(i=2) //assignment
if(i==2)//comparison
personally when comparing a variable to a raw int etc I like to do the following:
if(2==i) //comparison no error
if(2=i) //attempt to assign to raw int :ERROR
Instruction
if(i=2)
mean: assign value 2 to i and check if i!=0.
You had to do:
if(i==2)
Check if i is equal to value 2.
Simply syntax.
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 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.
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
I am trying initialize element of array
array[m][n] == char("X");
after printing that element i'm getting the value of it equals ะด (Russian d); how deal with it, and I'm not even able to initialize that element without parsing const char to char.
You have to write simply as
array[m][n] = 'X';
where 'X' is a character literal.
Or if you like very much string literals then:)
array[m][n] = *"X";
or
array[m][n] = "X"[0];
EDIT: I am sorry. You have also to use the assignmnet operator (=) instead of the comparison operator (==)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I'm writing a fairly large application containing many different modules. I've always been programming with the GCC debugging info turned on and all optimisations turned off, for obvious reasons of debugging. Now I've decided that it's time for a release and I've set GCC to optimise to the best of it's abilities (-O3). And this is when the strangest of bugs appeared. Take the following code:
void SomeClass::setValue(int i) { this->iValue = i; }
int SomeClass::getValue() const { return this->iValue; }
Now without optimisations, these work perfectly. With optimisations, the value of SomeClass.iValue is not modified in the setValue() method. In fact, the output of the following:
cout << x.getValue();
x.setValue(5);
cout << x.getValue();
returns
0
0
when the iValue is intialised in the class to 0.
Now the strange part: if I insert the following code into setValue():
void SomeClass::setValue(int i) { cout << "Narf"; this->iValue = i; }
the code works!
Can someone please explain to me what is going on?
did you try checking cout<<x.iValue; ? perhaps the problem is in SomeClass::getValue(); for example, it returning void or being const ? :)
also, just an idea, the optimisation might happen in cout not your actual code as hinted by cout << "Narf";
Firstly, your code is not exactly correct. Getter function should return int instead of void. I think you just typed it incorrectly here, because gcc will not let you compile that (std::cout has no overloaded operator << for type void). What is more, SomeClass.i_value doesn't compile either. Did you mean this->i_value or just i_value?
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
If I declare a function:
vector<int> & call(string *target)
How should I use target in the function for a comparison with another string? For example:
string str;
if(str == //string in target){
...
}
As &target, or simply target? Also, how should the return look? I'm assuming it should be:
return &some_vector;
since that is the type in the function declaration. Finally, what about the opposite? That is:
vector<int> & call(string &target)
When in the function and wanting to use the string, is it as simple as:
*target
On your first question it's
if(str == *target){
On your second question my advice would simply be, don't. It looks like you are trying to return a reference to a local variable. That is a well known newbie mistake that will simply crash your program.
On your third question it's
if(str == target){
You need to dereference the pointer to get the object. That is, use *:
if (str == *target) {
Also, how should the return look?
You're returning a reference, not a pointer. That means you simply need to return the name of the object:
return some_vector;
Finally, what about the opposite?
If target is a reference (NOT a pointer) than you simply use the name of the object. There's no dereference involved with this.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm beginning in C++ and I have a simple task. As title said, I want to delete first and last character from a string for x times (where x is the lenght of the string). For example, if the string is "example", the result will be:
example
xampl
amp
m
amp
xampl
example
So far, I'm thinking like this:
#include <iostream>
#include <string>
string sir = "Example";
int len, i;
len = sir.length();
for(i=1; i<=len; i++)
{
sir.erase(sir.begin(), sir.end());
cout<<sir;
}
Or something like that... Can someone help me ?
You want to delete both the first and last char.But in the example you also added them each step. It is not actually clear what you want. Whatever you want to delete or add the characters it is feasible to keep the string unchanged. So you should use substr. Check it out here.
The problem is that
you can not use std::string::erase with integral index such as int i,
you need to use std::string::iterator
but even if you use std::string::iterator, with the current logic you would be trying to increment an iterator after the erase has been called. (such iterator is invalid)
Possible solution: assign sir.begin() to your iterator after each erase.
Here's how it could look like:
std::string sir = "Example";
for(string::iterator i = sir.begin(); i != sir.end(); i = sir.begin())
{
sir.erase(i);
std::cout << sir << std::endl;
}
outputs:
xample
ample
mple
ple
le
e
Just note that after erasing characters from your std::string, these characters are lost. You can not "restore" them. For the other half, you'll have to come with more sophisticated approach, but I'll leave that to you :)