help on this enrollment system in cprogramming when I run the programming the output sometimes fall through example when i enter the student number name and address but when i press enter in the address it skips the scanf in the status and goes to the gender it alse does it when I enter the rate it skips the no of units can you guys help me out

#include<stdio.h>
#include<conio.h>

main()
{
int studentNumber, units, yearLevel, cash, statusLocal;
char studentName[50], address[50], status, gender,course[10], birthday;
float rate , mis , cashTendred , accessment, tutionFee, change; 

      printf(" Enter Student Number : ");
      scanf("%d",&studentNumber);
     
      
      printf(" Enter Student Name : ");
      scanf("%s", studentName);
      
      
      printf(" Enter Address : ");
      scanf("%s", &address);
    
      
      printf(" Birhtday: MM/DD/YY ");
      scanf("%s",&birthday);
      
      
      printf(" Status: M/S ");
      scanf("%c", &status);
      
      
      printf(" Gender: ");
      scanf("%c", &gender);
      
      
      printf("_____________________________________________\n");
      printf(" Course : ");
      scanf("%s",  &course);
      
      
      printf(" Year Level : ");
      scanf("%d" ,&yearLevel);
     
      
      printf(" Status ([1]Local/[2]International):\n");
      scanf("%d",&statusLocal);
      
      printf("______________________________________________\n");
      switch(statusLocal)
      {
      case 1:
           {
           printf(" Rate per unit: ");
           scanf("%5.2f",&rate);
           
           printf(" No of units: ");
           scanf("%d",&units);
           
           printf(" Miscellaneos fee: ");
           scanf("%5.2f",&mis);
           
           accessment = (rate * units) + mis;
           
           printf("Total Ascessment: %f",accessment);
           
           printf(" Mode of Payment: ([1]cash/[2]installment)");
           scanf("%d", &cash);
           break;
           }
      case 2:
           {
           printf("422222");
           }
      }
      if(cash == 1)
      {
           tutionFee= accessment * 0.10;
           printf(" TuitionFee %5.2f",tutionFee);
      }
      else
      {
           tutionFee = accessment / 2;
           printf(" Downpayment : ");
           printf(" Balance ");
      }
      printf(" Cash Tendered : ");
      scanf("%5.2f", cashTendred);
      
      change = cashTendred - tutionFee;
      printf(" Change: %f5.2",change);
    getch();
}

Recommended Answers

All 2 Replies

I tried compiling your program and got numerous errors

test.c:4:1: warning: return type defaults to ‘int’
test.c: In function ‘main’:
test.c:19:2: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[50]’
test.c:36:2: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[10]’
test.c:52:4: warning: unknown conversion type character ‘.’ in format
test.c:52:4: warning: too many arguments for format
test.c:58:4: warning: unknown conversion type character ‘.’ in format
test.c:58:4: warning: too many arguments for format
test.c:86:2: warning: unknown conversion type character ‘.’ in format
test.c:86:2: warning: too many arguments for format
test.c:86:7: warning: ‘cashTendred’ is used uninitialized in this function

This is one of your errors:

scanf("%s", &address);

It should be

scanf("%s", address);

Which is discouraged. Try using fgets() instead.

If you use scanf(), you have to know what scanf() does and does not do:

1) For char input, scanf() will take *ONE and one ONLY* char. That means that the newline char from hitting the enter key, is still in the keyboard buffer.

Which means that the NEXT scanf() for a char will be taking the newline FIRST, and say "I've got my char, so I'm done here". Leaving you to believe it has magically "skipped" it's whole input.

2) For number input, scanf() will skip over whitespace char's - including newlines, so it's not a problem if there is a newline in the buffer, left over.

The solution is to put one of these, after EVERY scanf(), immediately:

getchar();

That will pull off the newline char, and stop the "skipping" behavior that you've seen.

Note carefully, Gerard's advice, above. Don't use &address, if address is a char array, and undoubtedly, fgets is a more polished way of getting user input. Scanf() can be used for most assignments, but you have to know how it works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.