Does'nt excute program always crashes

This is a discussion on Does'nt excute program always crashes within the C Programming forums, part of the General Programming Boards category; I wrote this code to convert a mathematical equation from 1 form to another but when you put in everything ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    92

    Does'nt excute program always crashes

    I wrote this code to convert a mathematical equation from 1 form to another but when you put in everything it just crashes. I was hoping if some can point out my mistakes to me.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    int main()
    {
    	char z,w,G,num,den;
    	float Ts;
    	z=z;
    	
    w=z-1/Ts;
    
    G=num/den;
    
    printf("Please enter the samplling rate: ");
    scanf("%f",&Ts);
    	
    printf("Please enter the numarator in terms of w: ");
    scanf("%s",&num);
    
    printf("Please enter the denominator in terms of w: ");
    scanf("%s",&den);
    
    printf("The discrete transfer function in the form of Z is %s \n",G);
    
    return 0;
    }

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    You are asking for a string here:

    Code:
    scanf("%s",&num);
    Yet you are trying to put it into a single char variable. That will not work. You should either store your string (which is what scanf is looking for with %s) in an array, or else change your call to scanf to be looking for a single char (%c) If you do go the first way, then you ought to limit the amount of input to you scanf call with a width specifier, so the user does not overflow the array.

  3. #3
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Huh?

    You already posted almost identical code to another thread, which already contains feedback. The only things you've done different this time is add #include <math.h>, which doesn't make any difference, because you're not using anything from math.h, and you changed the format string from %c to %s, which makes even less sense. If you want to read in the numbers as strings first and convert them, then read them into a char array, not a single character.

    Pay attention to the feedback on the previous thread, and if you have other questions, post them on that thread.

    This thread should probably be locked.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple login program - Crashes :(
    By spadez in forum C Programming
    Replies: 1
    Last Post: 03-23-2009, 04:16 PM
  2. Replies: 3
    Last Post: 02-29-2008, 12:29 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. program crashes on closure.
    By ssjnamek in forum C++ Programming
    Replies: 7
    Last Post: 09-26-2005, 04:55 PM
  5. My program crashes with this code
    By blackwyvern in forum C++ Programming
    Replies: 3
    Last Post: 01-27-2002, 11:28 PM

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