0

why is this happening, there shouldn't be any space between 10 and the input

The input is 'Potato' and the output is showing after 10.

This is the actual code -

#include <iostream>
using namespace std;

int main()
{
    int a = 5;
    int b = 6;
    cout << endl <<a+b;
    for (int i = 0; i <= 10; ++i) {
        cout << endl << " Number " << i;
}
    string  name;
    cin >> name;
    cout << endl << endl << name;
    return 0;
}
abhi abhi
  • 1
  • 1
  • [Why should i not upload images of code/data/errors?](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors) – 463035818_is_not_an_ai Jun 21 '23 at 11:02
  • 1
    what "gap" ? You have a blank before and after `Number` and thats also present in the output. You should include expected and actual output as text in the question – 463035818_is_not_an_ai Jun 21 '23 at 11:03
  • you posted the same image twice, but anyhow you should not post images of text, post the text – 463035818_is_not_an_ai Jun 21 '23 at 11:03
  • 1
    Why are you printing the end of the line _before_ the actual text? What textbook is teaching `cout << endl << x;`? – Botje Jun 21 '23 at 11:04
  • Read about c++ a bit more and learn how you should be asking question in stack overflow a bit better. The way you're asking is very confusing.. But i think the answer might be this part of your code:```cout << endl << " Number " << i;``` where you're placing a space between ```" Number "```. remove one of the either spaces in them and youre good to go – Shreeyash Shrestha Jun 21 '23 at 11:05
  • as for the line where you said ```The input is 'Potato' and the output is showing after 10.``` , you specifically inputted ```potato``` at the end and printed at the end, and why would you expect it to print before 10? – Shreeyash Shrestha Jun 21 '23 at 11:07
  • I don't understand what your question is? For me, there is no space after the output when I run your code. I didn't use CLion so don't know about that. – Anakin Jun 21 '23 at 11:08
  • 1
    Why are you putting the `endl` (end-of-line, and flush) *before* the end of the line? Is the `endl` itself the "gap" you are asking about? – Eljay Jun 21 '23 at 11:12
  • Please [edit] and add what output you expect as text. I do not see any whitespace which cannot be explained by the present (and weirdly line-starting) outputs of whitespace in your code. Also please in the output you get (also please show as text) somehow make the "gap" visible. I recommend to give the output you get once verbatim as text and once as edited text where you e.g. replace the unwanted blanks by "." and the unwanted newlines by ":". – Yunnosch Jun 21 '23 at 11:15
  • Another reason for showing input, output, expected output (and maybe edited output) as text is that in your pictures I am unsuere what output is (the upper part, ending in potato I think) and what input is (lower part, consisting only of input I guess). To get things clear and avoid confusion by separate input and output windows of your IDE, you might want to run the executable in a separate shell/console/terminal/commandline/prompt.... – Yunnosch Jun 21 '23 at 11:18

1 Answers1

0

I dont know what you read to do these things because :

  1. nobody does this: cout << endl << " Number " << i; . You should learn using it like this: cout << " Number " << i<<endl; and if you specifically want a newline at the first, you can just putcout<<endl; or cout<<"\n";
  2. As of my understanding of your question, i think you're missing the point here. You have a space between the " Number ", which you should probably change to "Number "
  3. You specifically and explicitly took input and printed the input of 'potato' at the end. This part :
    string  name;
    cin >> name;
    cout << endl << endl << name;

is after this part:

 for (int i = 0; i <= 10; ++i) {
        cout << endl << " Number " << i;
}
  

which is obvious that the text 'potato' that you entered is printed after 10.

This code might help you:

#include <iostream>
using namespace std;

int main()
{ 
    string name;
    for (int i = 0; i <= 10; ++i) 
    {
        cout << "Number: " << i << endl;
    }
   
    cin >> name;
    cout << name;
    return 0;
}

this code shows this output:

1 
2
3
4
5
6
7
8
9
10
Potato

I hope this helps!!!

  • 2
    Without the info of the expected output (and maybe about which part in the shown output is the "gap" which OP does not like) this seems a lot of guessing and assuming. Good luck nevertheless. – Yunnosch Jun 21 '23 at 11:38