Incorrect sum in for loop Qt c++ [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
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;

Related

In C++, why can int initialize a variable using new operator but double cannot? [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 3 years ago.
Improve this question
#include <iostream>
using namespace std;
int main()
{
int* i = new int(75);
double* d = new double(3.14159);
printf("%d\n",*i);
printf("%d\n",*d);
}
In the above code i returns a value of 75 however, d returns 1.
I tried explicitly initializing it as
*d = 3.14159
But the value is still returned as 1.
Can anyone explain what I am doing wrong here?
Use this for printing.
cout<<*i;
cout<<*d
"%f" is the (or at least one) correct format for a double if you want to use printf for printing the value of the double in C++.

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.

Heap corruption detected when use delete [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 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.

What is wrong with this code in string reversal? [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 9 years ago.
Improve this question
In the following piece of code I get the error mentioned below. Please tell me
Why *p=t gives error here
void reverse (char *p)
{
int length=strlen (p);
int c=0, i=length/2;
char *Temp=p+length-1, t;
while (c<length)
{
t=*Temp;
*Temp=*p
*p=t;
//Gives error as illegal, right operand has type char*
//Why is the error in the above line?
c++;
Temp--;
}
}
There is a semi-colon missing:
t=*Temp;
*Temp=*p ; //--here
*p=t;