GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 26-Mar-2005, 12:11
hopesfall
 
Posts: n/a

Enter key in basic c program


Sorry if this question has been asked before but I couldnt find anything on it using google or the GID search.

Quick question that can probably be answered easily.

I have a program that I'm doing for school and the only problem I'm having is with the Enter key.
What I want to do is: using a loop, calculate the average number of a certain amount of numbers given by the user.
All that is fine and working, but I'm also supposed to handle the situation if the user doesnt enter any numbers and just presses Enter. The program should then display "No input, quitting..." and then quit.

I cant figure out what C recognizes as the enter key. Basicly I want something like this

if (x == Enter Key)
{
printf ("No input, quitting");
delay (2000); exit (EXIT_SUCCESS);
}

Any help will be greatly appreciated.
  #2  
Old 26-Mar-2005, 13:20
Dr. Evil Dr. Evil is offline
Member
 
Join Date: Oct 2004
Location: Netherlands
Posts: 120
Dr. Evil will become famous soon enough
Newline = 10 = 0xA.
  #3  
Old 26-Mar-2005, 16:09
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by hopesfall
Sorry if this question has been asked before but I couldnt find anything on it using google or the GID search.

Quick question that can probably be answered easily.

I have a program that I'm doing for school and the only problem I'm having is with the Enter key.
What I want to do is: using a loop, calculate the average number of a certain amount of numbers given by the user.
All that is fine and working, but I'm also supposed to handle the situation if the user doesnt enter any numbers and just presses Enter. The program should then display "No input, quitting..." and then quit.

I cant figure out what C recognizes as the enter key. Basicly I want something like this

if (x == Enter Key)
{
printf ("No input, quitting");
delay (2000); exit (EXIT_SUCCESS);
}

Any help will be greatly appreciated.


I would probably use fgets() in a loop to read the entire line. If the only thing is a newline, then you know to quit. Otherwise, you get whatever information you expect to see on the line (combinations of strtok(), scanf(), strtol() or whatever you want.)

CPP / C++ / C Code:
#include <stdio.h>

int main()
{

  int x;
  int count = 0;

  char buffer[BUFSIZ];

  printf("Enter a decimal integer: ");
  while (fgets(buffer, BUFSIZ, stdin)) {/* exit upon EOF */
    if (buffer[0] == '\n') {
      break;
    }
    if (sscanf(buffer, "%d", &x) != 1) { /* guard against non-decimal digit chars */
      printf("Invalid decimal number; try again.\n");
      continue;
    }
    /* do something useful with x here; I just print it out */
    printf("            You entered  %d\n\n", x);
    count++;
    printf("Enter a decimal integer: ");
  }

  printf("Total number of valid entries = %d\n", count);
  return 0;
}

Regards,

Dave
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 4) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
RSA program fwongmc C Programming Language 11 08-Nov-2004 22:15
C++ help Grid C++ Forum 7 25-Oct-2004 22:40
Anyone can write a program code for this??? chriskan76 C Programming Language 1 19-Oct-2004 20:25
Array..... chriskan76 C Programming Language 4 18-Oct-2004 14:19

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:55.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.