#Hod do I output the text more like a menu

34 messages · Page 1 of 1 (latest)

heady moat
#

This is all I have so far don’t judge I’m new to c this is just a function that prints out the lengths you can convert it

void length() {
  int choice;
  printf("1.  In -> Cm   ");
  printf("2. Cm -> In\n");
  printf("3.  In -> Ft   ");
  printf("4. Ft -> In\n");
  printf("5.  Cm -> Ft   ");
  printf("6.Ft -> Cm\n");
  printf("7.  Cm -> M   ");
  printf("8.M -> Cm\n");
  printf("9.  In -> M   ");
  printf("10.M -> In\n");
  printf("11. Ft -> M   ");
  printf("12.M -> Ft\n");
  printf("13. Yrd -> M   ");
  printf("14.M -> Yrd\n");
  printf("15. Yrd -> Ft   ");
  printf("16.Ft -> Yrd\n");
  printf("17. M -> Km   ");
  printf("18.Km -> M\n");
  printf("19. Ft -> Km   ");
  printf("20.Km -> Ft\n");
  printf("21. M -> Mi   ");
  printf("22.Mi -> M\n");
  printf("23. Km -> Mi   ");
  printf("24.Mi -> Km\n");
  printf("25. Ft -> Mi   ");
  printf("26.Mi -> Ft\n");
  printf("27. Back\n");
  printf("27. Exit\n");
  printf("Enter your choice: ");
  scanf("%d", &choice);
 }```
pulsar jacinthBOT
#

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 use !howto ask.

fading egret
#

Hey, I answered you in #c-help-text

#
void length() {
  int choice;
  printf("1.  In -> Cm   "
         "2. Cm -> In\n"  
         "3.  In -> Ft   "
         /* ... */         )
  printf("Enter your choice: ");
  scanf("%d", &choice);
 }

#

You can omit the printf calls that are only text.

#

@heady moat

heady moat
#

I mean the formatting of the output

#

Like less cluttered

fading egret
#
void length() {
  
  printf("1.  In -> Cm   "
         "2. Cm -> In\n"
         "3.  In -> Ft   "
         "4. Ft -> In\n"
         "5.  Cm -> Ft   "
         "6.Ft -> Cm\n"
         "7.  Cm -> M   "
         "8.M -> Cm\n"
         "9.  In -> M   "
         "10.M -> In\n"
         "11. Ft -> M   "
         "12.M -> Ft\n"
         "13. Yrd -> M   "
         "14.M -> Yrd\n"
         "15. Yrd -> Ft   "
         "16.Ft -> Yrd\n"
         "17. M -> Km   "
         "18.Km -> M\n"
         "19. Ft -> Km   "
         "20.Km -> Ft\n"
         "21. M -> Mi   "
         "22.Mi -> M\n"
         "23. Km -> Mi   "
         "24.Mi -> Km\n"
         "25. Ft -> Mi   "
         "26.Mi -> Ft\n"
         "27. Back\n"
         "27. Exit\n"
         "Enter your choice: ");

  int choice;
  scanf("%d", &choice);
 }```
fading egret
#

I think that already looks good.

heady moat
#

I want it kinda like tables so all the 2nd colum options are evenly spaced out

#

You get what I’m trying to say

fading egret
#

Can you show me the text how you want it to look?

#
void length() {
  
  printf("1.  In -> Cm    2. Cm -> In\n"
         "3.  In -> Ft    4. Ft -> In\n"
         "5.  Cm -> Ft    6. Ft -> Cm\n"
         "7.  Cm -> M     8. M  -> Cm\n"
         "9.  In -> M    10. M  -> In\n"
         "11. Ft -> M    12. M  -> Ft\n"
         "13. Yrd -> M   14. M  -> Yrd\n"
         "15. Yrd -> Ft  16. Ft -> Yrd\n"
         "17. M -> Km    18. Km -> M\n"
         "19. Ft -> Km   20. Km -> Ft\n"
         "21. M -> Mi    22. Mi -> M\n"
         "23. Km -> Mi   24. Mi -> Km\n"
         "25. Ft -> Mi   26. Mi -> Ft\n"
         "27. Back       27. Exit\n"
         "Enter your choice: ");

  int choice;
  scanf("%d", &choice);
 }```
#

Like this?

heady moat
#

Let me try this sorry for the slow typing I’m on my phone

fading egret
#

How many columns do you want to have on your table menu?

heady moat
#

Yes

#

How would I go about adding 3 instead of 2

fading egret
#

Okay so:

        column1 |  col-2 | col-3  | 
        ---------------------------
row1:   |option1| option2| option3| 
row2:   |option4| option5| option6|
#

You just have to write your strings from the printf call in a way that would align like this.

#

I don't think there are format specifiers that will align that for you.

#

My recommendation would be to use a text editor that has equal widths for each letter.

#

Discord allows you to type within the box with equal widths, so you can manually align your text.

fading egret
#

Within the box all symbols have equal widths.

heady moat
#

Okay I thought there was a built in text function that could make tables it’s in one of the first programs of the book C the programming language v2

fading egret
viscid mauve
#

there is the tab escape character that would put a tab gap between each column \t, but note that i didn’t really read this thread and don’t know what your trying to do.
There is also format specifiers in printf like %10s or %-10s –– these have something to do with spacing but i haven’t used them enough to work it out myself.

fast panther
#

You can use tab characters, but that would be a little bit terminal/OS dependent. Should work. You can also use string formatting, but then you would have to change the way you structure your code... Like @viscid mauve said, you'd have to use format specifiers, but then you must replace the raw string by a format string, i.e.,

#include <stdio.h>

int main(int argc, char **argv) {
        printf("%3s %10s\t%3s %10s\n",
                "1.", "In -> Cm", "2.", "Cm -> In");
        return 0;
}

If you observe the output, you'll see it won't change much from what you're already getting. If you're on an Unix environment, you can look for the printf reference for formatting details by running $ man 3 printf, there you'll find a full documentation on formatting. In your example you have spaced them manually, but you could avoid it and even make it somewhat automatic, like i.e.:

#
#include <stdio.h>

struct menuentry {
        int id;
        char *from;
        char *to;
};

struct menuentry options[] = {
        { .id = 1, .from = "In", .to = "Cm" },
        { .id = 2, .from = "Cm", .to = "In" },
        { .id = 3, .from = "In", .to = "Ft" },
        { .id = 4, .from = "Ft", .to = "In" },
        { .id = 5, .from = "Cm", .to = "Ft" },
        { .id = 6, .from = "Ft", .to = "Cm" },
        { .id = 7, .from = "Cm", .to = "M" },
        { .id = 8, .from = "M", .to = "Cm" },
        { .id = 9, .from = "In", .to = "M" },
        { .id = 10, .from = "M", .to = "In" },
        { .id = 11, .from = "Ft", .to = "M" },
        { .id = 12, .from = "M", .to = "Ft" },
        { 0x00, NULL, NULL }
};

int main(int argc, char **argv) {
        struct menuentry *cur = options, *next = cur+1;
        char cat1[10], cat2[10];

        printf("-------------------------------\n");
        while (cur && next && cur->id != 0x00 && next->id != 0x00 ) {
                snprintf(cat1, 9*sizeof(char), "%s->%s", cur->from, cur->to);
                snprintf(cat2, 9*sizeof(char), "%s->%s", next->from, next->to);
                printf("| %2d. %8s | %2d. %8s |\n", cur->id, cat1, next->id, cat2);
                cur += 2;
                next = cur+1;
        }
        printf("-------------------------------\n");

        return 0;
}

It's ugly IMO, but well, that's closer to your desired "table" format.

You can also explore more sophisticated libraries like ncurses if you want a really better formatting, adjusted to the number of columns available in the terminal and such, but that's a whole new complexity level to handle!

fading egret
fast panther