Thread: What is the problem with this one.

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    6

    What is the problem with this one.

    it crashes when input special type of delivery



    Code:
    #include <stdio.h>
    
    
    
    
    int main()
    
    
    {
    
    
    	float total_words,total;
    	char t[50],deliv; 	
    
    
    	printf("Enter Customer's Name: ");
    	scanf("%s",t);
    	
    	printf("Input Number Of Words Used In The Telegram: ");
    	scanf("%f",&total_words);
       
    	 if(total_words>12)
    	{
    	total = 28.99+(2.5*(total_words-12));		
    	}
    	
    	 else if(total_words<12)
    	 {
    	 	total = 28.99;
    	 }	
    	printf("Input type of delivery (S-Special) or (N-None): ");
    	scanf("%c",deliv);
    
    
    	if( deliv == 's' && deliv == 'S')
    	{
    		total = total + 5;
    	
    		}
    	printf("Total cost: %f", total);
    
    
    	return 0;
    Attached Images Attached Images What is the problem with this one.-untitled-jpg 

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    A few suggestions.

    Always pay attention to compiler warnings. You should have gotten one regarding your scanf for deliv. You need to pass the address of deliv to scanf, not the value.
    Code:
    scanf("%c",&deliv);
    Also, at least when I run this on my machine, I get some finickiness with the 3rd scanf as it picks up whitespace (newline characters) from the previous scanf. To combat this, you can prepend a space to the scanf string.
    Code:
    scanf(" %c",&deliv);
    Finally, I believe you have a logic problem. Where you check to see if the user typed in S or s, you have && (AND). I believe with what you are trying to do is || (OR).
    Code:
    if( deliv == 's' || deliv == 'S')
    Good luck!

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,687
    If your reading strings into a program, do not use scanf. Use fgets and sizeof to get the exact memory space including the NULL terminating character. You can then format the string further by using
    sscanf() to ensure the correct data type is inputted before it's used.
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-09-2014, 06:46 PM
  2. Problem passing argument into function, basic problem
    By tsdad in forum C++ Programming
    Replies: 7
    Last Post: 05-22-2013, 12:09 PM
  3. Replies: 1
    Last Post: 12-07-2012, 10:00 AM
  4. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM

Tags for this Thread