Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So I am making a program which makes a star pattern while giving you a number of rows to enter , Example:

1- enter 2 rows :

*
**

2- enter 3 rows :

*
**
***

3- enter 4 rows :
*
**
***
****

For each next line the number of stars increase by one I did a program written in C which does that but it failed ("I know I know my code might seem horrible but please bear with it I am still a beginner XD)

What I have tried:

C++
#include <stdio.h>
int main() {
int i = 0;
int b = 1;
int r;
int o = 0;
scanf("%d",&r);
int c = (r + b) - r;
while ( i < c) {
if (c == r) {
return 0;
}
while( o < c ) {
printf("*");
}
printf("/n");
i++;
b++;
c = (r + b) - r;
}
}
Posted
Updated 23-Apr-19 11:51am
v2

Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
C++
#include <stdio.h>
int main() {
    int i = 0;
    int b = 1;
    int r;
    int o = 0;
    scanf("%d",&r);
    int c = (r + b) - r;
    while ( i < c) {
        if (c == r) {
            return 0;
        }
        while( o < c ) {
            printf("*");
        }
        printf("/n");
        i++;
        b++;
        c = (r + b) - r;
    }
}

Indentation style - Wikipedia[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
Noor Shiha 23-Apr-19 18:07pm    
Thank you so much Patrice I am seeing a video on how to use the debugger in Codeblocks :) By the way this community is amazing!!!
Patrice T 23-Apr-19 18:15pm    
Thank you
Quote:
C
while( o < c ) {
    printf("*");
}
An infinite number of stars ...
 
Share this answer
 
Comments
Noor Shiha 23-Apr-19 17:46pm    
Ah stupid me XD
Noor Shiha 23-Apr-19 17:50pm    
This is why I should never read my code when I am sleepy :P
k5054 23-Apr-19 17:52pm    
You're welcome.
You should probably make friends with your debugger, an indispensable tool for any programmer. Also, think carefully about your variable name - using single letter variables has its place, but normally you'd want to use something that conveys information about what the variable's intended use is. If you need to review your code several months in the future you might be scratching your head wondering what the variable 'o' is supposed to be doing for you.
Noor Shiha 23-Apr-19 18:33pm    
Sorry internet is slow that's why I was late for reply thank you very much for your answer :D I am seeing a video on how to use the debugger in Code blocks :P
Why are you learning C in 2019? Where do you intend to use it?

I'm not sure why you've tied yourself in knots with all this code. This works:

#include <stdio.h>
int main() {
int r;
scanf("%d",&r);

for(int i = 0; i < r; ++i)
{
printf("*");

}

printf("\n");

return 0;
}
 
Share this answer
 
Comments
Noor Shiha 23-Apr-19 17:48pm    
My prof on youtube uses it XD , by the way thanks for the code :D , I did that code you saw there because I was trying to create an algorithm myself :P
Christian Graus 23-Apr-19 17:53pm    
Why are you learning C though? It's complex and barely used.
Noor Shiha 23-Apr-19 18:39pm    
The course that I am seeing is called "Problem Solving through C" As I wanted to hone my problem solving skills and the course turned out to be amazing , When I finish the course I will see an Algorithms course which uses Python!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900