« first day (583 days earlier)      last day (2494 days later) » 

10:52
can someone plz help me a bit with my code?
@Ven i am posting it... see if u may help
for ( i = 0; i < n; i++)
{
for ( j = 1; j < 5; j++)
{
marks[i][7] += marks[i][j];
}
}

std::cout << std::endl;

for ( i = 0; i < n ; i++)
{
cout<<marks[i][7];
std::cout << '\n';
}

std::cout << std::endl;
Ven
Ven
@tatan How can you be so absolutely terrible and stupid?
I just don't get it. I literally said "it's not this room" and I gave you the name. Why are you posting this here?
@Ven Why exactly?
Ven
Ven
@tatan I just explained why.
@Ven Ok .... I am sorry
Can someone help me a bit with my code?
11:11
Hello people
Does anyone know mechanisms of binding?
It should work if I can make this in constructor for a class, so suppose that, which is not working
why?
class A { public: int m_a; int m_b; }class B { public: char m_a; char m_b; B() {} }
class B : public A
B::B() { m_a = 100; m_b = 100; m_a = &m_a; m_b = &m_b; }
nwp
nwp
@LXSoft You have to specify that in the definition of B. You can't add it later.
@tatan Maybe. You can post it on coliru.stacked-crooked.com and maybe someone can help you with it.
(and post a link here)
@nwp isn't that an online compiler?
nwp
nwp
Yes. Very useful to show compilation errors.
11:27
@nwp Actually its logical error
Can I post it here?
nwp
nwp
If it's not too long. Also after you paste it press CTRL+K before sending it so that it uses code formatting.
#include <iostream>
#include <string.h>
using namespace std;

int main()
{
    int n,i,j,k,m_phy=0,m_chem=0,m_maths=0,m_eng=0,m_csc=0,hst=0,total=0;
    char s_tp[5][50];
    cout<<"Enter the number of students:";
    cin>>n;
    char name[n][100];
    for(i=0;i<n;i++)
    {
        cout<<"Enter the name of student "<<i+1<<": ";
        cin>>name[i];
    }
    /*for(i=0;i<n;i++)
    {
        for (int j = 0; j < strlen(x[i]); ++j)
        {
            cout<<x[i][j];
        }
        cout<<"\n";
@nwp Is it too long?
nwp
nwp
Would have been better on coliru but it's ok.
Did they teach you that in a class?
1 sec plz
#include <iostream>
#include <string.h>
using namespace std;

int main()
{
    int n,i,j,k,m_phy=0,m_chem=0,m_maths=0,m_eng=0,m_csc=0,hst=0,total=0;
    char s_tp[5][50];
    cout<<"Enter the number of students:";
    cin>>n;
    char name[n][100];
    for(i=0;i<n;i++)
    {
        cout<<"Enter the name of student "<<i+1<<": ";
        cin>>name[i];
    }

    int marks[n][10];
    for(i=0;i<n;i++)
    {
    	for(k=0;k<6;k++)
    	{
    		if(k==0)
			{cout<<"Enter 3 digit ID of  ";puts(name[i]);
nwp
nwp
This is not what C++ is supposed to look like, you have C with cout. old C at that.
Forgot the CTRL+K. You can press the up arrow to edit it.
11:33
I am sorry again... forgot to press ctrl+k
@nwp Thanks
@nwp It wasn't really taught at class... I am trying out with a short project myself
nwp
nwp
That's good. That gives you the chance to learn it properly.
Ideally you would read a book.
To improve your code you should first decide if you want to use C or C++. They do things differently and you are mixing them inappropriately.
@nwp I am sorry about that
Would definitely try to improve next time
I am specifically using C++
nwp
nwp
Cool. So first get rid of the .h in #include <string.h>. C++ standard headers have no ending.
Then you should move the declaration of variables as far down as possible. Only declare variables when you need them.
@nwp Now,I am having a problem in the last part... a short overview of the project is- I am inputting the number of students, ID and marks in 5 subjects, printing the subject highest marks among all students and lastly the total marks of all students... Now,I am having trouble with printing the total of each student. I have written the above code (the last part for total of each student). I want first six columns of each row in my marks matrix to store the id and marks in 5 subjects..(cont.)
nwp
nwp
We'll get to that when the code looks more reasonable.
11:41
(cont.) In the 7th column I want the total of 5 subjects of each students... then I get some erroneous output... I think I should post this as otherwise I would myself forget my issue
@nwp Ok
sure
How can I make it more reasonable?
nwp
nwp
Next, you should use std::string instead of char [].
The .h and moving of variable declarations for starters.
You mean I should use std::string [n][100] instead of char [n][100]?
nwp
nwp
No, You should be using std::vector<std::string>.
@nwp As for variable declaration,I know,it has been quite a mess.. would clear it definitely at last
nwp
nwp
C arrays have weird problematic behavior.
11:44
@nwp Sorry... but I have not yet been taught vectors... I know arrays, matrices and structures
nwp
nwp
That's fine. You need #include <vector> to get it and then you can use std::vector<std::string> names; for example.
ok... i do that
next?
nwp
nwp
std::vector is basically a list of stuff. In this case a list of strings. You can put stuff into it with names.push_back("Hi"); for example.
But first we need to read the name. Something like std::string name; std::cin >> name; names.push_back(name); would be ok to start with.
ok
nwp
nwp
Note that you don't need all those numbers anymore and the program doesn't crash anymore if the name is longer than 100-ish characters.
11:48
ok
nwp
nwp
You could decide to also get rid of making the user specify how many names there will be and just read names until the user just enters nothing to show that they are done with entering names.
how can the user enter nothing?
nwp
nwp
By just pressing enter.
@nwp what is names.push_back(name); ?
nwp
nwp
Which makes you get an empty string.
11:53
do I have of declare it anywhere?
nwp
nwp
@tatan That basically says "put the 'name' into the list 'names'".
Does the thing look this this-
#include<iostream>
#include <vector>
int main()
{   int names
    std::string name;
    std::cin >> name;
    names.push_back(name);
}
nwp
nwp
your names is an int. It should be a std::vector<std::string>.
Also it need to #include <string>.
But other than that yeah, that's pretty much it.
#include<iostream>
#include <vector>
#include <string>
int main()
{   std::vector<std::string>;
    std::string name;
    std::cin >> name;
    names.push_back(name);
}
ok?
nwp
nwp
Forgot the names. Instead of int names; you use std::vector<std::string> names;.
std::vector<std::string> is a type that basically means "list of strings" and names is the name of the list.
Maybe a bit confusing. You could name it names_list instead if you want.
11:56
ok
next?
nwp
nwp
That should already compile. Except you can't see anything yet.
let me try
nwp
nwp
You can std::cout << name; to see what you entered and std::cout << names[0]; to get the same thing, just from the list.
Be careful with the indexes, if you access an element that doesn't exist, names[1] for example, bad things happen.
We'll try to get rid of them later.
its working
okay
this inputs one name and prints it
next?
nwp
nwp
Cool. Next we need to get the loop back. I don't know a good way to express it though. One way is while (true) { /* read names */ } which reads forever and then use break to stop the loop.
12:01
ok.we are using a while loop...
now?
nwp
nwp
Can you enter multiple names now?
1 sec
not really
nwp
nwp
What does it look like now?
should be something like this?
std::cout<<"Another?(y/n)";
	std::cin>>c;
    while(c==y)
    {
    	std::cin >> name;
	}
nwp
nwp
Skip the asking for now. Try to make smaller steps.
12:10
@nwp sorry to say... but can we continue after some time?(maybe after 4 hrs?)
nwp
nwp
Sure.
@nwp Thanks a lot...should I open a room with you or continue here?
nwp
nwp
Not sure. There is also a chance that I'm not on later. You can just post actual questions here, the "teach everything from the beginning" is usually supposed to be done by a book.
Anyone here using Dlib? I have specific question about how the DNN API works.
@nwp I needed help with a specific part... I mentioned that
12:16
@nwp Thank you I was really busy
@nwp I was able to solve this :)
nwp
nwp
@tatan Yeah... we probably didn't get to that yet.
@nwp
@nwp this is the achieved result
#include <iostream>

using namespace std;

class A
{
protected:
int* m_a;
int* m_b;
};

class B : protected A
{
public:
int m_c;
int m_d;

B()
{
m_c = 100;
m_d = 100;
/* Binding custom interface to base interface */
m_a = &m_c;
m_b = &m_d;
}

int& getRefA() const
{
return *m_a;
}

int& getRefB() const
{
return *m_b;
}
};

class C
{
private:
B m_e;

public:
B& getIfClass()
{
return m_e;
}

/* Not needed in case of the binding in constructor method */
void ReadData()
{
m_e.getRefA() = m_e.m_c;
m_e.getRefB() = m_e.m_d;
nwp
nwp
12:40
@LXSoft I don't really know what you are trying to do here. Writing a wrapper?
 
1 hour later…
14:05
@nwp yes wrapper for a type
@nwp Santa :))
8 messages moved from Lounge<C++>
14:59
is there a better function to read data from input to a string other than gets(var); ?
because with gets() i need to put a getchar(); after
and std::cin doesnt save the spaces
should i use getline(cin, input); for what i want?
or is there a better option?
nwp
nwp
15:27
@jeyejow That one. You can even specify the delimiter if '\n' isn't where you want to stop reading.
ok ill give it a try, thanks!
nwp
nwp
Someone just upvoted a bunch of my answers within a short time. I have a suspicion that someone here wanted to show their appreciation. Unfortunately that is against the rules and a script will probably revert the votes and there may be a moderator intervening if it continues. Don't do that please.
3
Heyyo again!
So for once, I'm here with something other other than I've been with the previous two days. Same project, but different thing.
I'm currently trying to check if a string exists in a specific position in an array, and if it does, return the array (or, the data on that position)
char * get_history_array(char * sortType, int n, int i) {
    if(strstr(sortType, history[i]) != NULL) {
      return history[i];
    }
    else {
      return NULL;
    }
}
That's the function that I'm currently looking at.
GDB proves, that sortType is "Insertion Sort", and history[i] is "Insertion Sort Random Array 1000 1.00 ms", though it still goes to the else-statement.
Hence the issue, and confusion
nwp
nwp
15:43
Make a mcve.
Shit, closed the chat. Back
oh wait
Yeah
So I might have gotten the strstr() arguments swapped up
history[i] was where sortType should have been, vice versa
Thank you for pointing out the apparently-not-so-obvious @milleniumbug
yeah this should be your reflex next time - if a function does something different than you expect, read the docs for that function
Thing is, I did
But I mostly looked at the syntax, went "THATS WHAT I GOT THOUGH!!!111" - not so much at the args
15:52
so you haven't read it carefully enough, yeah
happens
Gonna be sooooo relieved when this task is pushed and done and can move on to C++ from C
Probably wont happen until tomorrow, but hey
 
1 hour later…
17:04
have two search methods...maybe its possible to combine them together ? coliru.stacked-crooked.com/a/0d6beb84dd7035d6
have a default "will never appear" ID and use that as a marker you want any ID coliru.stacked-crooked.com/a/fa0417fd70679fb2
17:38
@ratchetfreak o.k. so it its maybe appropriate, to combine my Gesture and ExtendedGesture to one Type with an default id of -1
and do it without inheritance. Gesture->ExtendedGesture
17:58
or like this, when let it with inheritance. coliru.stacked-crooked.com
i have a problem. Im using the function std::getline and as parameters i have std::cin and a char[]. Im getting an error that says that no instance matches the parameters, but if i use a string i dont get the error, is is that?
isnt a string a char[] ?
nwp
nwp
@jeyejow No. std::string behaves differently. Specifically it adapts its size to whatever is needed while a char [] has fixed size.
but i want it to have a fix size
nwp
nwp
Why would you want that? That only makes your program crash when std::cin reads too much.
But if you absolutely must have a fixed size use std::array<char, 42> or something.
really?i didnt knew that. Basicly i have another function that takes a char* as parameters, so im reading input from the user into a char[]
if i use a std::string will that work in the function?
or do i have to convert it?
nwp
nwp
18:04
You shouldn't use output parameters. Your function should not take a char * and return an std::string.
the function is not mine, is a function of a library i have in my project
nwp
nwp
Then read the library documentation how to use the function properly. You can still use string.data() but you lose out on the automatic size adaption so you must do string.resize(42); first.
Providing a non-resizable buffer for an unknown amount of data is pretty bad design. There is no way to get it right.
the second argument of thatfunction
thats what i want the user to input
but the getline doesnt let me use char *
only string
and i need to use getline because of the spaces the text might have
nwp
nwp
So you use regular std::getline to read the data into the string and then pass string.data() and string.size() to the send function.
18:11
that works?
i need to try that
but istn .data() only for vectors?
string also has data()
nwp
nwp
No, it also works for std::string. There is also string.c_str() which you could read as "give me the string as a C string". It basically does the same.
ohh nice i didnt knew that, thanks!
ok this is weird
so i ask the user for the text, and then i send it with the size parameter as string.size()
but on the other side i recieve the text plus random characters
nwp
nwp
How did you receive it?
i store the things i receive in a char []
is that a problem?
nwp
nwp
18:20
Yeah. Typical C string.
The receive function should tell you how much data it got, so you can do cstring[length] = '\0'; to make it a properly null-terminated C string.
well the thing is, i have a function called recv (msdn.microsoft.com/en-us/library/windows/desktop/…) and i need to specify in a file the size of what im recieving
how do i know the size of the string i will receive?
do i just use std::string for this aswell?
nwp
nwp
Read the description again. It doesn't ask you for the size of the data you receive.
before i didnt gave me a problem when i used char []
the lenght of the buffer i mean
nwp
nwp
Right, and the return value is either SOCKET_ERROR or the number of bytes received, so you know where to put your null terminator.
Alternatively you can send the null-terminator through the socket.
can i do this in string? : std::string abc; abc[4] = '\n'; ?
will this do the job?
nwp
nwp
18:25
no. Use .push_back('\n') instead.
ok ill try that
nwp
nwp
Whenever you use an index like [4] you must make sure not to access out of bounds, otherwise bad things happen.
If you do that then you will get a newline and the rest of the buffer will still have garbage.
ok ill keep that in mind
argument of type const char * is incompatible with argument of type char *
what do i do about this,im passing a string.data() as one of the parameters
nwp
nwp
Where are you passing that to?
im passing it as the second argument of recv()
nwp
nwp
18:31
Did you .resize() the string first?
no, do i need to?
if i resice it to for example 512 and then i read user input, will it only store 512 chars?
so if i want do make sure there is a \n in the end in resise it to 511
nwp
nwp
Yes. recv writes data into it and you should give it enough memory to hold the data. recv is not smart enough to resize the string by itself. Something like boost::asio would be.
i tried installing boost but it gave me alot of errors
nwp
nwp
@jeyejow If you read user input using std::cin then the string will automatically be resized.
so how do i make it so the string is 512 counting the '\n' ?
do i do this? string[511] = '\n';
nwp
nwp
18:35
Do you have 511 characters to put into the string before the '\n'?
@jeyejow If string.size() >= 512 then yes, otherwise no.
i dont know, thats up to the user, i just want to send a maximum of 512 bites
nwp
nwp
Ok, lets go back a bit. You had the user enter data with std::getline correct?
yes, but please keep in mind im just testing the usage of sockets, in the end this will be applied to another program i alreaddy have that will send BYTEs, not chars
nwp
nwp
Bytes and chars is essentially the same thing until std::byte arrives.
what is std::byte?
nwp
nwp
18:39
Anyways, you got the user data in a std::string string;. The size may or may not be more than 512 bytes. What do you want to do about it?
i want to send it over, using sockets, to another applycation that is expecting 512 bites
it could be less
but not more then 512
so i need to make sure that whatever the user types, i will only send 512 or less if he types less
nwp
nwp
Ok, so you should check that before you send the data.
i would put the '\n' in the position 511
or i would cut the string
it doesnt matter wich one sicne on the ohter side only 512 will be stored
the problem is when its less then 512
nwp
nwp
A '\n' does not end the string. You can do something like if (string.size() > 512) string.resize(512);.
Ok, now on to the recv part.
ok
the recv function
nwp
nwp
18:43
You have a std::string string; you did string.resize(512); in order to give it enough memory and you passed string.data() and string.size() to recv.
yes
no
i passed on the size 512
i need it to be 512
nwp
nwp
Why 512? You can avoid repeating that magic number here.
Since you just did string.resize(512); you know that string.size() will be 512. But if you later change the number you only have to do so in 1 place if you use .size().
because 512 is sector aligned data, and ill use the data i receive to write to a device i have open
nwp
nwp
There still is the problem that it doesn't compile because in pre-C++17 string.data() returns a const char * and recv wants a char *.
yes
thats my problem
so it cant be a string
nwp
nwp
18:46
You can either change your compiler settings to use C++17 or use &data[0] instead.
&data[0] ?
whats does that mean?
the address of the address of what in position 0 ?
nwp
nwp
I meant &string[0]. Basically string[0] is the first char and & takes the address of that so you get a char *.
So recv should now be happy.
There is another problem that recv might not read a full 512 bytes, so you end up with garbage again.
does string store memory straight?
nwp
nwp
Contiguously, yes.
in a line?
ok
ok so there is the idea i have
recv will read 512 because i will send it 512
nwp
nwp
18:50
So you have to do something like const auto received = recv(...); if (received == SOCKET_ERROR) { //handle error } else { string.resize(received); }.
yes
thats better then what i was thinking
Hi, I had a quick dumb question. Making a binary search tree and basically when I make the new node in the add function, it won't go out of scope at the end of function right?
it returns a int i think
I might just be overthinking this though.
2 messages moved from Lounge<C++>
nwp
nwp
18:53
@Annabelle Depends on how you made your node.
struct Dict::DictNode {
string key; // Key.
int val; // Value.
bool removed; // Check if deleted or not.
struct DictNode *left, *right; // Pointer to left and right nodes. Will be NULL whenever a new nude is inserted.
};
nwp
nwp
removed should probably not be a property of a node.
This shows what a node is, but not how you made it in your add function.
I just did
int Dict::addOrUpdate(string key, int val) {
DictNode Node;
}
And then will assign the values to it
nwp
nwp
That Node will be gone after addOrUpdate returns.
Or would it need to be a DictNode* Node;?
nwp
nwp
18:57
Is it for a dumb school exercise or do you want to do it properly?
It's a dumb school exercise, but I'd rather do it properly regardless.
nwp
nwp
That's tricky. The school probably wants you to do new DictNode() and the proper solution is either std::map to be done immediately or std::unique_ptr to make the manual memory management less painful.
We have to implement a destructor and the requirement is:

`Delete the entire structure, and clean up all memory that was allocated.`
nwp
nwp
@Annabelle You probably want to turn that nude into a node in that comment before you send it in.
Oh haha
Didn't see that
So for the destructor we have to delete all the allocated memory
and I'm not sure which way would be easier to do that
nwp
nwp
19:02
@Annabelle The proper way is to let std::unique_ptr take care of it. The school way is to call delete on everything like a noob.
Did they mention smart pointers, RAII or move semantics at all?
Nope, none of that yet
So I believe I need to do delete
nwp
nwp
Yeah. Schools are really good at making C++ look dumb.
Anyways, there are 2 ways you can handle this. Either your dict deletes all the things or every node deletes its children and you only delete the root node.
@nwp it works, thanks
How would I do the former?
nwp
nwp
They did talk about recursion right?
19:06
Yes
nwp
nwp
Basically you write a function void delete_all_the_things(DictNode *node) that calls delete_all_the_things on left and right and then deletes the node.
Probably find a better name than delete_all_the_things.
Okay I can do that!
Thank you
Uh for my node though
before deleting it
should I set left and right to null?
nwp
nwp
You need to be a bit careful to do it in the right order so that you don't access nodes that you already deleted and that sometimes the node is nullptr.
@Annabelle no
Okay
Thanks I'll get started
 
4 hours later…
23:36
If an object is declared (MyObject foo;), is the constructor called?
nwp
nwp
The parenthesis are weird.
@nobism Yes, the default constructor is called
23:51
@nwp Sorry lol, I also realised I shouldn't have used them in this case.
@Justin Ok so MyObject foo = MyObject(); is redundant?
@nobism Yes. You could argue for writing auto foo = MyObject(); though. Some people prefer that style. I usually just write MyObject foo;
@Justin Ok. And MyObject foo; foo = MyObject(); would replace foo with a new fresh object?
Conceptually yeah. What happens is you default-construct foo, then call operator=(MyObject&&) with a new default-constructed MyObject as the parameter. But assuming that MyObject behaves well, you can pretend that you are replacing foo with a new fresh object
@Justin Ok great, thanks :)

« first day (583 days earlier)      last day (2494 days later) »