Thread: newbie question regarding user input

  1. #1
    Registered User cantore's Avatar
    Join Date
    Feb 2006
    Posts
    8

    newbie question regarding user input

    Hi all, I'm writing a program to calculate the average of 10 integer numbers (which is easy). I have to use an array to store all 10 integers (easy again) but the issue that I'm having is that if the user enters any letter the program should calculate the average at that point. In other words if the user enters 5 integers and then enters a letter the program should calculate the average of those 5 integers and end.

    What kind of "if" statement should I write? The array that holds the user's input is int values[10]. I'm trying to use this if statement but I get garbage output:

    if (values[i]=='x') {store the value of i and proceed to find the average}

    And that's after using a loop with index i. Again, the issue Im having is telling the computer to stop after the user enters a letter.

    Any help would be appreciated, thanks.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Check the return value of scanf().

    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    5
    read up on functions like issalpha() and then implement in an if statement.

  4. #4
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by cantore
    What kind of "if" statement should I write? The array that holds the user's input is int values[10]. I'm trying to use this if statement but I get garbage output:
    Code:
    if (values[i]=='x') {store the value of i and proceed to find the average}
    If you are reading in values as ints, you cannot expect to handle a character inputted. scanf will just stop at that point, leaving it on the stream for another call, because it can't convert it in the manner that you requested.

  5. #5
    Registered User cantore's Avatar
    Join Date
    Feb 2006
    Posts
    8
    After reading some of the stuff in that link and the suggestions here I got it to work. Thanks guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-03-2008, 09:07 PM
  2. User determined input question
    By lyoncourt in forum C Programming
    Replies: 8
    Last Post: 09-30-2007, 06:10 PM
  3. String input reliability question
    By WinterMute in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2007, 11:58 PM
  4. Newbie Help: Currency Converter
    By Ashfury in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 01:21 PM
  5. File input question from a newbie
    By Dylancougar in forum C Programming
    Replies: 9
    Last Post: 10-20-2005, 06:14 PM