Why is my program looping too many times?

Abushawish

I'm a beginner in C and trying to create a program and having a problem with my main function.

Problem:

  1. After asking how many integers they'd like to enter, for example: 4 numbers, the loop goes on 5 times essentially taking in 5 numbers. It also only prints "Next: " after the 2nd number.

  2. In my while loop, which I put for error checking, after the user puts a valid method, example: enters 1, it will print out that it's an "invalid choice" and re-asks again only once more.

Code:

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

int main() {
  StackType stk;
  StackType *stkPtr = &stk;

  //Will be used to check whether to use recursive or iterative
  int method = 0;
  int sum;
  int *sumPnt = &sum;

  //Will be used to create array for amount of ints:
  int numOfIntegers;

  //Array of ints:
  int *userInts;
  printf("How many integers would you like to enter? "); 
  scanf("%d", &numOfIntegers);
  userInts = (int*)calloc(numOfIntegers, sizeof(int)); //Create the array

  printf("Please enter %d numbers: \n", numOfIntegers);
  int i;
  for (i = 0; i < numOfIntegers; i++) {
    scanf("%d\n", &userInts[i]);
    printf("Next:");
  }

  while(1) {
    printf("Would you like to used iterative or recursive to sum?\n");
    printf("Enter 1 for iterative or 2 for recursive: ");
    scanf("%d\n", &method); 
    if (method == 1) {
      //found in loop.c
      sumIterative(stkPtr, numOfIntegers, userInts, sumPnt);
      break;
    } else if (method == 2) {
      //Found in loop.c
      sumRecursive(stkPtr, numOfIntegers, userInts, sumPnt);
      break;
    } else {
      printf("Invalid choice. Repeating... \n");
        continue;
    }
  } 

  printf("Your sum is: %d", *sumPnt);
    return 0;
}
Rikayan Bandyopadhyay

Replace scanf("%d\n", &userInts[i]); with scanf("%d", &userInts[i]);

See this about entering a nonwhitespace character in format specifier in scanf.

It says:

Any character that is not either a whitespace character (blank, newline or tab) or part of a format specifier (which begin with a % character) causes the function to read the next character from the stream, compare it to this non-whitespace character and if it matches, it is discarded and the function continues with the next character of format. If the character does not match, the function fails, returning and leaving subsequent characters of the stream unread.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Why is my program slow when looping over exactly 8192 elements?

From Dev

Elasticsearch: Why is my query returning too many results?

From Dev

Why did my program call the copy constructor several times?

From Dev

OnControllerColliderHit triggering too many times

From Dev

When I swap keys using SetWindowsHookEx WH_KEYBOARD_LL, why does my program get into a cycle of too many keyboard input events?

From Dev

Why does my LabView while loop appear to execute one too many times?

From Dev

Why might I be getting "task completion callback called too many times" in gulp?

From Dev

Why does my program accept one integer too many and input one too few?

From Dev

Why is the file utility telling me there are "too many program headers"?

From Dev

Why is my javascript not looping

From Dev

Why is my controller function being called so many times?

From Dev

Why is my didBeginContact function detecting way too many collisions?

From Dev

Swift: Why the CustomStringConvertible description is run too many times in this case?

From Dev

My bubble sort seems to be running too many times, or running backwards

From Dev

I am confused why my program is not looping and printing

From Dev

php mysqli looping too many results

From Dev

Why did my program call the copy constructor several times?

From Dev

Program outputting Too many lines?

From Dev

My program is asking too many times for input. C while loop

From Dev

Why does my python socket in a thread not always close properly (i.e. if i run the program many times in a row)

From Dev

Delete method being called too many times. Why?

From Dev

Why my haskell program is too slow?

From Dev

Process evaluated too many times

From Dev

Timer is executing too many times

From Dev

Why is Iteration looping too many times?

From Dev

Why does my virtual destructor executes these many times?

From Dev

redirect too many times laravel

From Dev

React+Javascript:-Component will update is getting called too many times causing my application to crash

From Dev

Why does my MessageBox show so many times?

Related Related

  1. 1

    Why is my program slow when looping over exactly 8192 elements?

  2. 2

    Elasticsearch: Why is my query returning too many results?

  3. 3

    Why did my program call the copy constructor several times?

  4. 4

    OnControllerColliderHit triggering too many times

  5. 5

    When I swap keys using SetWindowsHookEx WH_KEYBOARD_LL, why does my program get into a cycle of too many keyboard input events?

  6. 6

    Why does my LabView while loop appear to execute one too many times?

  7. 7

    Why might I be getting "task completion callback called too many times" in gulp?

  8. 8

    Why does my program accept one integer too many and input one too few?

  9. 9

    Why is the file utility telling me there are "too many program headers"?

  10. 10

    Why is my javascript not looping

  11. 11

    Why is my controller function being called so many times?

  12. 12

    Why is my didBeginContact function detecting way too many collisions?

  13. 13

    Swift: Why the CustomStringConvertible description is run too many times in this case?

  14. 14

    My bubble sort seems to be running too many times, or running backwards

  15. 15

    I am confused why my program is not looping and printing

  16. 16

    php mysqli looping too many results

  17. 17

    Why did my program call the copy constructor several times?

  18. 18

    Program outputting Too many lines?

  19. 19

    My program is asking too many times for input. C while loop

  20. 20

    Why does my python socket in a thread not always close properly (i.e. if i run the program many times in a row)

  21. 21

    Delete method being called too many times. Why?

  22. 22

    Why my haskell program is too slow?

  23. 23

    Process evaluated too many times

  24. 24

    Timer is executing too many times

  25. 25

    Why is Iteration looping too many times?

  26. 26

    Why does my virtual destructor executes these many times?

  27. 27

    redirect too many times laravel

  28. 28

    React+Javascript:-Component will update is getting called too many times causing my application to crash

  29. 29

    Why does my MessageBox show so many times?

HotTag

Archive