Why is $ \psi = A \cos(kx) $ not an acceptable wave function for a particle in a box? Why is $ \psi = A \cos(kx) $ not an acceptable wave function for a particle in a box with rigid walls at $x=0$ and $x=L$ where
$$ k = \frac {(2mE)^{1/2}} {\hbar} \, ?$$
I had plugged the wave function into the time-independent Schrodinger equation for a particle in a box. By solving the Schrodinger equation from both sides I saw that the left hand side equaled to the right hand side, hence the function is a solution to the Schrodinger Equation. 
I don't understand why this is not an acceptable wave function for a particle in a box with finite rigid walls.
 A: Indeed the cosine function is valid for instance for other boundary conditions $$\psi'(0)=\psi'(L)=0$$
Your goal when you look for a set of solutions to the Schroedinger equation is to be able to decompose any general wavefunction as a sum over this set, and to do it consistently your boundary conditions must be the same for all solutions. So it is unlikely that you are going to find a 1-D box with boundary conditions so retorted than both the sine and the cosine functions are solutions.
Mathematically it can be seen that there is a four-parameter family of conditions that you can impose in the values of the wavefunction and its derivative evaluated at points $0$ and $L$. If you add the physical condition of locality, so that probability is not magically transferred from one extreme to other -your box transformed then in a circle-, you have only two parameters to play with:
$$\psi(0) = -\alpha \ \psi'(0)$$ 
$$\psi(L) =  \beta \ \psi'(L)$$
where the derivatives are the ones taken inside the interval. The conditions can be seen as "bouncing" the probability wave with some extra phase, and you can think the whole set of four parameters as defining reflection and transmission (from one extreme to other) coefficients for the waves, but I will not complicate the answer writing them. Instead let me point out what it is happening technically: the hamiltonian for an one dimensional box is hermitian (symmetric) but not self-adjoint because its dual is defined in a different set of integrable functions, and one must consider their self-adjoint extensions, restrictions on the valid wavefunctions that grant the equality of the definition domains, the set of functions where the operators (momentum, position, energy hamiltonian...) and their adjoints act. A lot of papers can be found in the internet googling for this concept of "self-adjoint extensions", and I do hesitate about what to recommend, but for instance in page 21 of http://arxiv.org/abs/quant-ph/0103153 you can find your cosine :-)
Of course one must still explain with the usual conditions are choosen to be $\alpha = \beta=0$ and not infinite, or any finite value, or zero in one extreme and infinite in the other. The paper I have cited argues that it is because they proceed to build the system by raising the walls from a finite well.
A: The particle in a box is subjected to the further restriction that the potential function rapidly tends to infinity at $ x=0 $ and $ x=L $
This means that the position of the particle is restricted to being within the box.
As you said, the general solution to the schrodinger equation for particle in a box is $$ \psi (x)=A\cos kx + B\sin kx $$
Because the potential $ V(x) $ at $ x=0 $ and $ x=L $ is infinite, the particle will never be found here. i.e. the probability of finding the particle inside the wall is basically zero.
Hence we get the boundary condition $ \psi (0) = 0 $ and $ \psi (L) = 0 $
From the first condition, we get $ A = 0 $ and the second yields $ k = \frac {n\pi} L $
In essence in accordance with the $ A=0 $ from the first boundary condition, it is impossible for the solution $ \psi (x) = A\cos kx $ to be correct. Although this satisfies the Schrodinger equation, it will not satisfy the specified boundary conditions of the particle in a box, with walls at $ x=0 $ and $ x= L $
A: The answer by @arivero is perfect but I would like to present a bit different argument than the standard--the one which plays out in terms of the boundary condition. Of course, my arguments can (almost trivially) be translated into the boundary condition language. 

I had plugged the wave function into the time-independent Schrodinger equation for a particle in a box. By solving the Schrodinger equation from both sides, I saw that the left-hand side equaled to the right-hand side, hence the function is a solution to the Schrodinger equation.

Well, you didn't really see that the two sides are equal--or rather, you shouldn't have seen that the two sides are equal! 
Here is why: The time-independent Schrodinger equation needs to be satisfied for all values of $x$. What you essentially did was to verify that $\psi=A\cos(kx)$ solves the time-independent Schrodinger equation for a potential $V(x)=0$. But that is not really the particle-in-a-box problem--that is just the free-particle problem. And $ \psi = A \cos(kx) $ is a solution to that problem--no doubt! The potential for the particle-in-a-box problem is the following $$V(x)=0 \text{ for }x\neq0,L$$
$$V(x)=\infty\text{ for }x=0,L$$
So, in order to really solve the particle-in-a-box problem, we need to make sure that the time-independent Schrodinger equation is solved by our solution for this $V(x)$ which distinctively differs from the free-particle potential at $x=0,L$. Now, since $\psi = A \cos(kx)$ solves the time-independent Schrodinger equation for the free-particle problem, it does solve the particle-in-a-box problem as well for all points $x\neq 0, L$, But we must also take care of $x=0,L$. So, let's ask the question as to whether \psi = A \cos(kx) satisfies the time-independent Schrodinger equation at $x=0,L$ or not. 
The time-independent Schrodinger equation reads $$-\dfrac{\hbar^2}{2m}\dfrac{d^2}{dx^2}\psi(x)+V(x)\psi(x)=E\psi(x)$$
Now, if we put in $\psi = A \cos(kx)$ then for it to be the solution, the following should necessarily hold true
$$\dfrac{\hbar^2k^2}{2m}\cos{0}+V(0)\cos{0}=E\cos{0}$$
Or, equivalently, 
$$V(0)=E-\dfrac{\hbar^2k^2}{2m}=0$$
There is no way for this to be true since $V(0)=\infty$ and thus, our proposed solution is simply wrong! In particular, it fails to satisfy the time-independent Schrodinger equation at $x=0$.
