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 08-Dec-2004, 05:59
linkin linkin is offline
New Member
 
Join Date: Nov 2004
Posts: 6
linkin is on a distinguished road

Problem with alpharithmetical check


I recently made a calculating program in C but in order to improve it i want to make a check on the values that the user enters.It's not the following but this simple code also reflects my problem.
CPP / C++ / C Code:
#include <stdio.h>

int main()
{
	int number=0;
	for(;;)
	{
		printf("Give a number:");
		scanf("%d",&number);
		printf("ok\n");
	}
	return 0;
}
When the user enters a char value in respond such as from 'a' to 'z' the program crashes , and that's what i 'd like to avoid.I need to find a way to print a notifying message and give him a second chance.
I was told to use functios atoi from math.h and fgets
or isalpha from ctype.h , but i don't know how.
Any suggestion would be helpful.Thanks in advance. :-)
Last edited by JdS : 09-Dec-2004 at 16:52. Reason: Please insert [c] & [/c] tags between your example C codes
  #2  
Old 08-Dec-2004, 06:32
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,108
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
Quote:
Originally Posted by absent
I recently made a calculating program in C but in order to improve it i want to make a check on the values that the user enters.It's not the following but this simple code also reflects my problem.
#include <stdio.h>

Hello and Welcome to GIDForums absent. A few things you want to do. READ this thread about the code tags. To do that just make it look like this in your editor.

Quote:
Originally Posted by absent
Code:
CPP / C++ / C Code:
int main()
{
	int number=0;
	for(;;)
	{
		printf("Give a number:");
		scanf("%d",&number);
		printf("ok\n");
	}
	return 0;
}

That will maintain your formatting and use the custom code highlighter that folks here are proud to have at their disposal. It helps you get more responses as well.

Quote:
Originally Posted by absent
When the user enters a char value in respond such as from 'a' to 'z' the program crashes , and that's what i 'd like to avoid.I need to find a way to print a notifying message and give him a second chance.
I was told to use functios atoi from math.h and fgets
or isalpha from ctype.h , but i don't know how.
Any suggestion would be helpful.Thanks in advance. :-)

Here is a link to a post here with some good info (at least I think so) that touches on the topic of input. My last post shows how to use cctype to filter the input before attempting to use it. Last but not least read this thread that talks about scanf along the way but more importantly describes in depth why your problem is occurring.

My example is c++ but I modified most from c code and the headers that are eventually included in my example are the same ones you want to use. There are also some helpful reference links at the bottom of the post.

Hope this was helpful.

Mark
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
  #3  
Old 08-Dec-2004, 07:38
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,648
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 absent
I recently made a calculating program in C but in order to improve it i want to make a check on the values that the user enters.It's not the following but this simple code also reflects my problem.

When the user enters a char value in respond such as from 'a' to 'z' the program crashes , and that's what i 'd like to avoid.I need to find a way to print a notifying message and give him a second chance.

Any suggestion would be helpful.Thanks in advance. :-)

The way preferred by most experienced programmers includes fgets(), followed by sscanf(), atoi() or some such thing. For starters, however it is possible, and maybe even practical, to use scanf(). Here's an example.


If you want to use scanf() for numeric input, you should always test to see if the scan succeded. scanf() returns the number of items that it saw. When scan is looking for a numeric entry, if you enter a non-numeric character, scanf() stops without doing anything else, so the character is still in the input buffer for the next scanf() or getchar() or whatever.

Unlike C++ use of cin>>, scanf() input anomalies are fairly easy to recover from. Try this:

CPP / C++ / C Code:
#include <stdio.h>
#include <ctype.h>
int main()
{
  int number=0;
  printf("\n\nPress ctrl-c to exit the program.\n");
  for(;;)
  {
    printf("\n\n");
    printf("  Give a number: ");
    if (scanf("%d",&number) == 1) { /* always test to see if scanf succeeded!*/
      printf("  You entered %d\n", number);
    }
    else {
      if (feof(stdin)) {/* ctrl-z for Windows, ctrl-d for Linux */
        break;
      }
      number = getchar(); /* eat the bad character */
      else {
        printf("\n***  Illegal input encountered: ");
        if (isprint(number)){
          printf("%c ", number);
        }
        printf("(0x%02x hex). ***\n", number);
      }
    }
  }
  return 0;
}




Try it with inputs like

123

12a

abc

etc.
Regards,

Dave
  #4  
Old 09-Dec-2004, 08:15
linkin linkin is offline
New Member
 
Join Date: Nov 2004
Posts: 6
linkin is on a distinguished road
i liked your idea davekw7x
but your code is causing a segment
i mean it doesn't give the user the chance to answer again.
I didn't understand the part with the
printf("(0x%02x hex). ***\n", number);
but i hadn't thought using scanf inside if
thanks anyway.
  #5  
Old 09-Dec-2004, 08:56
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,648
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 absent
i liked your idea davekw7x
but your code is causing a segment
i mean it doesn't give the user the chance to answer again.
I didn't understand the part with the
printf("(0x%02x hex). ***\n", number);
but i hadn't thought using scanf inside if
thanks anyway.

Oh, man!!! I try really hard to post code that works. There was a serious error that the compiler should have caught. Here's something better (I hope).
CPP / C++ / C Code:
#include <stdio.h>
#include <ctype.h>
int main()
{
  int number=0;
  printf("\n\nPress ctrl-c to exit the program.\n");
  for(;;)
  {
    printf("\n\n");
    printf("  Give a number: ");
    if (scanf("%d",&number) == 1) { /* always test to see if scanf succeeded!*/
      printf("  You entered %d\n", number);
    }
    else {
      if (feof(stdin)) {/* ctrl-z for Windows, ctrl-d for Linux */
        break;
      }
      else {
        number = getchar(); /* eat the bad character */
        printf("\n***  Illegal input encountered: ");
        if (isprint(number)){
          printf("%c ", number);
        }
        printf("(0x%02x hex). ***\n", number);
      }
    }
  }
  return 0;
}

Somehow, in the code that I posted, the "eat the bad character" line sneaked to an illegal spot (between the if{} and else{} stuff.)

Regards,

Dave
  #6  
Old 09-Dec-2004, 09:04
linkin linkin is offline
New Member
 
Join Date: Nov 2004
Posts: 6
linkin is on a distinguished road
That's it man, thanks!!
  #7  
Old 09-Dec-2004, 09:28
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,648
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 absent
I didn't understand the part with the
printf("(0x%02x hex). ***\n", number);


For debug purposes I wanted to print out the illegal character. If the character was printable, I printed it as %c. If the user entered something not printable, say Ctrl-A, I printed the hex value of the character (0x01 in this case).

Regards,

Dave
 
 

Recent GIDBlogLast Week of IA Training 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
CD burner will not burn, new problem sdshaman Computer Hardware Forum 4 06-Feb-2008 23:17
Gallery Thumbnail problem Darksat MySQL / PHP Forum 2 25-Oct-2004 08:21
Debugging problem.... Max C Programming Language 4 04-Aug-2004 04:59
C I/O problem kelly80 C Programming Language 4 27-Apr-2004 20:15
Another FX 5600 problem (but with details that might shed light on this) BobDaDuck Computer Hardware Forum 2 16-Apr-2004 07:53

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

All times are GMT -6. The time now is 01:08.


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