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
int nums [] {1,2,3,4,5,6,7,8,9,10};
int main(){
int size {10};
for (int i;i<size;i++)
cout << nums[i] << " ";
for (int i;i<size;i++)
cout << nums[i] << " ";
return 0;
}
This prints my array once but shouldn't I get my array printed twice not once?
You need to initialize the i variable to the start index, like in for (i = 0; i < size; i++). Otherwise i can start in any value and this is undefined behavior.
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 1 year ago.
Improve this question
char x;
cout << "\n/something?(y/n): ";
cin >> x;
if(x=='y'){
int n2 = 0;
cout << "number: ";
cin >> n2;
int i = n2;
for(int i; 0<i; i--){
cout << i;
}
}
else{
system("pause");
}
How come when I run the code it doesn't count down from the number the user gave?
What is happening here is that you declare local variable i in the block which contains for loop, and then declare another local variable i in the loop itself, and the one in the loop is not initialized. It gets some "trash" value, from each it counts down. I assume you wanted to use i declated above the loop in the loop, but it is done differently. To fix this I suggest 2 most reasonable variants:
variant 1 - use i declared above the loop
int i = n2;
for(; i>0; i--) {
...
variant 2 - use loop-local i:
// just this, do not declare i above the loop
for(int i = n2; i>0; 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 2 years ago.
Improve this question
output should be like this I hope u guys can help me..
Here's the code that I tried:
int main() {
int x,y;
for (x=1; x<=10; x++) {
cout << x << "|";
for (y=1; y<=10; y++) {
cout << x*y << "\t";
}
cout << endl;
return 0;
}
}
Your program is terminating just after the first iteration of the loop, the reason behind this is that you misplaced return 0 inside the loop instead of putting it at the end, so your program is returning after first iteration of the loop.
here is the fix
int main()
{
int x,y;
for (x=1; x<=10; x++)
{
cout<<x <<"|";
for (y=1; y<=10; y++)
{
cout<<x*y<<"\t";
}
cout<<endl;
}
return 0;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 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
I've been reviewing my C++ lately. But I am running into a puzzle about printing a char array. The code is below:
int n = 5;
char *array1 = new char[n];
for (unsigned int i = 0; i < n - 1; i++)
array1[i] = (char)i;
cout << array1 << endl;
cout << array1[3] << endl;
cout << *array1 << endl;
None of the three cout lines works. Could anyone tell me why?
array1[0] == 0. cout << array1 interprets array1 as a pointer to a NUL-terminated string, and since the very first character is in fact NUL, the string is empty.
cout << array1[3] does print a character with ASCII code 3. It's a non-printable character, not visible to a naked eye. Not sure what output you expected to see there.
As a separate answer, it seems you're trying to get a string which has the following : array = "1234....(n-1)"
Try :
for (int i = 0; i<(n-1); i++)
array1[i] = (char)i - '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
This is probably a very simple question, however it is hounding me.
I can get the data into the container but I can't then seem to print it to the console.
The following produces a subscript out of range debug error
Here is my code
std::vector<std::vector<int>> myVector;
for (int x = 0; x != 18; x++)
{
myVector.push_back(std::vector<int>(x,x));
std::cout << myVector[x][x] << std::endl;
}
Many thanks!
When you do std::vector<int>(x,x) inside for loop when x=0 you allocate zero elements. At other times you allocate x elements(row index:x and column indexes: 0,1,...x-1) and access x+1th element in xth row.
So change it to
for (int x = 0; x != 18; x++)
{
myVector.push_back(vector<int>(x+1,x));
std::cout << myVector[x][x] << std::endl;
}
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
I am trying to implement the following function :
void dump() const
{
size_t it = 0;
std::cout << "[";
while (it < this->_size);
{
std::cout << (this->_arr)[it];
if ((this->_arr)[it + 1])
std::cout << ", ";
it++;
}
std::cout << "]" << std::endl;
}
This appears to cause an infinite loop, I'm guessing it comes from a bad use of std::cout, and std::endl, but I can see how to manage this. Any ideas?
As I have commented, you need to change
while (it < this->_size);
to
while (it < this->_size)
to make it a while-loop with the ability to change its condition in order to avoid infinite loop.