Thread: ending number array with a word/char

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    4

    Question ending number array with a word/char

    I am confused how to have a user enter up to 20 numbers into an array and then end the input with any word. any suggestions?

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    What have you tried? Post your code (in code tags).

    Can you write a program to read exactly 20 numbers into an array and print them back out?

  3. #3
    Registered User
    Join Date
    Feb 2017
    Posts
    4
    This is what i have so far, but nothing ive tried has worked. Like, the program will exit once i enter a work or letter but i cant count how many numbers the user entered before the word

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main() {
        double numArray[25];
        char wordArray[10];
        int x = 0;
        int y = 0;
    
    
        setvbuf(stdout, NULL, _IONBF, 0);
    
    
        printf("Enter up to 25 positive reals (non-numeric to stop).\n");
        scanf(" %c", &wordArray[y]);
    
    
        for (x = 0; x < 25; x++) {
                scanf("%lf", &(numArray[x]));
                if (numArray[x] == wordArray[y]){
                    break;
                }
            }
        printf("%d values were entered\n", x);

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    I don't see any reason for the setvbuf call or the wordArray array.

    I think the insight you're looking for here is that scanf returns the number of values it was able to scan, which will be 1 in your case if it was able to read a number. So something like the following might work for you:
    Code:
    if (scanf("%lf", &numArray[x]) != 1)
        break;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Get word from char array
    By Aeoskype in forum C++ Programming
    Replies: 2
    Last Post: 01-28-2015, 11:58 AM
  2. Char array with a phrase to char word
    By TheOneID in forum C Programming
    Replies: 5
    Last Post: 11-28-2013, 07:49 PM
  3. Replies: 6
    Last Post: 07-15-2008, 02:27 PM
  4. Getting a 2 byte number from an array of char's
    By kzar in forum C Programming
    Replies: 20
    Last Post: 04-10-2005, 10:18 PM

Tags for this Thread