How do I validate input for the array to only accept integers?
#include <iostream>
#include <iomanip>
#include <string>
#include <string>
using namespace std;
int main()
{
int TheNumbers[10];// An array of 10 indexes
int i;
for (i=0; i<10; i ) // accepts input 10 times
{
cout << "Enter a number: ";
cin >> TheNumbers[i];
}
for (i=9; i>=0; i--) // returns inputs in reverse order
{
cout << TheNumbers[i] << endl;
}
// ERROR MSSG: While input is not an integer
while(!(cin >> TheNumbers[10]))
{
cout << "**Inncorect input** \n" << "Please input a number: \n";
cin.clear();
cin.ignore(50, '\n');
}
return 0;
}
Enter a number: Enter a number: Enter a number: Enter a number: Enter a number: Enter a number: Enter a number: Enter a number: Enter a number: 32765
-882742640
22081
1189945728
0
0
22081
1189946384
32744
0
CodePudding user response:
You could try adding another nested loop:
static const int MAXIMUM_NUMBERS = 10;
//...
for (int i = 0; i < MAXIMUM_NUMBERS; i)
{
std::cout << "Enter a number: ";
while (!(cin >> number[i]))
{
std::cout << "Invalid number. Try again.\n";
std::cout << "Enter a number: ";
std::cin.clear(); // Clear the error.
std::cin.ignore(10000, '\n');
}
}
This is a simple example, there are other methods.
CodePudding user response:
The solution Thomas Mathew recommended works very well! Creating the MaxNumbers variable to use as a data type comparison, rather than trying to validate a variable to itself.
#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
using namespace std;
int main()
{
int TheNumbers[10];// An array of 10 indexes
static const int MaxNumbers = 10;
int i;
for (i=0; i< MaxNumbers; i) // accepts input 10 times
{
cout << "Enter a number: ";
while (!(cin >> TheNumbers[i]))
{
cout << "** INNCORECT INPUT*** \n" << "Enter a number: ";
cin.clear();
cin.ignore(50, '\n');
}
}
for (i=9; i>=0; i--)
{
cout << TheNumbers[i] << endl;
}
return 0;
}
CodePudding user response:
An option would be:
- Use
std::getline
to read fromstd::cin
into astd::string
. - Use
std::from_chars
to check if the string contains a valid integer.
- If it does, update the count of valid integers read, and carry on.
- If it doesn't, you can optionally tell the users they gave you an invalid input.
Do this in a loop until you have read the amount of numbers you want.
#include <charconv> // from_chars
#include <iostream> // cin, cout
#include <string> // getline
#include <system_error> // errc
int main() {
int TheNumbers[3]; // An array of 3 indexes
for (int i{}; i < 3;) { // accepts input 3 times
std::cout << "Enter a number: ";
std::string number{};
std::getline(std::cin, number);
int n{};
auto [ptr, ec] = std::from_chars(number.data(), number.data() number.size(), n);
if (ec != std::errc{} or ptr != number.data() number.size()) {
std::cout << "Error: invalid input\n";
} else {
std::cout << "OK: read " << n << "\n";
TheNumbers[i ] = n;
}
}
std::cout << "---\n";
for (int i{2}; i >= 0; --i) { // returns inputs in reverse order
std::cout << TheNumbers[i] << "\n";
}
}
// Inputs:
// blah
// 1
// foo99
// 2
// 3.1415
// 4
//
// Outputs:
// Enter a number: Error: invalid input
// Enter a number: OK: read 1
// Enter a number: Error: invalid input
// Enter a number: OK: read 2
// Enter a number: Error: invalid input
// Enter a number: OK: read 4
// ---
// 4
// 2
// 1