Reputation: 131
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.
Upvotes: 1
Views: 3908
Reputation: 1
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;
}
Upvotes: 0
Reputation: 1
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 ;)
Upvotes: 0
Reputation: 11889
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;
}
Upvotes: 1
Reputation: 1291
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.
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"Upvotes: 1
Reputation: 3506
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.
Upvotes: 3