Thread: C++ array input validation.

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    2

    C++ array input validation.

    Hi.

    I have a program that reads in 10 numbers in an array function then prints out some output. I need the input to be able to only input numbers and nothing else (characters etc). Here is the code.

    Code:
    #include<iostream>
    #include<iomanip>
    
    
    using namespace std;
    
    void maximum ( float [], float, float&, float&, int&, int& );	//Function Prototype
    
    int main()
    {
    
    	const arraysize = 10;
    	float num[arraysize], small, big;		//Declaration
    	int p, p1;
    	
    
    	for ( int i = 0; i < 10; i++ ){
    
    		cout << endl << "Enter number  " << i + 1 << "  :\t";
    		cin >> num[i];		//Array input
    
    	}
    
    	
    		
    
    	maximum ( num, arraysize, small, big, p, p1 );	//Function call
    
    	cout << endl << "Smallest number" << setw(5) << ":" << small << endl;
    	cout << endl << "Position is" << setw(9) << ":" << p  << endl;
    	cout << endl << "Biggest number" << setw(5) << ":" << big << endl;
    	cout << endl << "Position is" << setw(8) << ":" << p1 << endl;
    	
    
    	return 0;
    }
    
    void maximum (float a[], float size, float& small, float& big, int& position, int& position1){
    
     big = a[0];
     small = a[1];
    
    	for ( int j = 0; j < size; j++ ){	//Conditions
    		
    
    		if ( a[j] <= small ){
    
    			small = a[j];
    			position = j + 1;
    			
    
    		}
    
    		if ( a[j] >= big ){
    
    			big = a[j];
    			position1 = j + 1;
    
    		}
    	}
    
    			
    }
    Can u guys help me validate the input so that only numbers are allowed. Or at least let me know where i can find out about it as my C++ knowledge sucks lol.

  2. #2
    Registered User
    Join Date
    Mar 2008
    Posts
    2
    Anybody?

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Patience. On weekends not everyone is that active.
    For your validation problem, the typical easy way is to read a string, then use a stringstream to convert to appropriate type.
    I'm no expert on stringstreams, however. They can be found in the header sstream (named stringstream).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. trying to read input into an array
    By trprince in forum C Programming
    Replies: 16
    Last Post: 11-17-2007, 06:20 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Input Validation Question
    By zackboll in forum C Programming
    Replies: 14
    Last Post: 10-12-2004, 12:05 AM
  5. input validation - new to programming
    By bigzeppelin2k in forum C Programming
    Replies: 2
    Last Post: 10-31-2003, 06:44 PM