program problems.

This is a discussion on program problems. within the C++ Programming forums, part of the General Programming Boards category; I am getting nowhere on this one. any help would be appreciated. Code: #include <iostream.h> #include <string.h> #include <fstream.h> // ...

  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
    Portugal
    Posts
    7,578
    You already know better than that nesagsar.

    State your problem, where it happens, and what you intend to do.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    int max=100;
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    32,823
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    in function bubble_sort
    return ;
    fixing indentetion and pointing to the error line with comment will help
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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

  14. #14
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    use strcmp function to compare C-strings
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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

Page 1 of 2 12 LastLast
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, 02:42 AM
  2. Problems with DLLEXPORT while updating a program
    By pirata in forum C++ Programming
    Replies: 3
    Last Post: 09-05-2008, 02: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, 07:18 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM

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