944,191 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 5711
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 27th, 2005
0

help with getchar

Expand Post »
i just would like to ask for help regarding my code... my problem is this... the user must enter an 80-digit number...if he enters more than 80 digits, the error "\007Input number must not exceed a total of %d digits\n" will appear setting the iserror flag to 1 and breaking the for loop. then if iserror==1, it will execute the line "continue" so that the user may enter a new number... the previous number he entered is no longer valid...

the problem is, after the program prints "Enter number 1: " to take a NEW input from user, the getchar function gets the number immediately after the 80th number which the user previously entered... i wonder how can i be cleared of the previous number the user entered so that when asked again to enter number 1, the getchar will get the char starting from the first digit of the NEW input number.

  1. while(isvalid==0){
  2. iserror=0;
  3. printf("Enter number 1: ");
  4. ch=getchar();
  5. /*check if no input for number 1*/
  6. if(ch=='\n'){
  7. printf("\007Please input an integer up to 80 digits\n");
  8. continue;
  9. }
  10. /*check if input for number 1 exceeds maximum digit count*/
  11. if(ch=='-'){/*signed input number 1*/
  12. signednum1=1;
  13. for(n=0;n<=MAXDIGITCOUNT,ch!='\n';n++){
  14. ch=getchar();
  15. if(n==MAXDIGITCOUNT && ch!='\n'){
  16. printf("\007Input number must not exceed a total of %d digits\n",MAXDIGITCOUNT);
  17. iserror=1;
  18. break;
  19. }
  20. if(n<MAXDIGITCOUNT && ch!='\n'){
  21. num1[n]=ch;
  22. len1=n+1;
  23. }
  24. }
  25. if(iserror==1){
  26. continue;
  27. }
  28. }
  29. if(iserror==1){
  30. isvalid=0;
  31. }
  32. else
  33. isvalid=1;
  34. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zeek is offline Offline
9 posts
since Jul 2005
Jul 27th, 2005
0

Re: help with getchar

Just clear the stream of extra characters. You can use this function:
  1. void discard ( FILE *in )
  2. {
  3. int c;
  4.  
  5. while ( ( c = fgetc ( in ) ) != EOF && c != '\n' )
  6. ;
  7. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 27th, 2005
0

Re: help with getchar

Couldn't you flow control (with an if statement) the entry? Once you have 80 digits, you switch over to the second number so any overage is automatically put into the second number.

Would that work, or am I not understanding the problem correctly?
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005
Jul 27th, 2005
0

Re: help with getchar

actually, i am creating a program to add 2 large-digit numbers up to 80... first you input number 1 (which can be up to 80 digits) next you input number 2 (also can be up to 80 digits)...
when the user enters greater than 80 digits for number 1, the remaining numbers beyond the limit of 80 must be discarded...

i have to prompt the user that he enters more than 80 digits and i have to prompt him again to enter "input number 1:"... wherein in my program it continues on running (getting the remaining digits previously entered because of getchar) and executing "input number 2:" without giving the user the chance to input a new number for number 1... (NOTE: i did not place the code for "input number 2:" in the question)

mr. narue... i am still new in c programming... can you pls explain the code (and where i should to place it) because as i understand it, i have to open a file... i'm sorry sir but i still have little knowledge in C. hope you understand thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zeek is offline Offline
9 posts
since Jul 2005
Jul 27th, 2005
0

Re: help with getchar

Quote originally posted by Narue ...
Just clear the stream of extra characters. You can use this function:
  1. void discard ( FILE *in )
  2. {
  3. int c;
  4.  
  5. while ( ( c = fgetc ( in ) ) != EOF && c != '\n' )
  6. ;
  7. }

DID YOU KNOW THAT:
The c-defined 'stdin' that takes the input directly from the keyboard is compatible with (if not a direct type of) FILE* ?

AND NOW YOU KNOW!

What's going on here? I'll walk you through it.
  1. void discard ( FILE *in ) //1
  2. {
  3. int c;//2
  4.  
  5. while ( ( c = fgetc ( in ) ) != EOF) && c != '\n' ) //3
  6. ;
  7. }

Okay, here goes:
1. The function name and accepted parameters. In this case, you'll want to call:
  1. discard(stdin);
when you want to clear the input stream of excess characters.
You'd call it after you get 80 digits.

2. This is the digit you want to get in for your input.

3. This while loop does everything in the declaration of the while loop.
Its first argument, ((c = fgetc(in)) != EOF), does two things:
First, it calls the fgetc function and returns an integer value, which is stored in the variable 'c'. The parenthesis around
  1. (c=fgetc(in))
are to help both you and the compiler realize that the fgetc function and assignment should be called first.
Second, it compares the value of 'c' to the defined value of the end of file character (abbreviated as EOF). If 'c' has the value of EOF, the while loop exits.
That's what the !=EOF does.

{Note: "in" is the reference the function uses instead of stdin, the keyboard input. It's the same thing, just renamed for the discard function.}

The second argument in the while loop,
  1. c != '\n'
Compares the value of c (taken in the first argument) to the value of the "newline" character. When using stdin, this translates to a press of the enter key.

The two arguments for the while loop are joined by the logical AND operator, &&. That means that as long as both conditions(c can't be the end of file and c can't be a new line) are met, any key press is discarded. Only pressing enter can break the loop.

So, once you've got your 80 digit entries, you call the discard function with stdin as the argument, and anything that's not an enter will be assigned and overwritten without getting into the numbers you want to preserve. Once enter is pressed, the discard function exits, and you can start entering the second 80 digit number.
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005
Jul 27th, 2005
0

Re: help with getchar

Various pieces have been presented and discussed already. Here's how I might piece some things together.
  1. #include <stdio.h>
  2.  
  3. char *getline(char *dst, size_t size)
  4. {
  5. size_t i = 0;
  6. for ( ;; )
  7. {
  8. int ch = getchar();
  9. if ( ch == '\n' || ch == EOF )
  10. {
  11. break;
  12. }
  13. dst[i] = ch;
  14. if ( ++i >= size - 1 )
  15. {
  16. printf("input may not exceed %lu digits\n", (long unsigned)(size - 1));
  17. break;
  18. }
  19. }
  20. dst[i] = '\0';
  21. return dst;
  22. }
  23.  
  24. void input(char *number, size_t size)
  25. {
  26. printf("enter a %lu-digit number: ", (long unsigned)(size - 1));
  27. fflush(stdout);
  28. getline(number, size);
  29. printf("number = \"%s\"\n", number);
  30. }
  31.  
  32. int main(void)
  33. {
  34. char number[10];
  35. input(number, sizeof number);
  36. return 0;
  37. }
  38.  
  39. /* my output
  40. enter a 9-digit number: 1234567890
  41. number = "123456789"
  42.  
  43. enter a 9-digit number: 1234567890123
  44. input may not exceed 9 digits
  45. number = "123456789"
  46. */
As an aside, '\007' can be expressed as '\a' to be more standard.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jul 28th, 2005
0

Re: help with getchar

thanks to you guys but i have a new problem:

the user is prompt to enter number 1 and then number 2.
i declared two arrays to handle each digit...
  1. char num1[ MAXDIGITCOUNT ];
  2. char num2[ MAXDIGITCOUNT ];
in my program, i will passed the contents of these arrays to a function called 'addition' but before i do that i decided to convert first each content of the arrays into integer. I first declare two arrays to handle the converted numbers:
  1. int inum1[ MAXDIGITCOUNT ];
  2. int inum2[ MAXDIGITCOUNT ];
then i do this:
  1. for( n = 0; n < len2; n++ ) {
  2. inum2[ n ] = atoi( num2[ n ] );
  3. }
and i received this message:
[Warning] passing arg 1 of 'atoi' makes pointer from integer without a cast

then i write the function 'addition' as:
  1. void addition(int _num1, int _num2,...){
  2. s1 = _num1[ _len1];
  3. }
and i received this error message:
- subscripted value is neither array nor pointer
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zeek is offline Offline
9 posts
since Jul 2005
Jul 28th, 2005
0

help on 'Segmentation Fault'

the user is prompt to enter number 1 and then number 2.
i declared two arrays to handle each digit...
  1. char num1[ MAXDIGITCOUNT ];
  2. char num2[ MAXDIGITCOUNT ];
in my program, i will passed the contents of these arrays to a function called 'addition' but before i do that i decided to convert first each content of the arrays into integer. I first declare two arrays to handle the converted numbers:
  1. int inum1[ MAXDIGITCOUNT ];
  2. int inum2[ MAXDIGITCOUNT ];
then i do this:
  1. for( n = 0; n < len2; n++ ) {
  2. inum2[ n ] = atoi( num2[ n ] );
  3. }
and i received this message:
[Warning] passing arg 1 of 'atoi' makes pointer from integer without a cast

then i write the function 'addition' as:
  1. void addition(int _num1, int _num2,...){
  2. s1 = _num1[ _len1];
  3. }
and i received this error message:
- subscripted value is neither array nor pointer

and when i tried to run the program, it shows Segmentation Fault while performing
the line containing the 'atoi'
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zeek is offline Offline
9 posts
since Jul 2005
Jul 28th, 2005
0

Re: help on 'Segmentation Fault'

atoi expects a pointer to a nul-terminated array of characters, that is, a "c-style string".

You're passing it a character, not a pointer to an array of characters. When a character gets interpreted as a pointer, it ends up refering to some arbitrary address of memory that the program has no right to access. Accessing memory it's not allowed to access gives your program the segmentation fault.

If you want to convert a character such as '1' to an integer value of 1, all you need to do is subtract '0'. I.e. char x = '5'; int y = x - '0'; /* y == 5 now */
Team Colleague
Reputation Points: 1135
Solved Threads: 173
Super Senior Demiposter
Rashakil Fol is offline Offline
2,480 posts
since Jun 2005
Jul 28th, 2005
0

Re: help with getchar

atoi expects a null-terminated string, not a single character. Try something like this (untested).
  1. inum2[ n ] = num2[ n ] - '0';
<< Threads merged -- don't post duplicates. >>
Quote ...
Do not flood the forum
Do not flood the forum posting the same question in multiple forums, or multiple ways. All that happens is it gets confusing. It's a lot easier for everyone to get the answers they need if everything is kept in one place.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: File I/O Help
Next Thread in C Forum Timeline: remote system





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC