1. ## 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;

gets(ownerName);

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. >>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.

3. ## 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 */

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();```

4. ## 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. >>Did i paste it in correctly?
I dunno, post your code.

6. ## 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;

gets(ownerName);

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. 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. 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. >>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");

10. 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. 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.

12. 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.

13. 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. >>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

Popular pages Recent additions