Student need your help

This is a discussion on Student need your help within the C Programming forums, part of the General Programming Boards category; Hey guys,
I was wondering if you could hep me. I'm a first yeat University student and i was having ...

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.

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

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

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

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!

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

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

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?

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

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