Thread: array sort output problem

  1. #1
    The code loser
    Join Date
    Mar 2007
    Posts
    13

    array sort output problem

    okay here is my problem now

    if the input is
    Code:
    penny
    henry
    george
    sammy
    vanne
    then the output is
    Code:
    george
    henry
    penny
    sammy
    where did vanne go?

    my program as of now
    Code:
    //this program 
    //
    //program4.cpp
    //my name. (im not going to tell you this)
    
    #include <fstream> 
    #include <iostream>
    #include <string>
    
    
    using namespace std; // using standard namespace
    
    int main ()
    {
    	char file[20];
    	string str;
    	string stri[100];
    	int quit =24;
    	cout << "enter the name of the input file in the format filename.txt: " ; //user determines the file to write to
    	cin >> file;
    	cout << endl;
    	fstream myfil (file);
        if (myfil.is_open())
    	{
    		quit=0;
    	}
    	else quit=1;
    	while (quit==1) // keeps the user from trying to write in a close file
        {
    	
    		cout << "file could not be opened. enter the name of another file in the format filename.txt:" ; //error message
    		cin >> file;
    		fstream myfil ("example.txt");
    		if (myfil.is_open())
    		{
    			quit=0;
    		}
    	}
    
    	for (int x=1; x<=100; x++)
    	myfil >> stri[x-1];
    
    
    	bool swap;
    	string temp;
    	int bottom = 99;     
    	do
    	{
    		swap = false;
    		for (int count = 0; count <= bottom; count++)
    		{
    			if (stri[count][0] > stri[count+1][0])
    			{	          
    			   temp = stri[count];  
    			   stri[count] = stri[count+1];
    			   stri[count+1] = temp;
    			   swap = true; // indicates that a swap occurred
    			}
    		}
                bottom--;   
    	            
    	} while(swap != false);
                  // loop repeats until a pass through the array with
    	          // no swaps occurs
    
    
    
    	cout << "enter the name of the output file in the format filename.txt: " ; //user determines the file to write to
    	cin >> file;
    	
    	ofstream myfile (file);
        if (myfile.is_open())
    	{
    		quit=0;
    	}
    	else quit=1;
    	while (quit==1) // keeps the user from trying to write in a close file
        {
    	
    		cout << "file could not be opened. enter the name of another file in the format filename.txt: " ; //error message
    		cin >> file;
    		ofstream myfile ("example.txt");
    		if (myfile.is_open())
    		{
    			quit=0;
    		}
    	}
    
    	for (int y =0; y<100; y++)
    	
    	{
    	str = stri[y] ;
    	if (str!="")
    	myfile <<str << endl;
    	
    	}
    	
    	
    		
    	
    		 
    	myfile.close();
    	myfil.close();// close the file
    	return 0;
    }

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The answer is simple: Buffer overruns.

    Changes both <='s to <'s

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Also start x at zero, and just use x instead of x-1. Same as your y loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Dynamically Increasing Array of Integers
    By laserlight in forum C++ Programming
    Replies: 30
    Last Post: 07-04-2008, 07:27 AM
  2. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  3. Help with output from array
    By jaw24 in forum C++ Programming
    Replies: 4
    Last Post: 04-05-2007, 09:16 PM
  4. Insertion Sort Problem
    By silicon in forum C++ Programming
    Replies: 1
    Last Post: 05-08-2005, 12:30 PM
  5. problem with an array of strings in C
    By ornamatica in forum C Programming
    Replies: 14
    Last Post: 05-01-2002, 06:08 PM