Thread: Rare printing (noob here, be patient)

  1. #1
    Registered User CatBrownie's Avatar
    Join Date
    Apr 2014
    Location
    Costa Rica
    Posts
    6

    Question Rare printing (noob here, be patient)

    Ok, I'm using Ubuntu with gcc to compile C programs and I'm doing a simple supermarket inventory tool. Everything's fine, but look at the printing in the option 2.

    Rare printing (noob here, be patient)-rareprinting-png

    I run option one of the switch to add articles and their quantity, the second option has to print each articles and how many we have. But, look at the rare, tab? printing.

    This is the line that got a problem.
    Code:
    printf("1.%79s = %d.2. %79s = %d.3. %79s = %d.", nart1, art1, nart2, art2, nart3, art3);
    What should I do? The "79s" printing is the problem? Thanks in advance.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    signed int art1, art2, art3, opt;
    char nart1[20], nart2[20], nart3[20], answer, user[10];
    printf("\a\7\n");
    printf("Digite su nombre, por favor\n");
    scanf("%79s", user);
    system("clear");
    do {
        printf("Bienvenido %s, digite una opción.\n 1. Agregar artículos. \n 2. Ver artículos con sus existencias. \n 3. Hacer movimientos de inventario. \n 4. Salir.\n", user);
        scanf("%d", & opt);
        switch(opt) {
        case 1:
        printf("¿Cómo se llama el artículo 1?\n");
        scanf("%79s", nart1);
        printf("¿Cuántos hay del artículo 1?\n");
        scanf("%d", & art1);
        printf("¿Cómo se llama el artículo 2?\n");
        scanf("%79s", nart2);
        printf("¿Cuántos hay del artículo 2?\n");
        scanf("%d", & art2);
        printf("¿Cómo se llama el artículo 3?\n");
        scanf("%79s", nart3);
        printf("¿Cuántos hay del artículo 3?\n");
        scanf("%d", & art3);
        break;
        case 2:
        if(nart1 && nart2[0] == '\0') {
        printf("Por favor ingresa valores antes de pedir mostrarlos");
        break; }    
        printf("1.%79s = %d.2. %79s = %d.3. %79s = %d.", nart1, art1, nart2, art2, nart3, art3); 
        break;
        case 3:
        /* ........, this is the worst part */
        case 4:
        exit(0);
    }
        printf("¿Desea continuar?\n");
        scanf("%79s", & answer);
    
    }while(answer == 's' || answer == 'S');
    return 0;
    }
    Attached Images Attached Images Rare printing (noob here, be patient)-printing-png 
    Last edited by CatBrownie; 04-18-2014 at 07:35 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    It appears to be all on one line, but because the line is longer than the terminal is wide, it "wraps around", so it looks like multiple lines with extra tabs. As you suspect, the %79s is the problem (or at least part of it)

    Take a look at this string:
    Code:
    printf("1.%79s = %d.2. %79s = %d.3. %79s = %d.", nart1, art1, nart2, art2, nart3, art3);
    That says print a "1.", then a string taking up 79 spaces (even if it's shorter), then print " = " then print an integer. It repeats that for "2." and "3.". You need to put a new line in between each item. Also, use a smaller number for your string width. 30 is probably sufficient. Better yet, make 3 printf statements:
    Code:
    printf("1. %30s = %d\n", nart1, art1);
    // same for 2. and 3.
    Notice the "\n" at the end. Since you can't put a literal new line (like hitting enter) in the string, you have to use an escape sequence. This is in just about every decent textbook and tutorial, and even most crappy ones. I suggest you find some decent study materials and read through the first few chapters, and try the examples.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-27-2011, 10:56 PM
  2. printing bytes from memory (NOOB question)
    By MishaMish in forum C++ Programming
    Replies: 7
    Last Post: 01-26-2011, 05:46 AM
  3. Debugging a rare / unreproducible bug..
    By g4j31a5 in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 08-05-2008, 12:56 PM
  4. Patient Structure Program - Help?
    By Help_me0318 in forum C++ Programming
    Replies: 3
    Last Post: 10-18-2006, 03:00 PM
  5. Help required from generous & patient souls!
    By Kremlin in forum C Programming
    Replies: 1
    Last Post: 06-30-2002, 11:47 PM

Tags for this Thread