Thread: array problem

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    44

    array problem

    Have two problems with this program:
    1. The user is not supposed to enter more than 20 numbers, but the program wont move on unless 20 numbers are enteres.

    2. How would i put the bubble sort part of the program into a function to be called. I tried and i kept getting the expression must have pointer-to-object type.

    Code:
    #include<iostream>
    using namespace std;
    
    
    int main()
    {
    
    
        int temp;
        int num[20];
            cout<<"Pleas enter a series of numbers(no more than 20): "<<endl;
            for(int i=0; i<20; i++)
        {
            cin>>num[i]; 
        }
        while(!cin){//says while the input is not equal to an interger go to next line
                    cout<<"Error, Please enter a series of numbers: "<<endl;
                    for(int i=0; i<20; i++)
                    {
                        cin.clear();//clears the input from num[i]
                        cin.ignore(256,'\n');//flushes the input stream
                        cin>>num[i];
                }
        }
        cout<<endl; 
        cout<<"Orignally entered array is: "<<endl;
    
    
        for(int j=0; j<20; j++)
        {
            cout<<num[j];
            cout<<endl; 
        } 
        cout<<endl;
        for(int i=0; i<19; i++)
        {
            for(int j=0; j<19; j++)
            {
                if(num[j]>num[j+1])
                {
                    temp=num[j];
                    num[j]=num[j+1];
                    num[j+1]=temp; 
                }
    } 
    } 
    cout<<"Sorted Array is: "<<endl;
    for(int i=0; i<20; i++)
    {
        cout<<num[i]<<endl; 
    }
    system("pause");
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,539
    1. The user is not supposed to enter more than 20 numbers, but the program wont move on unless 20 numbers are enteres.
    It looks to me that the program will ask for more than 20 numbers, because of the two loops.

    2. How would i put the bubble sort part of the program into a function to be called. I tried and i kept getting the expression must have pointer-to-object type.
    Post the code showing what you've tried.


    Jim

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    44
    ok i got the function to work. still not sure why if i only put in 10 numbers and hit enter it wont run the program. I have to enter 10 more number before it will sort them.
    Code:
    #include<iostream>using namespace std;
    int bubbleSort(int);
    int main()
    {
    
    
    	int temp=0;
    	int num[20];
    		cout<<"Pleas enter a series of numbers(no more than 20): "<<endl;
    		for(int i=0; i<20; i++)
    	{
    		cin>>num[i]; 
    	}
    	while(!cin){//says while the input is not equal to an interger go to next line
    				cout<<"Error, Please enter a series of numbers: "<<endl;
    				for(int i=0; i<20; i++)
    				{
    					cin.clear();//clears the input from num[i]
    					cin.ignore(256,'\n');//flushes the input stream
    					cin>>num[i];
    			}
    	}
    	cout<<endl; 
    	cout<<"Orignally entered array is: "<<endl;
    
    
    	for(int j=0; j<20; j++)
    	{
    		cout<<num[j];
    		cout<<endl; 
    	} 
    	cout<<endl;
    	bubbleSort(temp);
    	
    cout<<"Sorted Array is: "<<endl;
    for(int i=0; i<20; i++)
    {
    	cout<<num[i]<<endl; 
    }
    system("pause");
    }
    int bubbleSort(int temp)
    {
    	int num[20];
    	for(int i=0; i<19; i++)
    	{
    		for(int j=0; j<19; j++)
    		{
    			if(num[j]>num[j+1])
    			{
    				temp=num[j];
    				num[j]=num[j+1];
    				num[j+1]=temp; 
    			}
    } 
    } 
    	return temp;
    }

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,364
    How do you expect the user to indicate that they are done entering numbers? Do they just hit enter without typing anything? Do they type some non-digit and hit enter? You're just gathering numbers in a loop, and your loop is set to run 20 times no matter what. Nothing else breaks the loop. The cin >> call will sit and wait for the user to enter a number, so if the user enters only 10 numbers, then cin >> will just wait for a non-whitespace character.

  5. #5
    Registered User
    Join Date
    Oct 2013
    Posts
    44
    Quote Originally Posted by Daved View Post
    How do you expect the user to indicate that they are done entering numbers? Do they just hit enter without typing anything? Do they type some non-digit and hit enter? You're just gathering numbers in a loop, and your loop is set to run 20 times no matter what. Nothing else breaks the loop. The cin >> call will sit and wait for the user to enter a number, so if the user enters only 10 numbers, then cin >> will just wait for a non-whitespace character.


    Yes. It helps if you read the directions right. Evidently I was supposed to ask the user how many numbers they want to enter. Definitely makes more sense to do it that way.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,364
    That will definitely help.

    One other word of advice. You currently check (!cin) after your (first) for loop is done. That will only check the state of cin after you've already tried to read in all the numbers. It would probably be more help if you checked it for each number you read in.

  7. #7
    Registered User
    Join Date
    Oct 2013
    Posts
    44
    OK I have the program working just fine, even figured out how to call the bubble sort functin ( that took a while). What is the best way to test each input of the array individually to see if it's an integer?

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,539
    What is the best way to test each input of the array individually to see if it's an integer?
    Since your array is an array of int, anything in that array is an int. If the user tries to enter a character other than a digit the stream will enter an error state.

    Jim

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,364
    >> What is the best way to test each input of the array individually to see if it's an integer?

    Checking for !cin is what you want, you just want to do it during or immediately after each time you read into the array, not after the for loop finishes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-04-2014, 09:51 AM
  2. Replies: 2
    Last Post: 01-06-2013, 07:49 AM
  3. problem initializing a double array for large array
    By gkkmath in forum C Programming
    Replies: 4
    Last Post: 08-25-2010, 08:26 PM
  4. Problem converting from char array to int array.
    By TheUmer in forum C Programming
    Replies: 11
    Last Post: 03-26-2010, 11:48 AM
  5. simple array of char array problem
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2006, 12:04 PM