#Help with basic for loop algorithm
41 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
```cpp
int main() {}
```
int main() {} ```
` #include <stdio.h>
#include <string.h>
int main (int argc , char **argv ){
char name[20];
//prompt for first name
printf("Please enter your name: ");
fgets(name,20,stdin);
int number = atoi(name);
(for int i = 0; i < name; i ++){
printf("*");
print("\n");
print("*",name,"*");
print("\n");
}
return 0;
}`
!f
` ```cpp
#include <stdio.h>
#include <string.h>
int main(int argc, char** argv) {
char name[20];
// prompt for first name
printf("Please enter your name: ");
fgets(name, 20, stdin);
int number = atoi(name);
(for int i = 0; i < name; i ++){
printf("");
print("\n");
print("", name, "*");
print("\n");
}
return 0;
}
So, first of all there is no such function as print which is why it can't compile
Then (for int i = 0; i < name; i++) is not the correct syntax for for-loops, the correct sytnax is for (int i = 0; i < name; i++).
Next this isn't Python. So while the line print("*", name, "*") could come straight outta Python it doesn't work here. In C you have to do printf("*%s*", name) (not to mention that you completely forgot the spaces).
Next I have no clue what you're trying to do with int number = atoi(name). NEVER in your pseudo-code does it tell you that you should try to interpret the given name as a number.
Then why did you stuff so much into the for-loop? In the pseudo-code there's only the print * inside the loop, however in your code there is so much more inside the loop then there should be.
Also you should take it as a warning sign when the pseudo code that you're trying to copy 1-1 has 2 loops and yours has only 1
All in all this looks like you've never even seen a single line of (C) code nor put too much effort into it and I'm astonished how you could complete algorithm 1-4.
@stiff sable
#include <stdio.h>
#include <string.h>
#define LEN 128
int main()
{
char line[LEN];
printf("enter name> ");
fgets(line, LEN, stdin);
int len = strlen(line);
for(int i=0; i<len+4; i++)
printf("*");
printf("\n");
printf("* %s *\n", line);
for(int i=0; i<len+4; i++)
printf("*");
printf("\n");
return 0;
}
;compile c | ugo
enter name> *******
* ugo *
*******
You meant to ping me? Just ping <@&1028867343461384253> if you can't find me besides all the other Monkes
Only 3 minor things:
- Instead of
int len = strlen(line);with the conditioni < len + 4you can just doint len = strlen(line) + 4;with the conditioni < lenlike the pseudo-code suggests. - The pseudo-code never tells you to print a newline at the end although I agree that it should probably be there.
- Instead of having to seperate print calls
printf("\n"); printf("* %s *\n", line);you can just combine them toprintf("\n* %s *\n", line);
Other than that the code is good (one could add some checks to see whether fgets failed and whatnot, but that would be way beyond this excercise of yours)
I especially liked the usage of the macro LEN
thanks
I didn't because sizeof would be better
Okay. You have a point here.
But I don't see that much wrong here if you expect this to always be a char array (granted it could become f.e. a wchar_t one later on).
Yes, okay. I agree with you. sizeof would be better but at least using a macro like LEN is better than literal 128s and I remember really wanting to point out at least something good.
Yeah, using a macro there is totally fine. I'm mostly just splitting hairs.
For most C code going around changing a macro constant to an equivalent sizeof expression would have no effect on the generated code and would be past the point of diminishing returns from a maintainability point of view. But if there's a choice when writing new code then appropriate use of sizeof is always a joyous occasion 🙂
(Although usually it goes hand in hand with using an ARRAY_SIZE() function macro, so there's no avoiding the preprocessor entirely.)
This question is being automatically marked as stale.
If your question has been answered, run !solved.
If your question is not answered feel free to bump the post or re-ask.
Take a look at !howto ask for tips on improving your question.
!solved
You can only close threads you own
lmao
oh yeah sorry I didn't even realise that lmao
perfect thank you I forgot about tht lol
yh copied this from looking at another code, I did from past and couldn't remember wtf I am doing
lmaoooo I was having a bad day
morning either no sleep forgot everything 😂
@night solstice Has your question been resolved? If so, run !solved :)
!solved