H@L0_F00 Posted July 28, 2009 Share Posted July 28, 2009 I just got back into learning C and finished a tutorial on Arrays. I'd been thinking about a program like this one, where you can add a bunch of numbers without having to declare a whole bunch of different variables. I knew an array would be how I could do this but I wasn't sure exactly how so when I finished the array tutorial I immediately started visualizing this: #include <stdio.h> int main() { int array[50], numstoadd; int total = 0; printf("How many numbers would you like to add?\n"); while(1){ scanf("%d", &numstoadd); if(numstoadd < 2){ printf("Please use at least two numbers.\n"); continue; } else if(numstoadd > 50){ printf("Fifty number limit, just cuz I said. :)\n"); continue; } else{ int i = 0; printf("Input one number per line.\n"); for( i = 0; i < numstoadd; i++){ scanf("%d", &array[i]); total = total + array[i]; } for( i = 0; i < numstoadd - 1; i++){ printf("%d + ", array[i]); } printf("%d = %d", array[numstoadd - 1], total); fflush(stdin); getchar(); return 0; } } } The thing is, when a char is put into "numstoadd" int the 50 max nums line keeps going until I end it. Also, if a char is put into one of the array elements that is to be added, it jumps straight to the total with a bunch of random numbers. How can I stop something like this from occurring by maybe verifying that the input is an integer or something similar? And, is this a buffer overflow? If not, what is going on in memory (other than "You can't put a char into an int variable" because I already know that)? Thanks in advance. Quote Link to comment Share on other sites More sharing options...
Jason Cooper Posted July 28, 2009 Share Posted July 28, 2009 I have had a quick look, not exactly how I would tackle the job but then as you are learning it is a reasonable way to tackle the task. I have made a few changes, mainly removing the scanf's and replacing them with fgets's followed by atoi to convert the string into and integer. I also changed the structure of your code slightly so that you don't keep running pointless checks (that sort of thing doesn't make a noticable difference at this sort of thing but once you start coding apps that need to process massive amounts of data it will be very noticable). #include <stdio.h> #include <stdlib.h> #define BUFFER_SIZE 50 int main(int argc, char ** argv) { char input[BUFFER_SIZE]; int array[50]; int numstoadd=0; int total = 0; int i; printf("How many numbers would you like to add?\n"); while(!numstoadd) { fgets(input, BUFFER_SIZE, stdin); numstoadd=atoi(input); if(numstoadd < 2) { printf("Please use at least two numbers.\n"); } else if(numstoadd > 50) { printf("Fifty number limit, just cuz I said. :)\n"); } } for( i = 0; i < numstoadd; i++) { printf("\nEnter number => "); fgets(input, BUFFER_SIZE, stdin); array[i]=atoi(input); total = total + array[i]; } for( i = 0; i < numstoadd - 1; i++) { printf("%d + ", array[i]); } printf("%d = %d", array[numstoadd - 1], total); fflush(stdin); getchar(); return EXIT_SUCCESS; } Quote Link to comment Share on other sites More sharing options...
uberprinnyakatux Posted August 15, 2009 Share Posted August 15, 2009 Looks very good, one thing to remember is > is not inclusive so you gotta do >= or <=, but thats me just nit picking. Quote Link to comment Share on other sites More sharing options...
Jason Cooper Posted August 16, 2009 Share Posted August 16, 2009 Looks very good, one thing to remember is > is not inclusive so you gotta do >= or <=, but thats me just nit picking. Arrays in C use an offset for indexing (i.e. the first element is 0). So, for example, when you want to loop through a 50 elements you want your loop to run from 0 to 49 so you usually find that in the for loop you want to compare against one less than the number the user entered. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.