0
#include<bits/stdc++.h>
using namespace std;

int main()
{
    char *one;
    one = new char[1];
    cin.getline(one, 5);
    cout<<one<<endl;
    return 0;
}

This should allocate 1 byte space or 1 character space to the character pointer *one. Now I am taking 5 character input using cin.getline, Why does it output first 4 characters only instead of 5? and why it does not output only 1 character since I have allocated it only 1 character space?

I tried using

for(int i = 0; one[i] != '\0'; ++i) cout<<one[i];

and it does the same thing, it outputs the first 4 characters of my input, not 5 characters. I want it to store only 1 character because I have allocated it only 1 byte space.

Aman Jain
  • 13
  • 3
  • 1
    I think you're confused. `getline` does NOT reallocate your char array. It doesn't even know how it was allocated. Use `std::string` instead, or allocate enough bytes to store characters AND the string terminator. – paddy Oct 07 '20 at 07:55
  • 2
    Further, please avoid `` and `using namespace std`. The first is non-standard, the second is bad practice. – paddy Oct 07 '20 at 07:56
  • @paddy Why? In my books using namespace std is used in almost every program and my teachers also use it. I have a habit of using because it contains sorting, unordered_map and set functions which are very useful in competitive programming? Should I include these libraries separately? Why so? – Aman Jain Oct 07 '20 at 08:02
  • 1
    You encountered Undefined Behaviour. "Undefined" means it can anything, including "seeming working correctly", "formatting your C drive" or "[making demons fly out of your nose](http://catb.org/jargon/html/N/nasal-demons.html)" – Yksisarvinen Oct 07 '20 at 08:03
  • 4
    @AmanJain paddy is right, both should be avoided, see here: [Why should I not `#include `](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). [Why is `using namespace std;` considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Lukas-T Oct 07 '20 at 08:04
  • 1
    [using namespace std](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) brings *thousands* of names into scope, particularly when you include gcc's "everything std" header – Caleth Oct 07 '20 at 08:05

1 Answers1

1

Why does it output first 4 characters only instead of 5?

Because your program has undefined behaviour.

And also because getline(char * s, std::streamsize count) reads at most one fewer than count characters from the stream, appending a null character to whatever it wrote.

I want it to store only 1 character because I have allocated it only 1 byte space.

You can't use cin.getline to do that. There must be space for a terminating null character.

#include <iostream>

int main()
{
    char* one = new char[1];
    std::cin.get(*one);
    std::cout << *one << std::endl;
    return 0;
}

Aside: The dynamic allocation seems supurfluous here. Why not char one;?

Caleth
  • 52,200
  • 2
  • 44
  • 75
  • Thanks. I do not really understand this undefined behaviour thing happening with my code. I was trying to create a String class by myself and implement concatenation and size functions in it that is why I was trying to take input by using char string. – Aman Jain Oct 07 '20 at 08:24