Recursive
2012-10-25 21:26:30
So what I need it to do is to request input to fill the vector with an already specified amount of input. It is supposed to do something even when the user presses the enter key (it is not supposed to ignore these). Then it is supposed to keep looping until all valid characters have been entered. We are only allowed to use the following functions to accomplish getting and reading of input as well as output:
fgets(3), sscanf(3), scanf(3), fscanf(3), fprintf(3), printf(3), cin >>, if you are not adept with linux and shell programming, the numbers in bracket means the man[ual] page where you can find how these functions work.
My problem is that I can't seem to get it to respond to the pressing of the enter key. I already tried using printf to find out what value is returned from the enter key; and on Macs, it is 10. I haven't tried yet with linux or windows. But even that is still not making it produce the message I want to display when such an occurrence occurs.
The vector is receiving integers but fscanf is converting it to float/double (which one is it?). Here is the declaration of Vector type:
p.s. I cheated and used fgetc(3), but I am no supposed to use that
fgets(3), sscanf(3), scanf(3), fscanf(3), fprintf(3), printf(3), cin >>, if you are not adept with linux and shell programming, the numbers in bracket means the man[ual] page where you can find how these functions work.
My problem is that I can't seem to get it to respond to the pressing of the enter key. I already tried using printf to find out what value is returned from the enter key; and on Macs, it is 10. I haven't tried yet with linux or windows. But even that is still not making it produce the message I want to display when such an occurrence occurs.
The vector is receiving integers but fscanf is converting it to float/double (which one is it?). Here is the declaration of Vector type:
#include <cstdlib>
#include <cstdio>
#include <cassert>
#include <cctype>
#include <cstring>
using namespace std;
typedef float Elem;
struct Vector
{
Elem* new_Elem;
int numElements;
};
Vector *alloc_vec(void)
{
Vector* vect = new Vector;
assert(NULL != vect);
return vect;
}
#ifdef TESTING
int main(void)
{
int buffer;
int n_el;
Vector* myVec = alloc_vec();
scanf("%d", &myVec->numElements);
myVec->new_Elem = new float[myVec->numElements];
assert(0 <= myVec->numElements && (myVec->numElements % 1) == 0 && myVec->numElements < 65535);
for(int e = 0; e < myVec->numElements; e++)
{
printf("vector element %d: ", e + 1);
n_el = fgetc(stdin);
printf("%d", n_el);
buffer = fscanf(stdin, "%f", &myVec->new_Elem[e]);
/*if (buffer <= 0)
{
e--;
fprintf(stderr, "no value specified\n");
}*/
if (feof(stdin))
{
fprintf(stderr, "\nproblem reading input values\n");
exit(EXIT_FAILURE);
}
}
#endif
p.s. I cheated and used fgetc(3), but I am no supposed to use that