1

So I wanted to write a program which prints out a pyramid made out of "O", whose height is given by user input.

#include <stdio.h>

int main()
{

    int n, i, j, k;
    scanf_s("%d", &n);
    {
        for (i = 1; i <= n; i++)
        {
            for (j = 1; j <= n - i; j++)
            {
                printf(" ");
            }
            for (k = 1; k <= 2 * i - 1; k = k + 1);
            {
                printf("O");
            }
            printf("\n");
        }
        return 0;
    }
}

I'm a complete beginner, so if you have any advice, please do offer it. Anyways, I tried running it on a compiler on Android; seemed to work, printed out the pyramid.

Tried it on Microsoft Visual Studio. The command line opens, but after I put in the number and press "enter", the whole window just closes without giving me anything. How do I prevent this? Programs that don't need user input seem to run just fine.

Alexis Wilke
  • 15,168
  • 8
  • 60
  • 116

5 Answers5

3

For your described problem: Preventing console window from closing on Visual Studio C/C++ Console application

For your code, there seems to be a mistake, and currently your code will not produce a pyramid, but a slash of O. To solve this problem:

for (k = 1; k <= 2 * i - 1; k = k + 1); remove the ; from this line. Why this solved the problem?

When there is a ; after the loop, it means that the loop does nothing, and then the next three lines are:

{
    printf("O");
}

Which means that there are only a single O prints out, instead of printing it in a loop.

CoralK
  • 3,341
  • 2
  • 8
  • 21
  • 1
    That mistake won't prevent *some* kind of output though - one `'O'` on each line. *...the whole window just closes without giving me anything* – Weather Vane Sep 20 '19 at 19:44
  • The program prints `n` lines, each with one `'O'` with leading spaces. This does not answer the question, although that is a fault. – Weather Vane Sep 20 '19 at 19:48
  • @WeatherVane I missed the question, answer fixed. Thanks! – CoralK Sep 20 '19 at 19:50
1

Apart from the semicolon after the for (k = 1... loop, you have no bug in this code; if Visual Studio closes, the issue is with that program. (It could well just be closing because the program has finished execution, but I don't know that program).

Since you write that you are a complete beginner and you would appreciate advice, I'll offer some stylistic comments. But these are just comments on how I would do things differently if I were you, I am not saying that what you have is wrong.

  • Single-letter variable names: these will bite you. It is really easy to mix up i, j, and k since they are all kind of meaningless indexes. When your programs become more complex you will be happy to have meaningful variable names. Also, if you are trying to locate instances of a variable, searching for 'i' is a lot harder than searching for "spaces" or "spc"
  • Code block under scanf_s(): There is no reason to have this code inside a block--all that does is shift the internal code one tab right. Screenspace is precious.
  • You will likely find it more useful to iterate from 0 than from 1, as you will be using your iterator variable as an array index a whole lot. Getting into the habit of writing your loops as for (dex = 0 ; dex < max_val ; dex++) will serve you well. Note well that the comparison is "dex < max_val" and not "dex <= max_val"
  • The "k = k + 1" seems bad to me--people reading your code will need to stop and try to figure out why it's not just "k++" which what people are expecting and what you use in the other two loops. (And even for incrementing-by-more-than-one I'd expect something like "k += 2" not "k = k + 2")
Jabberwock
  • 1,037
  • 9
  • 13
  • Thank you for the advice. I wrote k=k+1, because I thought maybe shortening it wasn't allowed in the instance or sth. About scanf, I didn't really understand why, but Visual Studio told me not to use normal "scanf" but instead use scanf_s, or the program won't launch. Would you be able to explain why? – Chinguun Erdenebadrakh Sep 20 '19 at 20:27
  • Sorry, I have no idea. If Microsoft supports scanf(), I would just use that so the code is portable (ie., runs anywhere). But my background is all systems and embedded coding, so I have never had to use Windows as a development environment. When I write code at home, I write on a virtual Linux machine hosted on my Windows computer (using VirtualBox--a free program). – Jabberwock Sep 20 '19 at 22:15
  • @ChinguunErdenebadrakh please [read this](https://stackoverflow.com/a/58006091/4142924). As to *why* Microsoft warns you about using standard C library functions, you'll have to ask them. MSVC *does* support `scanf` but they think you should not use it. – Weather Vane Sep 21 '19 at 17:23
1

Once console application returns from main method, the associated console window closes automatically. For Windows OS add a system("pause"); before your return 0; statement. For platform independent solution you can just show a prompt to user and wait for a key press before returning from main. Any character remaining in input buffer (enter from scanf in this case) must be cleared.

int main()

{
   .........
   .........
   //clear input buffer
   int d;
   while ((d = getchar()) != '\n' && d != EOF) { }
   printf("Press ENTER key to Continue\n");  
   getchar(); 
   return 0;
}
Imran Rana
  • 11,447
  • 7
  • 42
  • 51
0

You should include conio.h header file in your program and then simply place getch(); after your program's last cout statement.

I think that this would help the window from closing it worked for me ;)

0

Put a cin command(C++) or a C equivalent at the very end just before return 0;, so it wont close. :> Eg:

.
.
.
.
int control; cin>>control;
return 0;
}