Advertisement

Home Work n' printf()

Started by February 08, 2000 12:26 AM
20 comments, last by Wild9eR 24 years, 7 months ago
Yes, the * sign is exactly what you want. Quoting from the MSDN docs:

If the width specification is an asterisk (*), an int argument from the argument list supplies the value. The width argument must precede the value being formatted in the argument list. A nonexistent or small field width does not cause the truncation of a field; if the result of a conversion is wider than the field width, the field expands to contain the conversion result.

printf("%*s\n", strlen(str)*2, str);

Edited by - Staffan on 2/8/00 2:56:08 PM
Thank you, thank you, and thank you

Thank you for that tide bit of code.

And yes, I do know how to to this if I used a for loop but that was never the question. That is not intended as a snide comment either I just wanted to show that I am not a complete tard.

#include
#include

int main(void)
{
char lastName[25];
int length;

printf("please input your last name :");
scanf("%s",lastName);
printf("\n");
length = strlen(lastName);
for (int i = 0; i < length; i++)
{
printf(" ");
}

printf("%s\n",lastName);

return 0;

}
Job description : Duct Tape
Advertisement
It's usually better to get the answer wrong then to not do it (or get it from somewhere else). The teacher (if he/she is a good one) will recognize that you (and maybe others) had a problem with that question and follow up on it in the next class.

My first response might not have given you the answer but it should have made you realize that you were incorrectly utilizing the function. That should usually be enough to make someone go back and research more. Which is a good thing. I have had to do exactly that many times and I usually figure out a better or faster way of addressing the problem and hand.

Sometimes the answer may not even be printed in any manual and it would be up to the student to work out a logical way of reaching a solution. This will help you out greatly if you want to become a game programmer. All successful games find creative solutions that never existed before. Why? Because the problems never existed before. They are specific to that application.

i.e. Writing a routine that xor's every second pixel in a sprite with the previous pixel. (would look bad). I doubt that you would find it in a manual or any pieces of code on the internet. You would know how to do the basics, but it would be up to you to find the solution and write code to solve this problem.

I don't give solutions to problems, I usually don't give code. However I do try to direct the person in the proper direction where they could find the answer (with a little research).

On occasion I have been know to direct someone to the exact code that they are looking for... but in PASCAL. Some might complain about this, but in essence, I have given them the exact information that they requested. "how do I do this". Reading PASCAL is almost the same as C but you can't just cut and paste it into your program. That would not benefit you in the long run. It would be just a quick fix. You may not be able to use this code, however all the calculations, logic, implementation etc.. is all there in vast amounts of detail. It would be up to the person asking the question to study the code until they understand the logic, in which case they could now write their own routine.

The homework that you received was not entirely on how to use the printf() function, but how to work out a problem to find a viable solution. You can't get anywhere in programming just by knowing how to use printf().

I know that you are just beginning to program. The fact that you are taking it in school is a good move because they offer a structured discipline. To become successful you will also need to study new concepts and theory on your own time.

Like I stated at the top of this post. It is better to get the answer wrong then to not try or use someone else’s. We are human beings and hopefully we learn by our mistakes.

Over and out.


Edited by - Gromit on 2/8/00 1:42:18 PM
William Reiach - Human Extrodinaire

Marlene and Me


You know in all my years of C programming, I have never needed the *. I didn''t even know you could do that. Looks like you learn something new everyday. Haven''t felt this way since I learned about the use of ( ? : ) if shortcut. Thanks folks. Chuckle. Been a while since I learned how to use a tool I already knew, in a better way.

Kressilac

ps I wonder? Does printf/sprintf/fprintf just implements a loop like the one mentioned in this thread or is there and optimized way to do it without a loop?
Derek Licciardi (Kressilac)Elysian Productions Inc.
There''s nothing wrong looking for help with a task you''re having difficulties with, thats what this message board is for. It''s practicaly the same thing as asking your teacher how to do it. Don''t tell me you guys never asked one of your friends in HS how to do this and that...

-- Staffan
I was going to sprintf to the format string, but that's just me:
char s[256], lastName[256];
...
sprintf ("last name w/ 2x padding: %%%ds", 2 * strlen (lastName));
printf (s, lastName);

Of course, now I see where you weren't allowed to use sprintf. Bummer.

I also didn't know about the * format specifier. You do learn something new every day.

You should turn in your homework like this:
cout << width (2 * strlen lastName) << lastName;



Edited by - Stoffel on 2/8/00 4:11:37 PM
Advertisement
You're right, there is nothing wrong with asking for help. The problem is asking for the answer. You don't learn anything from having things handed to you.

This message board is here to help others. The best way to help someone is to make them think so they can work out the answer for themselves. I don't find giving the answer very helpful at all. In fact I would say that it's harmful.

Asking you teacher for the correct answer doesn't help at all. The teachers should not tell you the correct answer, they should direct you to the correct answer.

If "teachers" just told you how to do something then they would call them "tellers".

Edited by - Gromit on 2/8/00 4:28:28 PM
William Reiach - Human Extrodinaire

Marlene and Me


I agree to that, but he didnt say exactly "give me a bit of working code that i can cut and paste so i can pass". I believe all he wanted was some help and directions, not just the answear.

-- Staffan

Edited by - Staffan on 2/8/00 4:32:56 PM
I agree totally with Gromit. Programming is more than syntaz and thousands of lines of code. It is about understanding a problem, and solving it. Wild9eR, you had the answer, and you would have gotten it if you stuck with it.

Domini
Sleeeeppppppyyyyy, very sleeeepy.

Man it was late last night that I posted this thread, my brain had shut off about 1/2 before. I had been working on my homework for the previous 2. Ya, I might have gotten it myself, but I didn''t. So I thank you all for the help.

Wild9eR

This topic is closed to new replies.

Advertisement