Help with output from array

This is a discussion on Help with output from array within the C++ Programming forums, part of the General Programming Boards category; I am writing a program to read a sentence into an array and then delete all repeated letters and spaces ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    13

    Help with output from array

    I am writing a program to read a sentence into an array and then delete all repeated letters and spaces and then print out the new sentence. after a repeated letter is deleted the remaining letters are moved up to fill in the gap. The output also includes the size of the new array. The problem is my output only shows four letters, sometimes the first 4 letters of the sentence and sometimes 'tttt' and new array size as 4, no matter what I type in. Thank you in advance for any assistance provided.

    Code:
    #include <iostream>
    
    void introduction();
    //Explains what the program does
    
    void fill_array(char a[], int size, int& number_used);
    //Array a[] is filled with data from the keyboard
    
    void delete_repeats(char a[], int& number_used);
    //Function will remove all repeated characters and move the rest of the characters
    //foward to fill in the gap.
    
    void output(char a[], int& number_used);
    //Outputs the contents of the array and outs the new size of the array
    
    int main()
    {
    	using namespace std;
    	char array[100];
    	int number_used;
    	introduction();
    	fill_array(array, 100, number_used);
    	delete_repeats(array, number_used);
    	output(array, number_used);
    }
    
    //uses iostream
    void introduction()
    {
    	using namespace std;
    	cout << "This program will will ask the user to type in a sentence\n"
    	     << "and then will delete all repeated characters of the sentence.\n"
    	     << "The program will then output the new sentence with all repeated\n"
    	     << "letters deleted\n";
    }
    
    //uses iostream
    void fill_array(char a[], int size, int& number_used)
    {
    	using namespace std;
    	char c;
    	int index = 0;
    	cout << "Please type in a sentence and then press enter.\n";
    	cin.get(c);
    	while (c != '\n' && index < size)
    	{
    		index++;
    		a[index] = c;
    		cin.get(c);
    	}
    	number_used = index;
    }
    
    //uses iostream
    void delete_repeats(char a[], int& number_used)
    {
    	using namespace std;
    	for (int i = 0; i < number_used; i++)
    	{
    		for (int j = i + 1; j < number_used; j++)
    		{
    			if (a[i] == a[j])
    			{
    				for (int k = j; k < number_used; k++)
    				a[k] = a[k + 1];
    			}
    		}
    	}
    	number_used = sizeof a;
    }
    
    //uses iostream
    void output(char a[], int& number_used)
    {
    	using namespace std;
    	cout << "The new sentence without the repeated letters is:\n";
    		for (int i = 0; i < number_used; i++)
    		{
    			cout << a[i];
    		}		
    	cout << "\nThe size of the new array is "
    	     << number_used
    	     << endl;
    }

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Ok well a few little things i noticed, i dunno if they'll fix it but here goes.

    Why not declare using namespace std; globally? So then you don't have to retype it every time.

    Also...

    Make sure you're sizeof() is working properly. I recently learned that sometimes thay doesnt work inside a function


    Good luck!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> number_used = sizeof a;
    Junior89 is right, this won't work. This will return the size of a pointer, which is 4 bytes on your machine. That is why you always get 4.

    I would keep count of the number of elements deleted and subtract it from the original number used, or keep counts of the number of elements not deleted.

    Also note that array indexes start at 0, not 1. Your loop that reads in characters seems to be adding characters to indexes 1 to 100 because it increments index before assigning the new character to the array.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    Code:
    #include <iostream>
    
    void introduction();
    //Explains what the program does
    
    void fill_array(char a[], int size, int& number_used);
    //Array a[] is filled with data from the keyboard
    
    void delete_repeats(char a[], int& number_used);
    //Function will remove all repeated characters and move the rest of the characters
    //foward to fill in the gap.
    
    void output(char a[], int& number_used);
    //Outputs the contents of the array and outs the new size of the array
    
    int main()
    {
    	using namespace std;
    	char array[100];
    	int number_used;
    	introduction();
    	fill_array(array, 100, number_used);
    	output(array, number_used);
    }
    
    //uses iostream
    void introduction()
    {
    	using namespace std;
    	cout << "This program will will ask the user to type in a sentence\n"
    	     << "and then will delete all repeated characters of the sentence.\n"
    	     << "The program will then output the new sentence with all repeated\n"
    	     << "letters deleted\n";
    }
    
    //uses iostream
    void fill_array(char a[], int size, int& number_used)
    {
    	using namespace std;
    	char c;
    	int index = 0;
    	cout << "Please type in a sentence and then press enter.\n";
    	cin.get(c);
    
    
    	while (c != '\n' && index < size)
    	{
    		a[index] = c;
    		cin.get(c);
    		index++;
    	}
    	number_used = index;
    
    
    	for (int i = 0; i < number_used; i++)
    	{
    		for (int j = i + 1; j < number_used; j++)
    		{
    			if (a[i] == a[j])
    			{
    			    number_used=number_used-1;
    				for (int k = j; k < number_used; k++)
    				a[k] = a[k + 1];
    			}
    		}
    		cout << a[i]<<std::endl;
    		cout << number_used<<std::endl;
    	}
    	cout << number_used<<std::endl;
    }
    
    
    
    //uses iostream
    void output(char a[], int& number_used)
    {
    	using namespace std;
    	cout << "The new sentence without the repeated letters is:\n";
    		for (int i = 0; i < number_used; i++)
    		{
    			cout << a[i];
    		}		
    	cout << "\nThe size of the new array is "
    	     << number_used
    	     << endl;
    
    
    	     cin.ignore();
    	     cin.get();
    }

    Changed are marked in red. Seems to work as you intended (I hate arrays).
    Last edited by Oldman47; 04-05-2007 at 08:20 PM.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    13
    You guys were fast and helpful, Thank you very much for taking time to assist.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Output an array in a textbox
    By Diablo02 in forum C# Programming
    Replies: 5
    Last Post: 10-18-2007, 03:56 AM
  2. Array output strange
    By swgh in forum C++ Programming
    Replies: 1
    Last Post: 12-09-2006, 05:58 AM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Sorting array output problem
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 02-19-2002, 12:44 PM

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