Insertion Sort Help

This is a discussion on Insertion Sort Help within the C++ Programming forums, part of the General Programming Boards category; Hi, I am stumped as to why the following code isn't working properly: (I will organize the code into functions ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    16

    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;
    }
    Last edited by vgame64; 09-07-2006 at 08:17 PM.

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    16
    I solved it and corrected the code...so nvm. Thanks!

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    I guess you fixed this then?
    Code:
    if(answer == "yes" || "Yes")
    ->
    Code:
    if(answer == "yes" || answer == "Yes")
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-02-2008, 06:23 AM
  2. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM
  3. Insertion Sort on Array of Structs
    By n0r3gr3tz in forum C Programming
    Replies: 3
    Last Post: 04-01-2008, 08:28 AM
  4. Insertion Sort Problem
    By silicon in forum C++ Programming
    Replies: 1
    Last Post: 05-08-2005, 12:30 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21