Infinite loop happenning [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 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.

Related

Positive input is returning as negative [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 11 months ago.
The community reviewed whether to reopen this question 11 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
My "num = -num" line inside of my "if (num<0)" line still affects results, even if my input is greater than 0.
#include <iostream>
int main()
{
std::cout << "Enter a positive number: ";
int num{};
std::cin >> num;
if (num < 0)
std::cout << "Negative number entered. Making positive.\n";
num = -num;
std::cout << "You entered: " << num;
return 0;
}
To have multiple statements inside an if, you must use brackets.
And at this point of learning the language, I would recommend just always using brackets.
if (num < 0) {
std::cout << "Negative number entered. Making positive.\n";
num = -num;
}
Unlike languages like python, leading whitespace is not meaningful to the compiler in C++.

Why does my array only print once and not twice here? [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 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.

How to make a proper line break in a C++ program [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 2 years ago.
Improve this question
What is wrong with my code? I try to get a line break using both \n and endl but it shows an error.
#include <iostream>
using namespace std;
int main()
{
const int MinutesPerHour = 60;
const float PI = 3.14;
cout << MinutesPerHour;endl;
cout << PI;
return 0;
}
You should place the endl after the output stream operator <<.
Like this:
cout << MinutesPerHour << endl;

Why does "std::cout << myVector[x][x] << std::endl" produce a subscript error? [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
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;
}

I am learning C++ via a book. What I am doing wrong? [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 am learning C++ via a book. It told me to type this compile and link it in order for me to see if i can read it. But there are errors when I run this. What is the issue?
#include <iostream>
int main()
{
int x = 8;
int y = 6;
std::cout << std::end1;
std::cout << x - y << " " << x * y << " " << x + y;
std::cout << std::end1; return 0;
}
It should be std::endl; instead of std::end1;