-1

can anyone tell where's the problem when I print out the code the first line shouldn't be empty and yet and the first line is empty and the staircase starts from the second line here's my code<

#include <iostream>
using namespace std;

int main(){
    int o ; 
    scanf("%d",&o);
    int n=o+1; 
    for(int g=0; g<n; g++){          
        for(int h=0; h < n ;h++) {
            if(g+h>=n){ cout<<"#";}
            if(g+h< n ){ cout<<" ";}
        }
        cout<<"\n";
    }
}       

Given the input "3" ENTER, here's the output:

THIS LINE IS EMPTY IN THE TERMINAL       
  #
 ##
###

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
DEEP
  • 1
  • 1
  • 1
    Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly please [edit] your question to tell us the input you give the program, together with the actual and expected output. – Some programmer dude Jun 19 '21 at 16:29
  • 2
    By the way, the code you show isn't a proper [mcve], as it will not build. Always make sure to copy-paste the exact program that causes the problem you ask about, and doesn't have other problems. – Some programmer dude Jun 19 '21 at 16:30
  • And lastly you should take this as an opportunity to both learn how to use your text-editors auto-formatting abilities (as that will show the problem leading to build errors), and how to use a *debugger*. With a *debugger* you can step through your code statement while monitoring variables and their values. – Some programmer dude Jun 19 '21 at 16:32
  • 1
    If using `scanf` function you should *always* check the result of to get aware of invalid input. Strange, though, that you mix C and C++ IO. Why not simply `if(cin >> o) { /* OK */ } else { /* error handling */ }` (note: again you should check stream state afterwards)? – Aconcagua Jun 19 '21 at 16:32
  • even if i use the cin>> i would have gotten the same result – DEEP Jun 19 '21 at 16:33
  • Side note: If outputting single characters I'd prefer character literals over string literals, i. e. `'#'`, `' '` and `'\n'`, they are more efficient as no string end needs to be detected. If compiler manages to optimise the difference away appears questionable to me, so I wouldn't rely on. – Aconcagua Jun 19 '21 at 16:35
  • 1
    You have an "off by one" error - your first line consists of three spaces. Just check your logic and maybe work through a run on paper, thinking about what the variable values are. – Tony Delroy Jun 19 '21 at 16:38
  • 1
    Please also include the *input* you give your program, which leads to the wrong output. And the expected output as well. And if you have an empty line then please leave it empty in the shown output. – Some programmer dude Jun 19 '21 at 16:38
  • Side note 2: `if(condition) { } if(complement) {}` is better written as `if(condition) {} else {}` – Aconcagua Jun 19 '21 at 16:39
  • You have chosen output that is tricky to debug. Try replacing the (invisible) space (`" "`) with something easier to see (for example, `"_"` or `'_'`). – JaMiT Jun 19 '21 at 16:40
  • As long as `g` is 0, `g + h` cannot ever get `>= n`, as `h` remains `< n`, too. For `g = 1` you will get one single hash sign for `h == n-1`, the last value of inner loop. So start with `for(int g = 1; ...)`. – Aconcagua Jun 19 '21 at 16:43
  • FYI, the proper include for `scanf` is `cstdio`. The `iostream` is for `cin`, which you are not using. – Thomas Matthews Jun 19 '21 at 17:45
  • See [Disadvantages of `scanf`](https://stackoverflow.com/questions/2430303/disadvantages-of-scanf). – Thomas Matthews Jun 19 '21 at 17:48
  • By the way, you could skip the part about mentioning your input if you replaced the two lines `int o ; scanf("%d",&o);` with the line `int o = 3;`. (Well I'd also suggest meaningful variable names, but that is a separate issue.) Keep in mind that your question should contain *example* code, which usually has trimmed-down functionality, like no longer relying on user input. – JaMiT Jun 19 '21 at 18:07
  • 1
    Change `cout << "\n";` to `cout << "|\n";` to better see what is going on. – Eljay Jun 19 '21 at 19:43

1 Answers1

-2

Try your code like this!

#include<iostream>
#include<cstdio>
using namespace std;
int main(){
   int o ;
   scanf("%d",&o);
   int n=o-1;
   for(int g=0; g<=n; g++){
        for(int h=0; h<=n ;h++){
            if(g+h>=n){ cout<<"#";}
            if(g+h< n ){ cout<<" ";}
        }
         cout<<"\n";
    }

    return 0;
 }
Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • 6
    What, aside from the gratuitous and non-portable `bits/stdc++.h`, is different in your version of the code? Yes, readers can hunt around, but you really should mention what you did and why. – Pete Becker Jun 19 '21 at 17:08
  • 2
    Please read about [how to write good answers](https://stackoverflow.com/help/how-to-answer). – Some programmer dude Jun 19 '21 at 17:30