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
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
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.
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 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
Following c++ code in qt is used by me to add values to an integer(initial value 10) and store it in a QStringList. But when I print the values of the StringList on to 3 labels it prints 10 on all lables though it should be 20,30 and 40 since I increment a by 10 through the for loop!
void MainWindow::on_pushButton_clicked()
{
QStringList array;
int a =10;
for(int i=0;i<10;i++){
a=+10;
array<<QString::number(a);
}
ui->label->setText(array[0]);
ui->label_2->setText(array[1]);
ui->label_2->setText(array[2]);
}
How can I correct this?
You are using
a =+ 10;
// equivalent to
a = +10;
That means you are assigning 10 to a.
You need to use the form below to increment it by 10:
a += 10;
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
Having issues with my code... the program compiles, but then it gives the following statement "Run-Time Check Failure #3 - The variable 'result' is being used without being initialized." It then ends the program at that point. Everything I have after trying to run the function is ignored. What should I do?
double result;
for (int i=0; i<nRows; i++)
{
absum.push_back(vector<double>());
for (int j=0; j<nColumns; j++)
{
double temp;
temp = matrixa[i][j]+matrixb[i][j];
absum[i].push_back(temp);
cout << temp << '\t';
}
cout << endl;
}
return result;
At the top of your code you have:
double result;
At the moment it's not initialised to anything at all, so the compiler won't use it. So you need to need to initialise it thus:
double result = 0;
It's also generally good practice to initialise every variable you use in C++, that way you don't get nasty compiler messages, and don't run the risk of returning some random chunk of memory. You always want to start your program from a known state, so if you know that result is 0, then all is good.
C++ is picky about this sometimes, have you tried double result = 0?
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 run this code and got this exception in visual studio:
int main ()
{
int * x=new int(23);
for(int i=0;i<9;i++)
{
x[i]=i;
}
delete []x;
return(0);
}
thanks for your help ;)
int * x = new int(23); does not do what you think. It allocates the memory for a int and gives it the value of 23.
What you want to do is this:
int * x = new int[23];
Although, I would recommend you use std::vector if the size change or std::array if the size is fix.
If you use new you need to use delete. If you use new[] then you need to use delete []. new() is not the same as new[]. new(someval) sets the new object to someval. It does not make someval number of objects.