Thread: scanf help?

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    17

    scanf help?

    The program allow the user to buy movie tickets. It should show movie listing, ask the user to select a movie, show time, and number of tickets, ask for payment. And have a print out to the console as the program proceeds.

    My problem is that individually the methods are all in working order. But when i try running the program as a whole the error occurs at the first scanf statement in the second method.

    I've been going at this for a few hours now. The variables are being read in correctly but the error always occurs.

    Help is greatly appreciated


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void printMovieListing (char* am, int* d3);
    void sellTickets(char am, int d3, int* c, float* d);
    void getPayment(float price, int ratetype);
    
    #define ADULTRATE 8.50f
    #define STUDENTRATE 5.00f
    #define CISRATE 3.00f
    
    int main (void)
    {
        float price;
        char am;
        int d3, ratetype;
    
        printMovieListing (&am, &d3);
    	sellTickets(am, d3, &ratetype, &price);
    	getPayment(price, ratetype);
    	
        system("pause");
        return 0;
    }
    
    void printMovieListing (char* am, int* d3)
    {
        int hour, min, choice, threeD;
        char a;
    
        printf("Current Movies Shown:\n");
        printf("1. The Eagle\n");
        printf("2. The Green Hornet\n");
        printf("3. The Green Hornet 3d\n");
        printf("4. Tron: Legacy\n");
        printf("5. Sanctum\n");
        printf("6. Sanctum 3d\n");
        
        printf("Your Selection [1-6]: ");
        scanf("%d", &choice);
    
        if(choice==3 || choice==6)
        {
            printf("11:35am,  2:30pm,  5:25pm,  8:05pm,  10:50pm\n");
            threeD = 0;
        }
        else
        {
            printf("9:35am,  12:30pm,  3:25pm,  6:05pm,  8:50pm\n");
            threeD = 1;
        }
    
        *d3 = threeD;
    
        printf("Enter a Time [example: 2:45pm]: ");
        scanf("%d:%d%c", &hour, &min, &a);
    
    	*am = a;
    
        return;
    }
    
    void sellTickets(char am, int d3, int* c, float* d)
    {
        int adult, student, cis, tickets, ratetype;
        float total;
    	
    	printf("%c", am);
    	printf("%d", d3);
    	
    
        printf("Number of Adult Tickets: ");
        scanf("%d", &adult);
        printf("Number of Student Tickets: ");
        scanf("%d", &student);
        printf("Number of CIS 15AG Students Tickets: ");
        scanf("%d", &cis);
    
    	tickets = adult + student + cis;
    	printf("That will be %d tickets.\n", tickets);
    	
        if(d3 == 0)
        {
            if(am == 'a')
            {
                total = ((ADULTRATE+1)*adult) + ((STUDENTRATE+1)*student) + ((CISRATE+2)*cis);
    			ratetype = 3;
            }
            else
            {
                total = ((ADULTRATE+2)*adult) + ((STUDENTRATE+2)*student) + ((CISRATE+2)*cis);
    			ratetype = 2;
            }
        }
        else
        {
            if(am == 'a')
            {
                total = ((ADULTRATE-1)*adult) + ((STUDENTRATE-1)*student) + ((CISRATE)*cis);
    			ratetype = 1;
            }
            else
            {
                total = ((ADULTRATE)*adult) + ((STUDENTRATE)*student) + ((CISRATE)*cis);
    			ratetype = 0;
            }
        }
    
    	*c = ratetype;
    	*d = total;
    	
        return;
    }
    
    void getPayment(float price, int ratetype)
    {
    	float payment, credit;
    
    	switch(ratetype)
    	{
    	case 0: printf("Standard Rate\n");
    			printf("Adult: $%2.2f, Student $%2.2f, CIS15AG Student $%2.2f\n", ADULTRATE, STUDENTRATE, CISRATE);
    		break;
    	case 1:	printf("Morning Discount\n");
    			printf("Adult: $%2.2f, Student $%2.2f, CIS15AG Student $%2.2f\n", (ADULTRATE-1), (STUDENTRATE-1), CISRATE);
    			printf("No Morning Discount for CIS15AG Sudents who should be in class.\n");
    		break;
    	case 2:	printf("Standard 3D Rate\n");
    			printf("Adult: $%2.2f, Student $%2.2f, CIS15AG Student $%2.2f\n", (ADULTRATE+2), (STUDENTRATE+2), (CISRATE+2));
    		break;
    	case 3:	printf("Morning 3D Discount\n");
    			printf("Adult: $%2.2f, Student $%2.2f, CIS15AG Student $%2.2f\n", (ADULTRATE+1), (STUDENTRATE+1), (CISRATE+2));
    			printf("No Morning Discount for CIS15AG Sudents who should be in class.\n");
    		break;
    	}
    
    	printf("Enter your payment: $");
    	scanf("%f", &payment);
    
    	if(payment > price)
    	{
    		credit = payment - price;
    		printf("You have a credit of: $%2.2f\n", credit);
    		printf("Please pick up your tickets at will call.\n"); 
    	}
    	else if(payment < price)
    	{
    		credit = price - payment;
    		printf("Your ticket cost is $%2.2f more than your payment\n", credit);
    		printf("Guess you will have to wait till the DVD release.\n"); 
    	}
    	else
    		printf("Please pick up your tickets at will call.\n");
    
            return;
    }
    Last edited by Teardrop3903; 02-17-2011 at 07:24 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    if(choice==3 || choice==6)

    scanf("%d:%d%c", &hour, &min, &ap);

    remove the colon after the first d, unless you want the user to be required to enter it.

    if(a = 'a')
    should be if(a == 'a'). You have a few of these. Your compiler should be giving you a warning about that.
    Last edited by Adak; 02-17-2011 at 06:58 AM.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    17
    Quote Originally Posted by Adak View Post
    if(choice==3 || choice==6)

    scanf("%d:%d%c", &hour, &min, &ap);

    remove the colon after the first d, unless you want the user to be required to enter it.
    Thanks for that catch. As you can see i'm still very new at the language.

    And yes i did want them to include the :.

    I'm using visual 2010 and the compiler didn't give me the warning about the if(a == 'a'). ALl i get are a bunch of these

    1>c:\users\philip\documents\visual studio 2010\projects\homework 5\homework 5\lab 5.c(44): warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(304) : see declaration of 'scanf'
    1>c:\users\philip\documents\visual studio 2010\projects\homework 5\homework 5\lab 5.c(60): warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

    relating to the scanf statementss in the second method.
    Last edited by Teardrop3903; 02-17-2011 at 07:05 AM.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This logic, looks Rube Goldberg to me:

    Code:
        if(b == 0)
        {
            if(a == 'a')
            {
                total = ((ADULTRATE+1)*adult) + ((STUDENTRATE+1)*student) + ((CISRATE+2)*cis);
    			ratetype = 3;
            }
            else
            {
                total = ((ADULTRATE+2)*adult) + ((STUDENTRATE+2)*student) + ((CISRATE+2)*cis);
    			ratetype = 2;
            }
        }
    How many rates do you have?

    Just
    sum += adultFull * adultFullTics
    sum += adultDis * adultDisTics
    sum+= studentFull * studFullTics
    //etc.

    All the a= and b = logic, I don't see the need for.

    I would turn off that (scanf() is unsafe) warning, in your compiler. No, scanf() is not the best function for user input, but all the books and teachers use it to teach with, so there you have it. It will work, but you have to use some caution.
    Last edited by Adak; 02-17-2011 at 07:12 AM.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    17
    Quote Originally Posted by Adak View Post
    This logic, looks Rube Goldberg to me:

    Code:
        if(b == 0)
        {
            if(a == 'a')
            {
                total = ((ADULTRATE+1)*adult) + ((STUDENTRATE+1)*student) + ((CISRATE+2)*cis);
    			ratetype = 3;
            }
            else
            {
                total = ((ADULTRATE+2)*adult) + ((STUDENTRATE+2)*student) + ((CISRATE+2)*cis);
    			ratetype = 2;
            }
        }
    How many rates do you have?

    Just
    sum += adultFull * adultFullTics
    sum += adultDis * adultDisTics
    sum+= studentFull * studFullTics
    //etc.

    All the a= and b = logic, I don't see the need for.
    theres 4 rates

    standard = pm + not3d
    morning disc = am + not3d
    standard 3d = pm + 3d
    morning 3d disc = am + 3d

    b determines if the movie is 3d or not
    a determines if the movie is in the morning or not

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Please don't use a or b as variable names! Second one in 12 hours!

    You couldn't pick a more meaningless, name. Use am for the morning and d3 for the 3d maybe (since variables can't always begin with numbers).

    You don't need them, anyway, if you think about it.

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    17
    This is my output. the problem is that the scanf statement wont start for some reason.

    Current Movies Shown:
    1. The Eagle
    2. The Green Hornet
    3. The Green Hornet 3d
    4. Tron: Legacy
    5. Sanctum
    6. Sanctum 3d
    Your Selection [1-6]: 1
    9:35am, 12:30pm, 3:25pm, 6:05pm, 8:50pm
    Enter a Time [example: 2:45pm]: 9:35am
    a1Number of Adult Tickets: Number of Student Tickets: Number of CIS 15AG Student
    s Tickets: That will be 1717986916 tickets.
    Morning Discount
    Adult: $7.50, Student $4.00, CIS15AG Student $3.00
    No Morning Discount for CIS15AG Sudents who should be in class.
    Enter your payment: $You have a credit of: $12348030976.00
    Please pick up your tickets at will call.
    Press any key to continue . . .

  8. #8
    Registered User
    Join Date
    Feb 2011
    Posts
    17
    Quote Originally Posted by Adak View Post
    Please don't use a or b as variable names! Second one in 12 hours!

    You couldn't pick a more meaningless, name. Use am for the morning and d3 for the 3d maybe (since variables can't always begin with numbers).

    You don't need them, anyway, if you think about it.
    sorry i know i'm a little bit sloppy with the code. Bad habit

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Put a printf("choice is: %d\n", choice) right before this line of code:
    if(choice==3 || choice==6)

    and see what's happening to choice's storage. It looks good to me.

    Note that that's a LOT of tickets and credit there! How about initializing them to something reasonable?

  10. #10
    Registered User
    Join Date
    Feb 2011
    Posts
    17
    heres the resulting output:


    Current Movies Shown:
    1. The Eagle
    2. The Green Hornet
    3. The Green Hornet 3d
    4. Tron: Legacy
    5. Sanctum
    6. Sanctum 3d
    Your Selection [1-6]: 1
    choice is: 1
    9:35am, 12:30pm, 3:25pm, 6:05pm, 8:50pm
    Enter a Time [example: 2:45pm]: 2:45pm
    p1Number of Adult Tickets: Number of Student Tickets: Number of CIS 15AG Student
    s Tickets: That will be 1717986916 tickets.
    Standard Rate
    Adult: $8.50, Student $5.00, CIS15AG Student $3.00
    Enter your payment: $You have a credit of: $14066017280.00
    Please pick up your tickets at will call.
    Press any key to continue . . .


    The problem might be in my first method. When i comment out the firs method and assign am n d3 values, everything is in working order.

  11. #11
    Registered User
    Join Date
    Feb 2011
    Posts
    17
    Quote Originally Posted by Adak View Post
    Note that that's a LOT of tickets and credit there! How about initializing them to something reasonable?
    Yea its like that because the program isn't running properly. The user is supposed to input the number of tickets in the second method and then the credit is calculated in the third.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Sounds like you've got a likely suspect there. Remember just how you did that, because troubleshooting is a big part of a programmer's job.

    Test it thoroughly - don't assume that one bug is all there is.

    Cheers!

    P.S. This needs another %c in the last part, for the second letter, doesn't it? I'm running on fumes atm.

    scanf("%d:%d%c%*c", &hour, &min, &a);

    %*c tells scanf() to scan the char, but don't store it anywhere.
    Last edited by Adak; 02-17-2011 at 07:48 AM.

  13. #13
    Registered User
    Join Date
    Feb 2011
    Posts
    17
    Quote Originally Posted by Adak View Post
    Sounds like you've got a likely suspect there. Remember just how you did that, because troubleshooting is a big part of a programmer's job.

    Test it thoroughly - don't assume that one bug is all there is.

    Cheers!
    Please don't leave me stranded. LOL. i've also placed printf("%c", am); and printf("%d", d3); statements in the second method to see if the variables are being read into the second method as you can see in the outputs. I have no idea what the problem could be in the first method whatsoever.

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Let me run it one time.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void printMovieListing (char* am, int* d3);
    void sellTickets(char am, int d3, int* c, float* d);
    void getPayment(float price, int ratetype);
    
    #define ADULTRATE 8.50f
    #define STUDENTRATE 5.00f
    #define CISRATE 3.00f
    
    int main (void)
    {
      float price;
      char am;
      int d3, ratetype;
    
      printMovieListing (&am, &d3);
      sellTickets(am, d3, &ratetype, &price);
      getPayment(price, ratetype);
    	
      system("pause");
      return 0;
    }
    
    void printMovieListing (char* am, int* d3)
    {
      int hour, min, choice, threeD;
      char a;
    
      printf("Current Movies Shown:\n");
      printf("1. The Eagle\n");
      printf("2. The Green Hornet\n");
      printf("3. The Green Hornet 3d\n");
      printf("4. Tron: Legacy\n");
      printf("5. Sanctum\n");
      printf("6. Sanctum 3d\n");
        
      printf("Your Selection [1-6]: ");
      scanf("%d", &choice);
    
      if(choice==3 || choice==6)
      {
        printf("11:35am,  2:30pm,  5:25pm,  8:05pm,  10:50pm\n");
        threeD = 1;
      }
      else
      {
        printf("9:35am,  12:30pm,  3:25pm,  6:05pm,  8:50pm\n");
        threeD = 0;
      }
    
      *d3 = threeD;
    
      printf("Enter a Time [example: 2:45pm]: ");
      scanf("%d:%d%c%*c", &hour, &min, &a);
    
      *am = a;
    
      return;
    }
    
    void sellTickets(char am, int d3, int* c, float* d)
    {
      int adult, student, cis, tickets, ratetype;
      float total;
    	
      printf("%c", am);
      printf("%d\n", d3);
    	
      printf("Number of Adult Tickets: ");
      scanf("%d", &adult);
      printf("Number of Student Tickets: ");
      scanf("%d", &student);
      printf("Number of CIS 15AG Students Tickets: ");
      scanf("%d", &cis);
    
      tickets = adult + student + cis;
      printf("That will be %d tickets.\n", tickets);
    	
      if(d3 == 1)
      {
        if(am == 'a')
        {
          total = ((ADULTRATE+1)*adult) + ((STUDENTRATE+1)*student) + ((CISRATE+2)*cis);
          ratetype = 3;
        }
        else
        {
          total = ((ADULTRATE+2)*adult) + ((STUDENTRATE+2)*student) + ((CISRATE+2)*cis);
          ratetype = 2;
        }
      }
      else
      {
        if(am == 'a')
        {
          total = ((ADULTRATE-1)*adult) + ((STUDENTRATE-1)*student) + ((CISRATE)*cis);
          ratetype = 1;
        }
        else
        {
          total = ((ADULTRATE)*adult) + ((STUDENTRATE)*student) + ((CISRATE)*cis);
          ratetype = 0;
        }
      }
    
      *c = ratetype;
      *d = total;
    	
      return;
    }
    
    void getPayment(float price, int ratetype)
    {
      float payment, credit;
    
      switch(ratetype)
      {
        case 0: printf("Standard Rate\n");
        printf("Adult: $%2.2f, Student $%2.2f, CIS15AG Student $%2.2f\n", ADULTRATE, STUDENTRATE, CISRATE);
        break;
        case 1:	printf("Morning Discount\n");
        printf("Adult: $%2.2f, Student $%2.2f, CIS15AG Student $%2.2f\n", (ADULTRATE-1), (STUDENTRATE-1), CISRATE);
        printf("No Morning Discount for CIS15AG Sudents who should be in class.\n");
        break;
        case 2:	printf("Standard 3D Rate\n");
        printf("Adult: $%2.2f, Student $%2.2f, CIS15AG Student $%2.2f\n", (ADULTRATE+2), (STUDENTRATE+2), (CISRATE+2));
        break;
        case 3:	printf("Morning 3D Discount\n");
        printf("Adult: $%2.2f, Student $%2.2f, CIS15AG Student $%2.2f\n", (ADULTRATE+1), (STUDENTRATE+1), (CISRATE+2));
        printf("No Morning Discount for CIS15AG Sudents who should be in class.\n");
        break;
       }
    
      printf("Enter your payment: $");
      scanf("%f", &payment);
    
      if(payment > price)
      {
        credit = payment - price;
        printf("You have a credit of: $%2.2f\n", credit);
        printf("Please pick up your tickets at will call.\n"); 
      }
      else if(payment < price)
      {
        credit = price - payment;
        printf("Your ticket cost is $%2.2f more than your payment\n", credit);
        printf("Guess you will have to wait till the DVD release.\n"); 
      }
      else
        printf("Please pick up your tickets at will call.\n");
    
      return;
    }
    Seems to work with the above. Give that a shot, I didn't test it thoroughly at all.
    Last edited by Adak; 02-17-2011 at 08:27 AM.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Teardrop3903 View Post
    This is my output. the problem is that the scanf statement wont start for some reason.

    Current Movies Shown:
    1. The Eagle
    2. The Green Hornet
    3. The Green Hornet 3d
    4. Tron: Legacy
    5. Sanctum
    6. Sanctum 3d
    Your Selection [1-6]: 1
    9:35am, 12:30pm, 3:25pm, 6:05pm, 8:50pm
    Enter a Time [example: 2:45pm]: 9:35am
    Since you started off having them select their movie by number, a far more consitent user interface would result if they also chose their times by number...

    Code:
    Your Selection [1-6] : 1
    The Eagle : times: 1) 9:35a, 2) 12:30p, 3) 3:25p, 4) 6:05p, 5) 8:50p
    Select showing [1 - 5] :

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple XOR Program
    By dolfaniss in forum C Programming
    Replies: 8
    Last Post: 05-24-2010, 01:27 PM
  2. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 04:39 PM
  3. Replies: 2
    Last Post: 02-20-2005, 01:48 PM
  4. scanf issue
    By fkheng in forum C Programming
    Replies: 6
    Last Post: 06-20-2003, 07:28 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM

Tags for this Thread