Thread: Help w/ my code

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    4

    Help w/ my code

    i'm working on a program to receive a word of 6 letters max. then organise it like this.
    exemple: (ps.: just used - to organise the words the way i want)
    --------------------Test
    ---------------Test Test
    ----------Test Test Test
    -----Test Test Test Test
    Test Test Test Test Test

    all that i've got atm is the reverse of this, could som1 help me with this code?
    thanks!!


    Code:
    #include<stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    
    main()
    {
      int x, y, size;
    
      char name[6];
    
      printf("Type a word: ");
    
      gets(name);
      size = strlen(name);
    
      for (x = 1; x <= size; x++)
      {
        for (y = 1; y <= x; y++)
    
          printf("%s\t", name);
    
        printf("\n");
      }
    
      return 0;
    }
    Last edited by Salem; 11-21-2020 at 06:00 AM. Reason: Removed crayola

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,657
    Some initial observations.
    1. main must be specified as returning an int.
    Implicit return types became obsolete in 1989 with the first C standard.

    2. Never EVER use gets()
    gets, gets_s - cppreference.com
    There is no way to make it safe, and it's been removed from the language completely.

    3. What are x and y?
    It's much easier to figure out what to do if you have meaningful variable names.
    Code:
    #include<stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    
    int main()
    {
      printf("Type a word: ");
      char name[10];
      fgets(name,sizeof(name),stdin);
    
      // fgets will append a \n if there is room in the buffer
      // this code will remove it, if it's present.
      {
        char *p;
        if ((p = strchr(name, '\n')) != NULL)
          *p = '\0';
      }
    
      // strlen returns a size_t
      size_t len = strlen(name);
    
      for ( size_t row = 1 ; row <= len ; row++ ) {
        size_t indent = 10;
        for ( size_t spaces = 0 ; spaces < indent ; spaces++ ) {
          printf(" ");
        }
        for ( size_t words_on_row = 1 ; words_on_row <= row ; words_on_row++ ) {
          printf(" %s", name);
        }
        printf("\n");
      }
    
      return 0;
    }

    Now look at your pattern
    --------------------Test
    ---------------Test Test
    ----------Test Test Test
    -----Test Test Test Test
    Test Test Test Test Test

    Count the spaces, and discover that the sequence is 20, 15, 10, 5, 0.

    What you do now is come up with some kind of mathematical expression involving the variables you have, that would generate that sequence of numbers.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2020
    Posts
    4
    thanks for help but your code still do the same as mine, i need the code do organise the words exactly the way i wrote, is it possible to do that?
    --------------------Test
    ---------------Test Test
    ----------Test Test Test
    -----Test Test Test Test
    Test Test Test Test Test

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,323
    You should read the other things Salem wrote, not just the code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-19-2012, 01:58 PM
  2. Replies: 1
    Last Post: 03-10-2010, 11:28 AM
  3. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  4. producing c/c++ code from flowcharts,pseudo code , algorithims
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 07:09 AM
  5. Having trouble translating psudeo-code to real-code.
    By Lithorien in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 07:51 PM

Tags for this Thread