Thread: Student need your help

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    8

    Question Student need your help

    Hey guys,

    I was wondering if you could hep me. I'm a first yeat University student and i was having some problems. Here's my (amateur) program:

    Code:
    #include <stdio.h>
    
    #define CHARGE 40
    
    main()
    {
    	char ownerName[81];
    	int apartmentNumber;
    	int bedrooms;
    	char category;
    	int levy;
    	char message [81]="";
    	double totalLevyPayable;
    
    
    	printf("\nPlease enter your name:  ");
    	gets(ownerName);
    
    	printf("\nPlease enter your apartment number:  ");
    	scanf("%d", &apartmentNumber);
    
    	printf("\nPlease enter the amount of bedrooms your apartment has:  ");
    	scanf("%d", &bedrooms);
    
    	printf("\nPlease enter the category of the apartment(A, B or C):  ");
    	getchar();
        category=getchar();
    
    
    
        switch(category)
        {
    		case 'a':
    		case 'A': if (bedrooms <= 1)
    						levy = (10);
    				  else if (bedrooms = 2)
    				  		levy = (10 + 3);
                      else
                      		levy = (10 + 3 + 3) * (bedrooms - 2);
                      break;
    
            case 'b':
    		case 'B': if (bedrooms <= 2)
    						levy = (7 + 1);
    				  else
    				  		levy = (7 + 1 + 2) * (bedrooms - 1);
                      break;
    
            case 'c':
    		case 'C': levy = 5 + (bedrooms * 1);
                      break;
    
            default : strcpy(message, "\nInvalid category.");
            break;
         }
    
         if(strcmp(message,"")==0)
         {
         totalLevyPayable = (levy * CHARGE);
    
         printf("\n			E V E R   R I S E   E S T A T E				");
         printf("\n			   Northern Paradise Beach					");
         printf("\n\n%s", ownerName);
         printf("\nApartment number:  %d", apartmentNumber);
         printf("\nEver Rise Estate");
         printf("\n--------------------------------------------------------");
         printf("\nParticulars of your apartment number %d:\n", apartmentNumber);
         printf("\nCategory             :	%s", &category);
         printf("\nNumber of Bedrooms	:	%d", bedrooms);
         printf("\nLevy (in units)		:	%d", levy);
         printf("\nCharge per unit		:	40.00");
         printf("\n\nTotal Levy payable	        %7.2lf", totalLevyPayable);
         printf("\n-------------------------------------------------------\n");
     	 }
     	 else printf("\n%s\n", message);
    
    
    }
    My first problem is mt first case category:


    Code:
    case 'a':
    case 'A': if (bedrooms <= 1)
    		levy = (10);
    	 else if (bedrooms = 2)
    		levy = (10 + 3);
                      else
                      	levy = (10 + 3 + 3) * (bedrooms - 2);
                      break;
    This doesn't seem to work for some reason. If you enter 1 or 2 bedrooms it doeswork but as soon as you go over 2 bedrooms and have a category A it doesnt. Can anyone help me with this?

    Another line causing problems with me is this one:

    printf("\nCategory : %s", &category);

    This prints out the character right but the character is usually followed by a smile or something else.Can you help me with this? And also could i also turn this charcter into an uppercase one automatically.

    And finally, (if your still with me) id like some help with doing a loop that ask users whether they want to re-enter the information and if they type yes the screen would be cleared and the program would start again.

    Any help would be greatly appreciated.

    Thnx in adavance guys!

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>If you enter 1 or 2 bedrooms it doeswork but as soon as you go over 2 bedrooms and have a category A it doesnt. Can anyone help me with this?

    Right now you're assigning the value 2 to bedrooms. To check for equality, you use ==, not just =.

    >>printf("\nCategory : %s", &category);
    Use %c since category is just a character, and remove the &.

    >>And finally, (if your still with me) id like some help with doing a loop that ask users whether they want to re-enter the information and if they type yes the screen would be cleared and the program would start again.

    Just encase all of your code in a do-while loop, like this:
    Code:
    char resp;
    do {
        // All of your code
       
        printf( "Do you want to enter another set of data? (y/n) " );
        scanf( "%c", &resp );
    
    } while( tolower( resp ) == 'y' );
    As for clearing the screen, check the FAQ.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: Student need your help

    Also, in this code:
    Code:
    	printf("\nPlease enter your name:  ");
    	gets(ownerName); /* Switch to fgets() -- gets will read in more chars than 
                                ownerName can hold and blow up your program */
    
    	printf("\nPlease enter your apartment number:  ");
    	scanf("%d", &apartmentNumber);
    
    	printf("\nPlease enter the amount of bedrooms your apartment has:  ");
    	scanf("%d", &bedrooms);
    
    	printf("\nPlease enter the category of the apartment(A, B or C):  ");
    	getchar();    This line reads the A,B,C and throws it away.  
                          If this works, you are cleaning up a problem with scanf() 
                          and therefore should probably not be using scanf(), 
                          better and less kludgey ways exist. */ 
           category=getchar();
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    8

    Thumbs up Thnx guys

    Whoa you guys are great .Geniouses! Now only one final questin to XSquared. When i put that do function in it does work, but once you type y or n it takes u out of the program straight away. Did i paste it in correctly? I thought using the while function would be better - its a shame we havent learned this in class yet!

    Cheers again guys

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>Did i paste it in correctly?
    I dunno, post your code.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    8

    LOL!

    Good point there mate.
    Here it is.

    Code:
    #include <stdio.h>
    
    #define CHARGE 40
    
    main()
    {
    	char ownerName[81];
    	int apartmentNumber;
    	int bedrooms;
    	char category;
    	int levy;
    	char message [81]="";
    	double totalLevyPayable;
    	char resp;
    
    
    	printf("\nPlease enter your name:  ");
    	gets(ownerName);
    
    	printf("\nPlease enter your apartment number:  ");
    	scanf("%d", &apartmentNumber);
    
    	printf("\nPlease enter the amount of bedrooms your apartment has:  ");
    	scanf("%d", &bedrooms);
    
    	printf("\nPlease enter the category of the apartment(A, B or C):  ");
    	getchar();
        category=getchar();
    
    
        do {
    
        switch(category)
        {
    		case 'a':
    		case 'A': if (bedrooms <= 1)
    						levy = (10);
    				  else if (bedrooms == 2)
    				  		levy = (10 + 3);
                      else
                      		levy = (10 + 3 + 3) * (bedrooms - 2);
                      break;
    
            case 'b':
    		case 'B': if (bedrooms <= 2)
    						levy = (7 + 1);
    				  else
    				  		levy = (7 + 1 + 2) * (bedrooms - 1);
                      break;
    
            case 'c':
    		case 'C': levy = 5 + (bedrooms * 1);
                      break;
    
            default : strcpy(message, "\nInvalid category.");
            break;
         }
    
         if(strcmp(message,"")==0)
         {
         totalLevyPayable = (levy * CHARGE);
    
         printf("\n			E V E R   R I S E   E S T A T E				");
         printf("\n			   Northern Paradise Beach					");
         printf("\n\n%s", ownerName);
         printf("\nApartment number:  %d", apartmentNumber);
         printf("\nEver Rise Estate");
         printf("\n--------------------------------------------------------");
         printf("\nParticulars of your apartment number %d:\n", apartmentNumber);
         printf("\nCategory             	:	%c", category);
         printf("\nNumber of Bedrooms	:	%d", bedrooms);
         printf("\nLevy (in units)		:	%d", levy);
         printf("\nCharge per unit		:	40.00");
         printf("\n\nTotal Levy payable	        		%7.2lf", totalLevyPayable);
         printf("\n--------------------------------------------------------\n");
     	 }
     	 else printf("\n%s\n", message);
    
     	 printf( "Do you want to enter another set of data? (y/n) " );
    	 scanf( "%c", &resp );
    	 }
    
    	 while( tolower( resp ) == 'y' );
    
    }
    I dnt know i just thought while would be used
    Thnx again

  7. #7
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    you could still fix the gets:
    gets(ownerName);

    with
    Code:
    fgets(ownerName, sizeof ownerName, stdin);
    if(ownerName[strlen(ownerName) - 1] == '\n') 
               (ownerName[strlen(ownerName) - 1] = '\n';

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Originally posted by chrismiceli
    you could still fix the gets:
    gets(ownerName);

    with
    Code:
    fgets(ownerName, sizeof ownerName, stdin);
    if(ownerName[strlen(ownerName) - 1] == '\n') 
               (ownerName[strlen(ownerName) - 1] = '\n';
    I think you mean:
    Code:
    if(ownerName[strlen(ownerName) - 1] == '\n') 
               (ownerName[strlen(ownerName) - 1] = '\0';
    since there is no point in testing to see if a spot is something and then assigning it the same value it already has.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>there is no point in testing to see if a spot is something and then assigning it the same value it already has<<
    There is also no point in using strlen() twice either, as the program will have to walk the length of the string twice.

    To improve, you can use
    char *p;
    if ((p = strchr (ownerName, '\n')) != NULL) *p = '\0';

    or to save on the keystrokes:
    ownerName[strcspn(ownerName, "\n")] = '\0';
    or even
    strtok (ownerName, "\n");
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User
    Join Date
    Aug 2003
    Posts
    8
    Thats great guys, the reason why i used getchar and gets and scan is coz our teacher told us to do it like that.
    The operations you are doing are little complex for our class.
    So im just keeping it to the standards he set. So can anyone help me with this loop?

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by pinkpanther1586
    So can anyone help me with this loop?
    I don't think I can help. Reading this code caused me to have a seg fault:
    Code:
    default : strcpy(message, "\nInvalid category.");
            break;
         }
    
         if(strcmp(message,"")==0)
    Don't take this the wrong way, but that's absolutely horrible.

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You also need to sort your layout out, it's hard to read through at present. Maybe its OK in your debugger/editor, but it on here it aint. Try keeping to tabs OR a fixed number of spaces for indents (preferably the later).

    You've missed some header files too:
    #include <string.h>
    #include <ctype.h>

    main() should be
    int main(void)
    and return 0; at the end.

    Code:
        scanf("%c", &resp);
      } while (tolower(resp) == 'y');
    Your problem here will be related to this , and you use of getchar() and scanf() together.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Registered User
    Join Date
    Aug 2003
    Posts
    8
    Quzah how should have I done it?
    Thats the only way I've seen my teacher used it and i copied it and it worked. Is it the wrong standard?
    So my use of getchar and scanf is incorrect?

  14. #14
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>if(strcmp(message,"")==0)<<
    This isn't good for two reason. To start with, to test for an empty string, you simply go
    >>if (message[0] == '\0')
    which tests the first byte. This saves the unnecessary overhead of calling strcmp() and creating a pointless empty string literal.

    Second, although using a string to determine success/failure of an action, it's not really good practice. But this is something you'll learn in time, when you've got to grips with making the loop work properly.

    >>So my use of getchar and scanf is incorrect?<<
    Using them together in the same program is a common mistake that causes the problems you're experiencing now. Avoid scanf() if you can, in the long run, you'll be better of without it
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Updating in a sequential file?
    By Ronnyv1 in forum C Programming
    Replies: 1
    Last Post: 03-24-2009, 04:41 PM
  2. Database assignment is Killing me!
    By Boltrig in forum C Programming
    Replies: 2
    Last Post: 11-29-2007, 03:56 AM
  3. LinkList Sorting in C
    By simly01 in forum C Programming
    Replies: 3
    Last Post: 11-25-2002, 01:21 PM
  4. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM
  5. A few tips please...
    By gems in forum C++ Programming
    Replies: 4
    Last Post: 04-11-2002, 02:28 PM