Home > Back-end >  Variable that keeps track of integers in an array
Variable that keeps track of integers in an array

Time:09-22

Part of my assignment is to write a function that takes 10 integers and if the user hits any other keyboard(letters) it goes back to main menu. The integers that the user entered should be available for other functions too(so I guess that I need to create a variable in main that keeps track of integers).

For example if the user decides to only enter 6 integers and then hit "c" the program should go back to main menu and know that there are 6 integers stored and next time user tries to enter more integers only allow 4 more.

I've been struggling for two days and I know the code is bad, but here it comes. My function for entering integers.

int enter(int a[], int n){
    int i;
    for( i=1; i<n;i  ){
    printf("Enter measurement #%d ", i);
    if(scanf(" %d", &a[i]) == 1){
    }
    else{
        printf("Exit!"); 
        break;
    }   
} main();    

}

My variables in main.

int nrOfMeasurements;
int measurements[10];

And my function in menu. If user hit "e", user can enter integers. This if statements is nested inside a while-loop.

if(ch == 'e'){
    enter(measurements,10);  
    nrOfMeasurements = enter(measurements,nrOfMeasurements);
}

CodePudding user response:

Non-numeric input not consumed

When a non-int text is entered, code fails to consume that input, leaving for later code to consume. This is likely an issue.

Return value save, but not used

nrOfMeasurements after rOfMeasurements = enter(...) apparently unused as OP reports "it continues to fill the array with random numbers".

EOF not detected

End-of-file nor input error detected.

Off by 1

for( i=1; i<n;i ) will iterate n-1 times, not n times.

Code fails to return a value

int enter(int a[], int n) should have a return someplace.

Unneeded space in scanf(" %d", &a[i])

"%d" consumes leading white-space. " " serves no purpose here.

main() lacks return type

Pedantic: int vs. size_t

Extreme array sizes may exceed the int range.

Save return value

enter(measurements,10); should save return value.


Sample fixed code

#include <stddef.h>
#include <stdio.h>

size_t enter(int a[], size_t n) {
  size_t i;
  for(i=0; i<n; i  ) {
    printf("Enter measurement #%zu ", i   1);
    int match_count = scanf("%d", &a[i]);
    if (match_count == EOF) {
      return (size_t) EOF;
    } 
    if (match_count == 0) {
      // Consume rest of line.
      int ch;
      while ((ch = getchar()) != `'\n'` && ch != EOF) {
        ;
      }
      break;
    }
  }
  return i;
}

#define N 10

int main(void) {
  size_t nrOfMeasurements = 0;
  int measurements[N];

  ...
  if (ch == 'e') {
    nrOfMeasurements = enter(measurements, N);
    if (nrOfMeasurements != (size_t) EOF) {
      for (size_t i = 0; i < N; i  ) {
        printf("%d\n", measurements[i]);
      }
    }

    // Last goal left for OP: 
    // "and next time user tries to enter more integers only allow 4 more."
  }
...
}

  • Better code would use fgets() for all user input.

  • Use strtol() rather than "%d" to well detect overflow of values outside int range.

CodePudding user response:

#include <stdio.h>
#include <stdlib.h>

void view(int a[], int n);
int enter(int a[], int n);

int main (){

int max = 10, numberOfMeasurements;
int measurements[max];
char ch;

while(ch != 'q'){

    printf("\nView(v)Enter(e)Compute(c)RQ: "); 
    scanf(" %c", &ch);

    if(ch == 'v'){
        view(measurements,numberOfMeasurements);
    }
    if(ch == 'e' && numberOfMeasurements != max ){
       numberOfMeasurements = enter(measurements   numberOfMeasurements, max);
    }
    }  return 0;
}

void view(int a[], int n){
    int i; 
    for(i=0;i<n;i  ){
      printf("%d ", a[i]); 
    }     
}

int enter(int a[], int n){

for(int i=0; i<n;i  ){
    printf("Enter measurement #%d: ", i);
    scanf("%d", &a[i]);  
}  
}
  • Related