-
checking multiple inputs
is there a way i can have a series of inputs that are storing numbers into an array, but make it so if they type something other than a number like finish or done it will end and goto a function, im pretty sure i can do this but can i make it so i dont have to write an if statement checking for something else every time??
-
Code:
#include<iostream> //provides cin & cout
#include<cctype> //provides isdigit()
#include<conio.h> //provides clrcsr()
using namespace std;
int main()
{
int number_array[10];
int temp;
for(int j = 0; j < 10; j++)
{
clrscr();
cout << "\n\nEnter an integer type number to be added to the array: ";
cin >> temp;
if(isdigit(temp))
number_array[j] = temp;
else
{
function_call(); //replace this with the function call of your choice
break;
}
}
return 0;
}
In this example, the isdigit( ) function (part of the <cctype> library) will return true if the user input is a number.. if the user input is something other than a number, then isdigit( ) will return false.. and the IF evaluation will short circuit and resolve to ELSE.
I haven't tried this code.. but this is something I would start out with. ;)
Hope this helps :)
-
Using a little stl can go a long way with things like this.
Code:
template<class Type>
std::istream & get_until(std::vector<Type> &v, const std::string &term=";",std::istream &is=std::cin) {
Type element;
std::string token;
std::vector<Type> tmp;
do {
while(is >> element) {tmp.push_back(element);}
if(is.fail()) is.clear();
} while(is >> token && token != term);
if(is) {v.swap(tmp);}
return is;
}
This will work for any type that supports >> It also won't change the vector if it doesn't see the terminating string. It might be too agressive in that it will read until end of file if there is no terminator, and you might want to stop after the first error. Naturally you will need to specialize for a vector of string or char.
-
>#include<conio.h> //provides clrcsr()
Not necessarily. conio is a nonstandard library, and it's contents will vary with each implementation. Why do you need to clear the screen anyway? That seems pretty antisocial to me in most cases.
>cin >> temp;
>if(isdigit(temp))
And this works for you by some miracle? Let's take it bit by bit (yes it's a pun, yes it was intended). cin's >> operator converts values based on the type of the variable you pass to it, in this case int. So cin will fail if anything but a digit is entered and the contents of temp will be indeterminate. Now, if this indeterminate value happens to not be within the range of unsigned char, passing it to isdigit results in undefined behavior.
So, let's come up with a solution and actually test it this time:
Code:
#include <iostream>
#include <cstdlib>
void bad_input()
{
std::cerr<<"Invalid input entered, bailing..."<<std::endl;
std::exit ( EXIT_FAILURE );
}
int main()
{
int vals[10];
for ( int i = 0; i < 10; i++ ) {
if ( !( std::cin>> vals[i] ) )
bad_input();
}
}
This works, but it relies on a failure state and isn't very flexible if you want to perform different operations based on what the invalid input was. Fortunately, we can fix the second part easily by using a more robust function:
Code:
#include <iostream>
#include <cstdlib>
#include <string>
void stop_input ( int vals[], int n )
{
std::string stop;
std::cin.clear(); // Make cin usable again
// Read the extraneous data as a string
// because we don't know how to convert it
if ( std::getline ( std::cin, stop ) ) {
// Valid stop condition
if ( stop == "stop" ) {
std::cout<<"You entered: ";
for ( int i = 0; i < n; i++ )
std::cout<< vals[i] <<' ';
std::cout<<std::endl;
std::exit ( EXIT_SUCCESS );
}
else { // Invalid condition
std::cerr<<"Error: Invalid input"<<std::endl;
std::exit ( EXIT_FAILURE );
}
}
}
int main()
{
int vals[10];
for ( int i = 0; i < 10; i++ ) {
if ( !( std::cin>> vals[i] ) )
stop_input ( vals, i );
}
}
The first part, where you rely on an error condition to direct potentially valid input, is considered by some to be bad style, but cleaning the stream and validating the data is relatively simple, so make your own decision about it.
-
My code was crushed... at the speed of sarcasm. :rolleyes:
Today officially marks the end of my "off the cuff" programming. :)