I can't seem to process the input correctly. This program here works fine, but I need to add the ability to be able to capture either a single INT variable, or a CHAR then an INT, and I'm not sure how to do that. Here's a sample program of what I'm trying to do.
Code:
#include <iostream>
using namespace std;
int get_input( )
{
char input[30];
char *end_ptr;
int var;
for ( ; ; )
{
fflush(stdout);
fgets(input, sizeof input, stdin);
errno = 0;
var = strtol(input, &end_ptr, 0);
if ( end_ptr == input )
cout << "Not a valid numeric input." << endl;
else if ( input[0] == '\n' || ERANGE == errno )
cout << "Not a valid number." << endl;
else
break;
}
return var;
}
int main( )
{
int x, y;
int MyTemp = 0;
int MyValue[10] = {};
for ( x = 1; x <= 10; x++ )
{
system( "cls" );
cout << "Please enter the Temperature." << endl;
cout << "Then enter 10 more Values." << endl << endl;
cout << " TEMP: " << MyTemp << endl;
for ( y = 1; y <= 10; y++ )
cout << y << ": " << MyValue[y] << endl;
if ( MyTemp == 0 ) // Temp cannot equal 0
{
cout << endl << "TEMP: ";
MyTemp = get_input();
--x;
continue;
}
cout << endl << x << ": ";
MyValue[x] = get_input();
}
return EXIT_SUCCESS;
}
You are first asked for a temperature, then 10 values. The part I cannot figure out is, in the get_input function I need a way to analyze the incoming input and see if it is just a numeric value, if so, "return var" just like normal so it can be entered in. But I need two commands that can be entered at any time, "Temp ###". Example...
Please enter the Temperature.
Then enter 10 more Values.
TEMP: 100
1: 1056
2: 2010
3: 1021
4: 4019
5: 0
6: 0
7: 0
8: 0
9: 0
10: 0
5: temp 109
When you at entering the 5th value, you can go back, change the value of the temperature, and continue along like nothing ever happened. I've tried a few different examples, but most ended up looking something like
cin >> variable1 >> variable2
And that doesn't work very well when for the most part you are entering single numbers. Any help out there please!