Thread: String Display Problem...

  1. #1
    System-7
    Join Date
    Nov 2005
    Posts
    65

    Question String Display Problem...

    I've finished the code but now for some reason, which I have yet to figure out, there is an additional 0 being put into the output which is pushing out the smallest number in the array. Hopefully you guys can give me a bit of help here because I have been trying for so long to figure this out.

    Code:
    #include <iostream>
    using namespace std;
    
    int getElements(signed long int elements[]);
    void BubbleSort(signed long int elements[], int passes);
    void DispResult(signed long int elements[], int passes);
    
    int main(void)
    {
    	int passes;
    	signed long int elements[6] = {0,0,0,0,0};
    
    	cout << "Enter up to 5 integers or end-of-file (ctrl-z, enter, ctrl-z, enter)" << endl;
    
    	passes = getElements(elements);
    	BubbleSort(elements, passes);
    	DispResult(elements, passes);
    
    	return 0;
    }
    int getElements(signed long int elements[])
    {
    	int n, elem_num = 1;
    
    	for (n=0; n<5; n++)
    	{
    		cout << "Please enter element " << (elem_num) << ":";
    		cin >> elements[n];
    
    		while ((elements[n] > 1000000) || (elements[n] < -1000000))
    		{
    			cerr << "The integer must range from -1,000,000 to +1,000,000" << endl;
    			cerr << "Please enter another integer: ";
    			cin >> elements[n];
    		}
    		
    		elem_num++;
    	}
    	return elem_num;
    }
    void BubbleSort(signed long int elements[], int passes)
    {
        int a, b, temp_num=0;
    
        for(a = 0; a < (passes -1) ; ++a)
        {
    		for(b = 1; b < passes; ++b)
    		{
    			if(elements[b-1] > elements[b] )
    			{
    				temp_num = elements[b];
    				elements[b] = elements[b-1];
    				elements[b-1] = temp_num;
    			}
    		}
    	}	
    }
    void DispResult(signed long int elements[], int passes)
    {
    	int c, num_element = 1;
    	cout << "Sorted array" << endl;
    
    	for(c=1; c <= (passes-1); c++)
    	{
    		cout << "Element " << num_element << ":" << elements[c] << endl;
    
    		num_element++;
    	}
    }
    Thanks for any help you can give,

    ...Dan

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    You elements array is of size 6, but you only put in 5 things.
    If you have and array and you initialize it with the = { x,x,x,x} and you do not set a value to everything, whatever you left out will automatically get initialized to 0.
    You also have some logic flaws. Sometimes your code will work right, others it will not. Go through step by step, if you can not figure out just say something. Somebody is always here!
    Last edited by Enahs; 11-29-2005 at 10:07 PM.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Quote Originally Posted by Enahs
    You elements array is of size 6, but you only put in 5 things.
    If you have and array and you initialize it with the = { x,x,x,x} and you do not set a value to everything, whatever you left out will automatically get initialized to 0.
    In regards to Enahs post, since an array element will automatically be initialized to 0 if: 1) you use an initializer list, and 2) you fail to specify a value for the element, you can initialize every element of an array to 0 very easily by doing this:
    Code:
    int myArray[20] = {0};
    That initializes the first element of the array, and since all the other elements of the array do not have values specified, they will automatically be initialized to 0.
    Last edited by 7stud; 11-29-2005 at 10:52 PM.

  4. #4
    System-7
    Join Date
    Nov 2005
    Posts
    65
    Thanks for the responses.

    Alright, so I fixed the problems I was having and the only problem now is when i have to use cin.eof(). The problem is that it creates a 0 in the array. The big deal about this is that the 0 is brought into bubbleSort and now I have this extra value being sorted that I dont want. So I would put in like 1,2,3,4,5 then do ctrl-z, enter, ctrl-z, enter and then the sorted output is 0,1,2,3,4,5...how do i get rid of this extra zero?

    Here's my "almost" finished code:
    Code:
    #include <iostream>
    using namespace std;
    
    int getElements(long int elements[]);
    void bubbleSort(long int elements[], int count);
    void dispResults(long int elements[], int count);
    
    int main(void)
    {
    	int count = 0;
    	long int elements[25] = {0};
    	
    	count = getElements(elements);
    	bubbleSort(elements, count);
    	dispResults(elements, count);
    
    	return 0;
    }
    int getElements(long int elements[])
    {
    	while (!cin.eof())
    	{
    	int sub_num, pass_count = 0;
    
    	for (sub_num = 0; (sub_num < 25) && !cin.eof(); sub_num++)
    	{
    		cout << "Enter Element: ";
    		cin >> elements[sub_num];
    
    		while ((elements[sub_num] > 1000000) || (elements[sub_num] < -1000000))	
    		{
    			cerr << "INPUT ERROR - Please enter an integer from -1,000,000 to +1,000,000" << endl;
    			cerr << "Please enter another element: ";
    			cin >> elements[sub_num];
    		}
    		pass_count++;
    	}
    	
    	return pass_count;
    	}
    }
    void bubbleSort(long int elements[], int count)
    {
    	int i, j;
    	long int temp;
    
    	for (i = 0; i < (count - 1) ; ++i)
    	{
    		for (j = 1; j < count; ++j)
    		{
    			if (elements[j-1] > elements[j] )
    			{
    				temp = elements[j];
    				elements[j] = elements[j-1];
    				elements[j-1] = temp;
    			}
    		}
    	}
    }
    void dispResults(long int elements[], int count)
    {
    	int subscr_num, num_element = 1;
    
    	cout << "\n";
    	cout << "Sorted Array" << endl;
    
    	for (subscr_num=0; subscr_num < count; subscr_num++)
    	{
    		cout << "Element " << num_element << ":" << elements[subscr_num] << endl;
    
    		num_element++;
    	}
    }
    Thanks for the help,

    ...Dan
    Last edited by Dan17; 11-30-2005 at 10:25 AM.

  5. #5
    System-7
    Join Date
    Nov 2005
    Posts
    65
    Also, I need to use cin.eof (assignment says so) so please dont say not to use it.

    ...Dan

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Can you state the purpose of your while loop in getElements()?

    ---
    So I would put in like 1,2,3,4,5 then do ctrl-z, enter, ctrl-z, enter
    How is that going to work when you do this every time a number is read in:
    Code:
    cout << "Enter Element: ";
    Also, why won't putting in 1,2,3,4,5 and just hitting <enter> suffice?
    Last edited by 7stud; 11-30-2005 at 11:16 AM.

  7. #7
    System-7
    Join Date
    Nov 2005
    Posts
    65
    Well what it's actually gonna say is like:
    Code:
     cout << "Enter Element " << (pass_count + 1) << ": ";
    I just hadn't gotten completely specific. The program is supposed to recieve a max of 25 randomly inputted characters and then sort them smallest to largest. We are supposed to include cin.eof() so that the user can exit anytime and it will display the inputted characters in order up to when they pressed ctrl-z, enter, ctrl-z, enter.

    One while loop is for the cin.eof() so that the program can end early
    and the other is for error checking the input

    ...Dan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string problem
    By INeedSleep in forum C++ Programming
    Replies: 24
    Last Post: 11-08-2007, 11:52 PM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM