Your solution goes further than the book's solution in terms of correctness. You have two issues. The first is that changing $\left|\cos\frac{x}{2}\right|$ to $\pm\cos\frac{x}{2}$ only creates a loss of information. Moreover, the $\pm$ sign itself depends on that value of $\cos\frac{x}{2}$, so it makes no sense to pull out the $\pm$ sign here, even if you did have it.
Integrating over absolute value signs is tricky. I'll use the definite integral to give an answer to the indefinite integral.
$$ \sqrt{2}\int{\left|\cos\frac{x}{2}\right|}dx = \sqrt{2}\int_{0}^{x}{\left|\cos\frac{u}{2}\right|}du + C $$
In the case of a definite integral, you have to split your domain into intervals on a case by case basis.
Case 1: $x>\pi$.
In this case, we have
\begin{align}
\sqrt{2}\int_{0}^{x}{\left|\cos\frac{u}{2}\right|}du + C &= \sqrt{2}\int_{0}^{\pi} \left|\cos\frac{u}{2}\right| du \\
& \quad+ \left( \sqrt{2}\int_{\pi}^{\pi+2\pi} \left|\cos\frac{u}{2}\right| du + \cdots + \sqrt{2}\int_{\pi+(n-1)2\pi}^{\pi+n2\pi} \left|\cos\frac{u}{2}\right| du \right) \\
& \quad+ \sqrt{2}\int_{\pi+n2\pi}^{x} \left|\cos\frac{u}{2}\right| du + C \qquad\qquad
\end{align}
where $n = \left\lfloor \dfrac{x-\pi}{2\pi}\right\rfloor$.
We look at the various definite integrals separately. First, note that $\cos\frac{u}{2}$ is nonnegative for all $u\in (0, \pi)$. This means we have $\left|\cos\frac{u}{2}\right| = \cos\frac{u}{2}$ on that interval. Hence
$$ \sqrt{2}\int_{0}^{\pi} \left|\cos\frac{u}{2}\right| du = \sqrt{2}\int_{0}^{\pi} \cos\frac{u}{2} du = 2\sqrt{2}. $$
Next, using periodicity of cosine, we can deduce that
$$ \sqrt{2}\int_{\pi+(k-1)2\pi}^{\pi+k2\pi} \left|\cos\frac{u}{2}\right| du = \sqrt{2}\int_{-\pi}^{\pi} \cos\frac{u}{2} du = 4\sqrt{2} $$
for each integer $k$.
Lastly, we need to do the last integral, which is going to give us a function of $x$. Again, using properties of cosine, we can deduce that
\begin{align}
\sqrt{2}\int_{\pi+n2\pi}^{x} \left|\cos\frac{u}{2}\right| du &= \sqrt{2}\int_{-\pi}^{x - 2(n+1)\pi} \cos\frac{u}{2} du = \sqrt{2}\cdot
2\sin\left(\frac{u}{2}\right) \Big|_{-\pi}^{x-(n+1)2\pi} \\
&= 2\sqrt{2} \left[ \sin\left(\frac{x-(n+1)2\pi}{2}\right) - \sin\left(-\frac{\pi}{2}\right) \right] \\
&= 2\sqrt{2} \left[ \sin\left(\frac{x}{2}-(n+1)\pi\right) - (-1) \right] \\
&= 2\sqrt{2} \left[ \sin\left(\frac{x}{2}\right)\cos(-(n+1)\pi) + \sin(-(n+1)\pi)\cos\left(\frac{x}{2}\right) + 1 \right] \\
&= 2\sqrt{2} \left[ \sin\left(\frac{x}{2}\right)(-1)^{n+1} + 0 + 1 \right] \\
&= 2\sqrt{2} \left[ 1 + (-1)^{n+1}\sin\left(\frac{x}{2}\right) \right]
\end{align}
Going all the way back to the beginning of Case 1, and putting this all together, we have
\begin{align}
\sqrt{2}\int_{0}^{x}{\left|\cos\frac{u}{2}\right|}du + C &= 2\sqrt{2} + \underbrace{(4\sqrt{2} + \cdots + 4\sqrt{2})}_{n\text{ terms}} + 2\sqrt{2}[1 + (-1)^{n+1}\sin\left(\frac{x}{2}\right)] + C \\[1.2ex]
&= 2\sqrt{2} + n(4\sqrt{2}) + 2\sqrt{2} + 2\sqrt{2}(-1)^{n+1}\sin\left(\frac{x}{2}\right) + C \\[1.6ex]
&= (n+1)\cdot 4\sqrt{2} + 2\sqrt{2}(-1)^{n+1}\sin\left(\frac{x}{2}\right) + C
\end{align}
Thus, for $x > \pi$ we have
$$ \sqrt{2}\int{\left|\cos\frac{x}{2}\right|}dx = 4\sqrt{2}(n+1) + 2\sqrt{2}(-1)^{n+1}\sin\left(\frac{x}{2}\right) + C $$
where $n = \left\lfloor \dfrac{x-\pi}{2\pi}\right\rfloor$.
The solution might look weird, but when I graph it in desmos for $C=0$ I get a continuous function.
Cases for $0\le x\le \pi$, $-\pi\le x<0$, and $x<-\pi$ are similar. I'll leave these other cases to you :)
The reason why we have all these cases and the reason why we need to split our definite integral over various intervals is because the absolute value function is actually a piecewise function that is itself handled by cases.
$$\sqrt{2}\int{|\cos\frac{x}{2}|dx}=\begin{cases} 2\sqrt{2}\sin\frac{x}{2}+C,\ \cos\frac{x}{2}\ge 0 \\ -2\sqrt{2}\sin\frac{x}{2}+C,\ \cos\frac{x}{2}<0 \end{cases}$$
sir do you mean writing like this?
– tryingtobeastoic Jan 25 '22 at 05:25