help on 'Segmentation Fault'

Reply

Join Date: Jul 2005
Posts: 9
Reputation: zeek is an unknown quantity at this point 
Solved Threads: 0
zeek zeek is offline Offline
Newbie Poster

help with getchar

 
0
  #1
Jul 27th, 2005
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. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: help with getchar

 
0
  #2
Jul 27th, 2005
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. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: help with getchar

 
0
  #3
Jul 27th, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 9
Reputation: zeek is an unknown quantity at this point 
Solved Threads: 0
zeek zeek is offline Offline
Newbie Poster

Re: help with getchar

 
0
  #4
Jul 27th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: help with getchar

 
0
  #5
Jul 27th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,321
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 230
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: help with getchar

 
0
  #6
Jul 27th, 2005
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 9
Reputation: zeek is an unknown quantity at this point 
Solved Threads: 0
zeek zeek is offline Offline
Newbie Poster

Re: help with getchar

 
0
  #7
Jul 28th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 9
Reputation: zeek is an unknown quantity at this point 
Solved Threads: 0
zeek zeek is offline Offline
Newbie Poster

help on 'Segmentation Fault'

 
0
  #8
Jul 28th, 2005
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'
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,018
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 137
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is online now Online
Super Senior Demiposter

Re: help on 'Segmentation Fault'

 
0
  #9
Jul 28th, 2005
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 */
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,321
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 230
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: help with getchar

 
0
  #10
Jul 28th, 2005
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. >>
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC