Thread: program problems.

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    66

    program problems.

    I am getting nowhere on this one. any help would be appreciated.

    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.
    
    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(long array[],int array_size)
        {
           for(int count_1=0;count_1<array_size;count_1++)
    	  {
    	     for(int count_2=0;count_2<array_size-1;count_2++)
    		{
    		   if(array[count_2]>array[count_2+1])
    		      {
    			 long temp=array[count_2];
    			 array[count_2]=array[count_2+1];
    			 array[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";
    
    sorting(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');
    };

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    You already know better than that nesagsar.

    State your problem, where it happens, and what you intend to do.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    int max=100;
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    66
    Code:
    Info :sort and search hoskins beta v1.cpp: out of date with destination sort and search hoskins beta v1.obj
    Info :  sort and search hoskins beta v1.cpp: source date 11:37:01 AM 12/13/2006  destination date <unknown>
    Info :Compiling C:\Documents and Settings\labpc\Desktop\sort and search hoskins beta v1.cpp
    Error:  sort and search hoskins beta v1.cpp(53,6):Expression syntax
    Error:  sort and search hoskins beta v1.cpp(53,6):'bubblesort(long *,int)' cannot return a value
    Error:  sort and search hoskins beta v1.cpp(53,6):Return statement missing ;
    Error:  sort and search hoskins beta v1.cpp(58,6):Declaration syntax error
    Error:  sort and search hoskins beta v1.cpp(136,1):Compound statement missing }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So put a ; on the end of the return statement at line 53 - those are what those numbers are, line numbers.

    Also, learn how to indent code more consistently, then a whole bunch of problems similar to "Compound statement missing" just vanish, because it becomes very obvious something is missing before you ever press "compile"

    Also, main returns an int.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    in function bubble_sort
    return ;
    fixing indentetion and pointing to the error line with comment will help
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    66
    the program isnt giving me any errors now but still dosent work right.

    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])
    		      {
    			 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 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');
    };

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    table[count_2]>table[count_2+1]

    to compare strings use strcmp()

    or better - use C++ strings instead of C-style strings... You are learning C++ not C...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    66
    My C++ teacher typed in the string code. He just emailed it to me. Is it wrong?

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by nesagsar
    My C++ teacher typed in the string code. He just emailed it to me. Is it wrong?
    Couldn't say without looking on the code he sent.

    comparing C-style strings with > IS wrong

    teaching to use C-style strings before C++ strings - discussable. IMHO better start with C++ ideology of using classes and only then fallback to C-style extentions... I learned the opposite way and I'm still a C-guy
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Registered User
    Join Date
    Nov 2006
    Posts
    66
    So is that why my sort routine dosent actually sort the names in the file?

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by nesagsar
    So is that why my sort routine dosent actually sort the names in the file?
    It will definitely prevent the algorithm from working.
    Cannot gurantee that there is no other errors
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #13
    Registered User
    Join Date
    Nov 2006
    Posts
    66
    Do you have an example of how to fix it?

  14. #14
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    use strcmp function to compare C-strings
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  15. #15
    Registered User
    Join Date
    Nov 2006
    Posts
    66
    I changed the ctrcpy lines to strcmp and I complied. Now I have these errors.

    Code:
    Info :sort and search hoskins beta v1.cpp: build due to .OBJ dependency sort and search hoskins beta v1.cpp
    Info :  sort and search hoskins beta v1.cpp: cached age 12:27:04 PM 12/13/2006  file age 1:03:28 PM 12/13/2006
    Info :Compiling C:\Documents and Settings\labpc\Desktop\sort and search hoskins beta v1.cpp
    Error:  sort and search hoskins beta v1.cpp(135,6):Misplaced else
    Error:  sort and search hoskins beta v1.cpp(135,13):Misplaced break

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, 04: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, 12:39 PM