#Help with basic for loop algorithm

41 messages · Page 1 of 1 (latest)

night solstice
#

I am trying to print the following :


  • Firstname Lastname*

using the following guide, yet for some reason I cannot do it because I am stoopid 🙂 someone pls help?

covert lynxBOT
#

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.

night solstice
#

cureent code:

covert lynxBOT
#
How to Format Code on Discord
Markup

```cpp
int main() {}
```

Result
int main() {} ```
night solstice
#

` #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;

}`

merry knotBOT
#
Critical error:

No main() specified. Invalid request

covert lynxBOT
#

` ```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;
}

adhdestroyed
fluid grail
#

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.

elfin totem
#

@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;
}
merry knotBOT
#
Program Output
enter name> *******
* ugo *
*******
fluid grail
fluid grail
# elfin totem ``` #include <stdio.h> #include <string.h> #define LEN 128 int main() { ch...

Only 3 minor things:

  1. Instead of int len = strlen(line); with the condition i < len + 4 you can just do int len = strlen(line) + 4; with the condition i < len like the pseudo-code suggests.
  2. The pseudo-code never tells you to print a newline at the end although I agree that it should probably be there.
  3. Instead of having to seperate print calls printf("\n"); printf("* %s *\n", line); you can just combine them to printf("\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

elfin totem
#

thanks

tardy ember
fluid grail
# tardy ember 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.

tardy ember
#

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

covert lynxBOT
#

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.

stiff sable
#

!solved

covert lynxBOT
stiff sable
#

lmao

night solstice
night solstice
night solstice
night solstice
#

morning either no sleep forgot everything 😂

covert lynxBOT
#

@night solstice Has your question been resolved? If so, run !solved :)

night solstice
#

!solved