someone please run my code and tell me what's wrong with it

closed account (1vf9z8AR)
weird symbols come in the output along with the intended output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>
#include<string.h>
using namespace std;
class sales
{
private:
        char name[80];
        float pay;
public:
        sales(char *s,float p)
        {
                strcpy(name,s);
                pay=p;
        }
void outdata()
        {
            cout.write(this->name,80);
            cout<<" has invoked 'outdata()'"<<endl;
        }
};
int main()
{
        sales s1("Raman",5000),s2("Sita",2000);
        s1.outdata();
        s2.outdata();
        return 0;
}


PS- i must use 'this'.
Last edited on
The write() function is trying to write 80 bytes (characters) to the console but you only have about 6 characters available to write(), I would recommend just using the output operator<< instead of write().

closed account (1vf9z8AR)
it works with cout<<

i am getting this warning:
F:\c++projects\thispointer\pointerthis.cpp|23|warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]|

replacing char with string doesn't solve the issue.Should i just ignore this warning?
Line 23 has:
sales s1("Raman",5000)
and warning mentions
conversion from string constant to 'char*' 

The constructor's declaration is
sales::sales(char *s,float p)

The first argument (s) is indeed 'char*'.

The constructor is called with value "Raman", a literal string constant and its type is thus const char*

The constructor has no intent to modify the s. Make it clear:
sales::sales(const char *s,float p)
Topic archived. No new replies allowed.