I am having a hard time find example in C (not c ++) in populating arrays.
I need example to refrence to because i have never done it before. I want to error check the inputs before i put it in the array.
I am having a hard time find example in C (not c ++) in populating arrays.
I need example to refrence to because i have never done it before. I want to error check the inputs before i put it in the array.
Perhaps in a for loop, you could have the input tested, either in a block of code, or in a function, before assigning the value into your array, something like:
Where getInput() receives input from the user or file, and okInput() tests the variable to see if it's OK to go into the array at index i, returning zero if the input is bad. An actual zero could be a good variable, and okInput() should return the value 1 for that input, if in fact, a zero would be in the range of a good input value.Code:for(i = 0; i < MaxArrayInputItems; i++) { variable = getInput(); if(okInput(variable)) array[i] = variable; }
Don't confuse the return value from okInput, with the value that okInput is testing.
You may need to hit the books or some on-line C tutorials, in order to make this program.
In any case, it will be a good exercise to learn from.
Try this
Its a good tutorial on arrays in c
C Programming - Arrays
Here's an example:
This may seem a bit complex, but the form should be easy to follow. The verification is slightly lame since strtof will go to some length to take a number, eg, "1324mdsf" will be 1324 and "3,5" will be 3, but this at least means you will end up with a valid number submitted to the array. And you get an example of how to error check the strtof/l/d() functions, which are useful in verifying input this way.Code:#include <stdio.h> #include <errno.h> #include <stdlib.h> /* compile -std=c99 */ float getInput() { char input[32], *tptr; float val; printf("Enter any kind of number: "); fgets(input,32,stdin); errno = 0; val = strtof(input,&tptr); if ((errno) || ((val == 0) && (tptr == input))) { puts("That input is not valid! Try again..."); val = getInput(); } return val; } int main() { float array[6]; int i; for (i=0; i<6; i++) array[i] = getInput(); for (i=0; i<6; i++) printf("%f\n",array[i]); return 0; }
NB. strtof is a c99 function and needs to be compiled with that in mind!
Last edited by MK27; 08-04-2009 at 09:54 AM.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
thanks Adak i think i got what you said there
roaan thanks for the link i will read it
MK27 thanks for the example but theres some stuff in there i haven't learn yet floats and strtof yet so i didnt understand it fully
i want to have so that if the user enters a number but if the number doesnt meet the requirements it doesnt store it in the array
i wrote some code quickly just now
i am not sure if i am doing it right
is is possible to do to this in one function
Code:int getInput(int x); int okInput(int y); main() { int variable, maxarr, usri; maxarr = 10 for (i=0; i < maxarr; i++); variable = getInput(usri); if (okInput (variable)) array[i] = variable } int getInput(int x); { printf("Enter a number"); scanf("%d, &usri"); return x; } int okInput(int y); { if (y <0) break; else return y; }
Last edited by jafa401; 08-04-2009 at 10:45 AM.
If you can write code, you should be able to read code. So, read the code you have posted. Follow its logic, either mentally or on paper. Think up some inputs (both good and bad) and see what will happen when this code runs. If it does what you want, then it's right(*). If it doesn't, it's wrong.
(*)Technically, it could still be wrong, if it fails on some input case you didn't consider. That's why you want to be sure, when you're thinking of test cases, that you be thorough about it.
Yeah, and using scanf() is fine too. But you need to use the return value of scanf to determine whether or not it retrieved a number. Then you can check that, and do what I did to repeat if scanf() failed -- here's a pseudo code version:
About the bit in red: %*c will trap the "trailing newline" which will get left in the input buffer and cause a problem (do it first without %*c and you will understand what the problem is).Code:int getInput () { int value; ask for input r=scanf("%d%*c",&value); if (r != 1) { puts error message; value = getInput(); } return value; }
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
i want it so if the user enters umm a negative number it will not store it in the array it just continues on with out error msg.
thanks MK27 i will play around this that code
Some suggestions:
Code:#include <stdio.h> //add #define maxarr 10 //change maxarr to a define int getInput(void); //88 int okInput(int y); int main() // always int main() and a return at the end { int i, variable; //add i, delete usri int array[maxarr]; //add array[] for (i=0; i < maxarr; i++) { //remove ; add curly brace variable = getInput(); if (okInput (variable)) array[i] = variable; //add ; } printf("\n The array is full.\n"); printf("\n Why not make a function to show it? \n"); printf("\n\t\t\t Press Enter When Ready\n"); i = getchar(); return 0; } //add closing brace /*we could send x to getInput, but we don't need to we're using just the one return value that a function can give us, instead. */ int getInput(void) //remove ; { int x; printf("Enter a number"); scanf("%d, &x"); return x; } int okInput(int y) //remove ; { int valid = 1; int invalid = 0; if (y <0) return invalid; else return valid; //there is no need to return y. main() already has it as "variable" }
I think that okInput would be more readable as:
after all, the extra variables are not necessary, and now it is easy to see that "ok input" means that "the input is greater than or equal to 0".Code:int okInput(int y) { return y >= 0; }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Do you want to use an error msg at all? Anyway, if not, you could just take that part out, and simplify by setting "value" to -1 initially. Then, if scanf() doesn't get a proper number, value will still be -1 and you can use that as the if condition -- so no more need to check scanf's return value"
Code:int value = -1; scanf("%d%*c",&value); if (value < 0) value = getInput(); return value;
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Wow, lots of people doing people's homework today!
It is reasonable to suggest improvements in code after the person has actually posted a reasonable attempt.Originally Posted by nonoob
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)