problem

This is a discussion on problem within the C Programming forums, part of the General Programming Boards category; I have this piece of code Code: int main(){ int x; printf("number:"); scanf( "%d" , &x); printf("x!== %d",x,fakt(x)); getchar(); return ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    28

    problem

    I have this piece of code
    Code:
    int main(){
    	int x;
    	printf("number:");
    	scanf( "%d" , &x);
    	printf("x!== %d",x,fakt(x));
    	getchar();
    	return 0;
    }
    when i ran it it asks for the number
    when you press enter it closes...
    where is the problem???
    note:fakt function works properly

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Did you enter a number before hitting enter?

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    printf("x!== %d",x,fakt(x));
    It may work perfectly, but what is it doing here? There is only one number in the template.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    28
    Quote Originally Posted by Bladactania View Post
    Did you enter a number before hitting enter?
    of course

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    28
    Quote Originally Posted by MK27 View Post
    Code:
    printf("x!== %d",x,fakt(x));
    It may work perfectly, but what is it doing here?
    tried it , it doesn"t solve it

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Tried what?

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    28
    Quote Originally Posted by Bladactania View Post
    Tried what?
    i tested the fakt function

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    that wasn't the point... in your printf statement, you have two variables to print, but only one place in your string to put the values.

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    28
    Quote Originally Posted by Bladactania View Post
    that wasn't the point... in your printf statement, you have two variables to print, but only one place in your string to put the values.
    tried it 2

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    what does your code look like now? And post the code for the fakt function

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    28
    Code:
    int fakt(int a){
    	int x,y=1;
    	for(x=1;x<=a;x++){
    
    		y*=x;
    	}
    	return y;
    }
    
    int main(){
    	int x;
    	printf("Rakam:   ");
    	scanf( "%d" , &x);
    	printf("%d!",x);
    	getchar();
    	return 0;
    by the i use windows visual c++
    Last edited by alperen1994; 04-17-2009 at 08:31 AM.

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by alperen1994 View Post
    tried it , it doesn"t solve it
    It works here. I suspect it's Windows closing windows. The getchar() at the end picks up the \newline from when you press return after entering the number, so it doesn't stop; add a second one to create an actual pause.
    Code:
    getchar();
    getchar();
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #13
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    then when you run it it should tell you what the error is

  14. #14
    Registered User
    Join Date
    Mar 2009
    Posts
    28
    Quote Originally Posted by MK27 View Post
    It works here. I suspect it's Windows closing windows. The getchar() at the end picks up the \newline from when you press return after entering the number, so it doesn't stop; add a second one to create an actual pause.
    Code:
    getchar();
    getchar();
    thank u alot u saved alot of trouble

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21