+1 vote
by
The task is to write a program exactly through a function, but when you return a value along with a number, it outputs 0 because of return. Is there any way to fix this?
#include <iostream>
#include "header.h"
using namespace std;

int main()
{
setlocale(LC_ALL, "russian");
int digit = 0;
cout << NumNewline(digit);
return 0;
}

int NumNewline(int num)
{
cout << "Введите число: ";
cin >> num;
while (num)
{
cout << num % 10 << endl;
num /= 10;
}
return 0;
}

1 Answer

+3 votes
by
cout << NumNewline(digit);
Why is there a cout?

return 0;
Why is there a return?
by
Probably because the function must return some value? And I didn't ask to answer my question with a question.
by
Timur Pokrovsky well, because in any function (except void) you have to return something
by
Timur Pokrovsky It just seems that half of the respondents do not even know the language to which they are answering the question.
by
myspacebarisbroken and why would you return any value if you didn't need it?
by
It seems that you have written some nonsense in the code and now you want something from us.

> "I return 0 from a function and then output it to stdout - help me, how can I not output 0 to stdout?"
What answer to this question do you expect?
by
myspacebarisbroken No, we know the language, we don't understand your logic
by
myspacebarisbroken , so make a void function
by
Timur Pokrovsky "So show me how it's done, since you're such a genius."
by
Timur Pokrovsky I can answer questions that way too, it doesn't take much intelligence.
by
myspacebarisbroken ,
1. You don't need to output 0? Don't print it!
2. You don't need to return 0 from a function? Make it void and don't return anything!
How else can I give you a hint as to what needs to be done?
by
Alexei Ukolov So show me how it's done, since you're such a genius.
by
God... I hope that at least with Third times will reach .
by
myspacebarisbroken ,
#include <iostream>
#include "header.h"
using namespace std;

int main()
{
setlocale(LC_ALL, "russian");
int digit = 0;
NumNewline(digit); // не вывожу 0!
return 0;
}

void NumNewline(int num)
{
cout << "Введите число: ";
cin >> num;
while (num)
{
cout << num % 10 << endl;
num /= 10;
}
// не возвращаю 0!
}
by
Alexei Ukolov , read your answer. You asked a question about why you need return in a function, even though all functions need to return something (except void). You didn't write that I should have used void instead of int. Then your comment would have been the solution.
by
No one was teasing you (originally) - I (and not only me) pointed out the sources of problems in your code. And, since the question is elementary, this should have been the end of it, but no...

If you had bothered to answer the questions asked for yourself (I knew the answers from the beginning), the solution would have come to you and you would have gained experience in solving problems.
by
Michael ,
And I didn't ask to answer my question with a question.

So show me how to do it, since you are such a genius.

I can answer questions that way too, it doesn't take much intelligence


Nobody owes you anything here. If you don't understand what you copied yourself, then learn the language.
by
datka I didn't copy it, I wrote it myself. This is the first. Second - the section is called Q & A, and even the dumb person will understand that if you ask a question, you need to answer specifically, and not tease, answering questions. And I don't need to be flubbed and advice from a dilettante about what I need to learn.
by
Alexei Ukolov As expected of a short-sighted man, it's all about personalities. The dialogue is over.
by
Alexei Ukolov I don't notice, by the way, that you knew the answers in the first place. It's more like you answer every question with the hope that someone will mark it as a solution. If I asked a question, I want an answer, not a riddle that you think I should solve.
by
although all functions need to return something (except void). You didn't write that I should have used void instead of int.
although all functions need to return something (except void). You didn't write that I should have used void instead of int.

It is inconspicuous, by the way, that you knew the answers in the first place. It's more like you answer each question with the hope that someone will mark it as a solution.
Losers :)
Losers :)
...