1

I'm learning about C++ and when I was learning about Class, I ran into a problem. I try cout my element before entering the value, but the element already has a value. I can't understand why?

class cusTomer
{
    char abc[30];
public:
    void input();
    void output();
};
void cusTomer::input(){
    cout<<"abc: "; fflush(stdin); gets(abc);
}
void cusTomer::output(){
    cout<<"abc: "<<abc<<endl;
}
class proDuct
{
    char name[30];
public:
    void input();
    void output();
};
void proDuct::input()
{
    cout<<"Name: "; fflush(stdin); gets(name);
}
void proDuct::output()
{
    cout<<"Name: "<<name<<endl;
}
class bill
{
    char date[30];
    proDuct *y;
    int n;
public:
    void input();
    void output();
};
void bill::input()
{
    cout<<"Date: "; fflush(stdin); gets(date);
    cout<<n<<endl; //16
    cout<<"Input n: "; cin>>n;
    cout<<n<<endl;
    y=new proDuct[n];
    for(int i=0;i<n;i++){
        y[i].input();
    }
}
void bill::output()
{
    cout<<"Date: "<<date<<endl;
    for(int i=0;i<n;i++){
        y[i].output();
    }
}
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • 1
    What would you expect to print? – Fred Larson Jul 14 '21 at 19:29
  • 2
    Reading from uninitialized memory is undefined behavior – Kevin Jul 14 '21 at 19:29
  • 3
    BTW, [never use `gets()`!!](https://stackoverflow.com/q/1694036/10077) – Fred Larson Jul 14 '21 at 19:30
  • 4
    Uninitialized variables have *indeterminate* value. Using such values is, as mentioned, undefined behavior. – Some programmer dude Jul 14 '21 at 19:30
  • my teacher taught me that get() should be used when using char – Nhật Hoàng Jul 14 '21 at 19:33
  • You are not using `std::istream::get()` anywhere though. You use a really fragile function called `std::gets()` which has been removed from the language (in C++14) – Ted Lyngmo Jul 14 '21 at 19:34
  • 2
    [I love what the man page for `gets`](https://man7.org/linux/man-pages/man3/gets.3.html) has to say about `gets`: *Never use `gets()`.* It's not often that the documentation actively recommends not using what's being documented. Usually it focuses on how to avoid the problems. – user4581301 Jul 14 '21 at 19:34
  • Your teacher have apparently taught you about `cout`, but what about **`cin`**? That's the normal way to read input in C++. – Some programmer dude Jul 14 '21 at 19:35
  • I'm sorry, I have just edit my code and I didn't still understand how to use stackoverflow clearly – Nhật Hoàng Jul 14 '21 at 19:35
  • @NhậtHoàng the secret to posting good-looking code is to place three backticks (`\`\`\``) at the beginning of an otherwise empty line before and after the code. – user4581301 Jul 14 '21 at 19:38
  • I was taught about gets() and cin.getline(), but I thought that both of it can use when using char – Nhật Hoàng Jul 14 '21 at 19:39
  • 1
    They can be, but `gets` has unfixable bugs--there is no way to prevent [buffer overflow](https://en.wikipedia.org/wiki/Buffer_overflow) because it will not stop reading and storing until it finds the end of the line regardless of when the program will run out of space in the buffer--and this has been known for decades. Anyone recommending its use is behind the times to the point of being negligent. – user4581301 Jul 14 '21 at 19:43
  • So, for the next codes, what should I change ? my class is taught that fflush(stdin) to clear the buffer memory, then gets()/cin.getline() to input a string. And when start a code, we often use "using namespace std;" I don't really understand about it. – Nhật Hoàng Jul 14 '21 at 19:54
  • `fflush(stdin)` also gives undefined behaviour. It only has defined behaviour for OUTPUT streams. – Peter Jul 14 '21 at 19:55
  • In that case, I recommend supplementing your instructors with [a good book or two](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). They will cover the use and pitfalls of `using namespace std;` and offer up sound wisdom and useful programming idioms. For the rest, you have to read documentation. For example, here's [some good documentation for `fflush`](https://en.cppreference.com/w/cpp/io/c/fflush). In this case the **Notes** section explains when `fflush` is safe to use on an input stream, but because it isn't always safe your portability will be reduced. – user4581301 Jul 14 '21 at 19:56
  • I didn't still understand the notes, it means that fflush() didn't discard buffers in input ? – Nhật Hoàng Jul 14 '21 at 20:12
  • 1
    The notes say that a POSIX-compliant system [extends the behaviour of `fflush`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/fflush.html) and older Microsoft libraries also extended the specification to specify the behaviour for input streams, but that Microsoft removed the extension in 2015. It's because it SOMETIMES works that many people think it ALWAYS works. – user4581301 Jul 14 '21 at 21:26
  • ok i got it, thanks for your help. – Nhật Hoàng Jul 15 '21 at 03:15

1 Answers1

2

but the element already has a value.

An integer always has a value. There is no concept of valueless integer in C++.

If you haven't initialised an integer, then that value is indeterminate. If you read an indeterminate value, then the behaviour of the program is undefined. You should never read an indeterminate value.

P.S. gets is no longer part of C++, and it should never have been used.

my class is taught that fflush(stdin) to clear the buffer memory, then gets()/cin.getline() to input a string

Your class has been taught bad things.

eerorika
  • 232,697
  • 12
  • 197
  • 326