How to make a proper line break in a C++ program [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 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;

Related

for loop result 0 in 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 5 years ago.
Improve this question
I am a newbie in C++, I started to learn coding in C++ two weeks ago. Why does my code below always give me result 0 when I build and run? Please help
# include <iostream>
# include <string>
using namespace std;
int main ()
{
int input = 1;
cout << "input your number : \n";
cin >> input;
int faktorial = 1;
for(int i=1;i<=input;i++)
{
faktorial = faktorial * i;
}
cout << "factorial value from number " << input << " is " << faktorial << endl;
}
Your code works: https://ideone.com/CYFaxo
I suspect your problem is, you are looking at program exit code. When you don't return any value from main, program exit code is 0 (this is special case, and only non-void function where you may leave the return statement out), which conventionally means success (non-zero exit code usually indicates some kind of error, by convention).
Try to find the program output from your IDE, it should have the correct printout.

The output begins with space [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 7 years ago.
Improve this question
I made a function that will reverse the string, but the output of the reversed string always shifts towards the right by one character.
#include <iostream>
#include <string>
using namespace std;
void reverse(string string1)
{
cout << endl;
for (int i = string1.size(); i >= 0; i--)
{
cout << string1[i];
}
cout << endl;
}
int main()
{
string string1;
getline(cin, string1);
reverse(string1);
system("pause");
return 0;
}
Your first output is of a character that does not exist.
std::string's leaky abstraction means that your first iteration is printing '\0', which apparently looks like a space in your configuration.
Begin at string1.size() - 1.

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;

Can't Print String Array Element [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
Whenever I try to run this program it returns an error saying:
no operator "<<" matches these operands
Also note that the program only runs into this problem in the getChoice() function.
#include <iostream>
#include "utilities.h"
using namespace std;
int getChoice(string inChoices[]){
int numOfChoices = sizeof(inChoices) / sizeof(inChoices[0]);
string x = inChoices[0];
string y = inChoices[1];
cout << x << endl << y << endl;
return numOfChoices;
}
int main()
{
string choices[2] = { "Happy Day", "Even Better Day" };
cout << utilities::getChoice(choices) << endl;
cout << endl << sizeof(choices) / sizeof(choices[0]) << endl;
}
You need also to include the string header:
#include <string>
You need to #include <string>
And your calculation of numOfChoices in getChoice() is wrong, since the parameter inChoices is actually a "pointer to string" instead of "array of strings".

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