program problems.

This is a discussion on program problems. within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream.h> #include <string.h> #include <fstream.h> // this program is supposed to read a list of names from a ...

  1. #16
    Registered User
    Join Date
    Nov 2006
    Posts
    66
    Code:
    #include <iostream.h>
    #include <string.h>
    #include <fstream.h>
    
    // this program is supposed to read a list of names from a file and then bubble sort them alphabeticaly
    // and allow for binary search.
    
    const int max=100;
    
    int initialize_input(char inname[], ifstream& input_stream)
    {
    	cout<<"Please type name of input file to sort: ";
    	cin>>inname;
    		input_stream.open(inname);
    			if (input_stream.fail()!=0)
    			{
     				  cout<<"Failure to open input file--sorry";
     		  return -1;
       		}
    	else
    	return 0;
    };
    
    int initialize_output(char outname[], ofstream& output_stream)
    {
    	cout<<"Please type name of sorted output file: ";
    	cin>>outname;
    		output_stream.open(outname);
    			if (output_stream.fail()!=0)
    			{
    			   cout<<"Failure to open output file--sorry";
      			 return -1;
      		   }
    	else
    	return 0;
    };
    
    void bubblesort(char table[max][max],int array_size)
    {
        char temp[max];
           for(int count_1=0;count_1<array_size;count_1++)
    	  {
    	     for(int count_2=0;count_2<array_size-1;count_2++)
    		{
    		   if(table[count_2]>table[count_2+1])
    		      {
    			 strcmp(temp,table[count_2]);
    			 strcmp(table[count_2],table[count_2+1]);
    			 strcmp(table[count_2+1],temp);
    		      }
    		}
    	 }
         return;
    }
    
    
    
    int binary_search(int array[],int array_size,int element)
    {
           int start=0;
           int end=array_size-1;
           int middle;
           int position=-1;
    
           middle=(start+end)/2;
    
           do
    	  {
    	     if(element<array[middle])
    		end=middle-1;
    
    	     else if(element>array[middle])
    		start=middle+1;
    
    	     middle=(start+end)/2;
    	  }
           while(start<=end && array[middle]!=element);
    
           if(array[middle]==element)
    	  position=middle;
    
           return position;
    }
    
    
    void main ()
    {
    	ifstream income;
    	ofstream outgo;
       char answer;  char inname[15]; char outname[15];
    	char line[max];
    	char names[max][max];
    	int test;
    	int count;
    
    		do
    		{
    			test=initialize_input(inname,income);
    		if (test==-1)
    	return;
    
    count=0;
    
    	test=initialize_output(outname,outgo);
    	if (test==-1)
    	return;
    
    while (income.getline(names[count],100) )
    	{
       	cout<<names[count]<<endl;
    		count++;
       };
    
    cout<<"There are "<<count<<" lines in this file.\n";
    
    	bubblesort(names, count);
    		cout<<endl<<"Sorted Names: "<<endl;
    			for (int which=0;which<count;which++)
    			{
     			    cout<<names[which]<<endl;
                 outgo<<names[which]<<endl;
                 strcpy(line,names[which]);
      			 };
    
    cout<<"The sorted version of the file has been written to: "<<outname<<endl;
    
    income.close();
    
    outgo.close();
    
    cout<<"Would you like run again('Y' to continue)?\n";
    cin>>answer;
    
    }while (answer=='Y');
     else break;
    };

  2. #17
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    Did I say to change strcpy to strcmp?
    No, definitly not.
    I said change comparison with > symbol to strcmp...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #18
    Registered User
    Join Date
    Nov 2006
    Posts
    66
    is that here
    Code:
    		{
    		   if(table[count_2]>table[count_2+1])
    		      {
    meaning you meant this?
    Code:
    		{
    		   if(table[count_2]>strcmp table[count_2+1])
    		      {

  4. #19
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    the place is correct, the usage is incorrect
    you should read a manual on the strcmp() function
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #20
    Registered User
    Join Date
    Nov 2006
    Posts
    66
    I got the sort function to work! yay us!

    Now, where do I make the call to the search function?

    Code:
    #include <iostream.h>
    #include <string.h>
    #include <fstream.h>
    
    // this program is supposed to read a list of names from a file and then bubble sort them alphabeticaly
    // and allow for binary search.
    
    const int max=100;
    
    int initialize_input(char inname[], ifstream& input_stream)
    {
    	cout<<"Please type name of input file to sort: ";
    	cin>>inname;
    		input_stream.open(inname);
    			if (input_stream.fail()!=0)
    			{
     				  cout<<"Failure to open input file--sorry";
     		  return -1;
       		}
    	else
    	return 0;
    };
    
    int initialize_output(char outname[], ofstream& output_stream)
    {
    	cout<<"Please type name of sorted output file: ";
    	cin>>outname;
    		output_stream.open(outname);
    			if (output_stream.fail()!=0)
    			{
    			   cout<<"Failure to open output file--sorry";
      			 return -1;
      		   }
    	else
    	return 0;
    };
    
    void bubblesort(char table[max][max],int array_size)
    {
        char temp[max];
           for(int count_1=0;count_1<array_size;count_1++)
    	  {
         cout<<"outer loop\n";
    	     for(int count_2=0;count_2<array_size-1;count_2++)
    		{
          cout<<"inner loop\n";
    		   if(strcmp(table[count_2], table[count_2+1])>0)
               {
               cout<<"SWAPPING"<<count_2<<endl;
    			 strcpy(temp,table[count_2]);
    			 strcpy(table[count_2],table[count_2+1]);
    			 strcpy(table[count_2+1],temp);
              }
    		}
    	 }
         return;
    }
    
    
    
    int binary_search(int array[],int array_size,int element)
    {
           int start=0;
           int end=array_size-1;
           int middle;
           int position=-1;
    
           middle=(start+end)/2;
    
           do
    	  {
    	     if(element<array[middle])
    		end=middle-1;
    
    	     else if(element>array[middle])
    		start=middle+1;
    
    	     middle=(start+end)/2;
    	  }
           while(start<=end && array[middle]!=element);
    
           if(array[middle]==element)
    	  position=middle;
    
           return position;
    }
    
    
    void main ()
    {
    	ifstream income;
    	ofstream outgo;
       char answer;  char inname[15]; char outname[15];
    	char line[max];
    	char names[max][max];
       int array;
    	int test;
    	int count;
       int array_size;
       int element;
    
    
    do{
    cout<<"Beginning DO\n";
    			test=initialize_input(inname,income);
    		if (test==-1)
    	return;
    
    count=0;
    
    	test=initialize_output(outname,outgo);
    	if (test==-1)
    	return;
    
    while (income.getline(names[count],100) )
    	{
       	cout<<names[count]<<endl;
    		count++;
       };
    
    cout<<"There are "<<count<<" lines in this file.\n";
    
    	bubblesort(names, count);
    		cout<<endl<<"Sorted Names: "<<endl;
    			for (int which=0;which<count;which++)
    			{
     			    cout<<names[which]<<endl;
                 outgo<<names[which]<<endl;
                 strcpy(line,names[which]);
      			 };
    
    cout<<"The sorted version of the file has been written to: "<<outname<<endl;
    
    income.close();
    
    outgo.close();
    
     //	binary_search(array,array_size,element);
    
    cout<<"Would you like to run again('Y' to continue)?\n";
    cin>>answer;
    cout<<"your answer was:"<<answer<<'!';
    }while (answer=='Y');
    };

  6. #21
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    1) These:
    Code:
    #include <iostream.h>
    #include <string.h>
    #include <fstream.h>
    Should be:
    Code:
    #include <iostream>
    #include <cstring>
    #include <fstream>
    2)
    Code:
    void main()
    Should be:
    Code:
    int main()
    3)
    Code:
    else break;
    This is your problem. The else has no matching if test and the break is in no loop. Also, try to fix you indentation so that it is easier to read.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Problems with DLLEXPORT while updating a program
    By pirata in forum C++ Programming
    Replies: 3
    Last Post: 09-05-2008, 01:00 PM
  3. having problems with my card program
    By mac025 in forum C Programming
    Replies: 4
    Last Post: 01-31-2006, 03:26 PM
  4. structure problems in windows program.
    By Bajanine in forum Windows Programming
    Replies: 3
    Last Post: 04-19-2004, 06:18 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM

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