TheUnriashol TheUnriashol - 23 days ago 10
C Question

C - Pattern Printing within a Switch Menu

I am new with C but have to do a project for school. I want to print out a menu in which the user selects a pattern which then gets printed out. It should be something like this:

Program running

But as you can see when I select the pattern for a second time it does not show the pattern. It only shows a blank space. Here is my code so far:

#include <stdio.h>
#include <stdlib.h>

int main() {
int numerodefilas;

printf("Ingrese la cantidad de filas/longitud del objeto:\n");
scanf("%d", &numerodefilas);

int fila, columna, eleccion;
int numerodecolumnas = numerodefilas;

do {
printf("1: Triangulo\n");
printf("2: Piramide\n");
printf("\nSalir\n");
scanf("%d", &eleccion);

switch (eleccion) {
case 1:
for (fila = 1; fila <= numerodefilas; fila++) {
for (columna = 1; columna <= numerodecolumnas; columna++) {
printf("X");
}
printf("\n");
numerodecolumnas--;
}
break;
case 2: /* Second choice */
break;
case 3:
system("cls");
printf("Adios!\n");
break;
default:
printf("No seleccionaste una opciĆ³n correcta!");
break;
}
} while (eleccion != 3);

system("PAUSE");
return 0;
}


The identifiers are in spanish, since I have to do it in spanish. Just a quick explanation about the names:


  1. numerodefilas = numberoflines

  2. numerodecolumnas = numberofcolumns

  3. fila = line

  4. columna = column

  5. eleccion = choice

  6. "Ingrese la cantidad de filas/longitud del objeto" = Enter the amount of lines/longitud of the object"

  7. Triangulo = Triangle

  8. Piramide = Pyramide

  9. Adio = Bye



I don't know if somebody can still help me? If you need more explanations just let me know! Am I doing something wrong? Is there something wrong with the pattern printing? I would really appreciate any help at all.

Answer

Please correct me if I am wrong.

Your problem may be caused by the fact that you do not reset the columns to their default value ?

You substract the columns with numerodecolumnas--; which works the first time but the second time the value is already 1 or 0 and therefore your loop might not work .