-
Insertion Sort Help
Hi, I am stumped as to why the following code isn't working properly:
(I will organize the code into functions and what not after...it was like that before, but i changed it many times to make it work)
Code:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
int testArray[30];
int n;
string answer;
for (n=0; n<30; n++)
{
testArray[n]=rand();
}
for(int z=0; z<30; z++)
{
cout << testArray[z] << " " << endl;
}
cout << "Would you like to sort the array?" << endl;
cin >> answer;
if(answer == "yes" || "Yes")
{
int key;
int i;
for(int j=1; j<30; j++)
{
key=testArray[j];
i=j-1;
while(testArray[i]>key && i>=0)
{
testArray[i+1]=testArray[i];
i--;
}
}
testArray[i+1]=key;
for(int p=0; p<30; p++)
{
cout << testArray[p] << " " << endl;
}
}
else if(answer == "no" || "No")
{
cout << "Ok..." << endl;
}
else
{
cout << "Your answer was invalid!" << endl;
}
return 0;
}
-
I solved it and corrected the code...so nvm. Thanks!
-
I guess you fixed this then?
Code:
if(answer == "yes" || "Yes")
->
Code:
if(answer == "yes" || answer == "Yes")