Question: I'm not sure how to write my dialogue should it be just in one line like
"We have to do it." "Why?"
or in separate lines like
"We have to do it."
Which way do books usually have it written?Answer:
The general rule in writing dialogue is to start a new paragraph each time you change speakers. This helps let the reader know when the change occurs and keep track of who says what.
This is especially important in cases like your example, when there are no speech tags (e.g. "said John"), action beats (e.g. "John scratched his nose."), or any other cues to identify the speaker of each quotation.
I'm sure you can imagine that if you had a long back-and-forth conversation with no such cues and all in one paragraph, could become very confusing.
Let me also point out that, whenever you are in doubt about something like this, the easiest way to find out is to pick up a few books and look for examples. While literary fiction often plays fast and loose with the rules (because it tends to prize a certain style over clarity), most books follow the standard rules for the country where the book is published.
(There are a few differences between the American, British, and Canadian style guides, but they are minor.)