Programmers only

#79
C++:
#include <iostream>
using namespace std;

const char* append(const char* s1, const char* s2) {   
    string s(s1);   
    s += s2;
    return s.c_str();
}
void foo() {
    const char* total = append("abc", "def");
    cout<<"total = "<<total<<endl;
}

int main() {
  foo();
  return 0;
}

another one
 

Altaïr

Space Stig, Master of gravity
Staff member
Head Moderator
Team Kolibri
Modder
TEAM HAWK
Atlas
Deja Vu
Under Pressure
Forum Legend
#80
C:
kin_energy = (1/2)*mass*speed*speed;
No, sorry :)
And it's not about the lacking initialization either, though it's a good practice to always initialize a variable.

Here is the answer for those who give up.
The problem comes from "1/2". When a division concerns 2 integers, the compiler interprets it as an euclidian division. At least one of the numbers has to be declared as a float for it to be considered an actual division. 1 and 2 being 2 integers, the result of 1/2 is 0.

There are several solutions to fix this:
Code:
// Fixed in a very simple way
kin_energy = 0.5*mass*speed*speed;

// Write one of the numbers as a float (1 is an int, 1.0 is a float)
kin_energy = 1.0/2*mass*speed*speed;

//Or simply...
kin_energy = mass*speed*speed/2;
// The left term is a float, so it's fine.
 
#81
No, sorry :)
And it's not about the lacking initialization either, though it's a good practice to always initialize a variable.

Here is the answer for those who give up.
The problem comes from "1/2". When a division concerns 2 integers, the compiler interprets it as an euclidian division. At least one of the numbers has to be declared as a float for it to be considered an actual division. 1 and 2 being 2 integers, the result of 1/2 is 0.

There are several solutions to fix this:
Code:
// Fixed in a very simple way
kin_energy = 0.5*mass*speed*speed;

// Write one of the numbers as a float (1 is an int, 1.0 is a float)
kin_energy = 1.0/2*mass*speed*speed;

//Or simply...
kin_energy = mass*speed*speed/2;
// The left term is a float, so it's fine.
hmm interesting
 

Tony

Registered
#82
No, sorry :)
And it's not about the lacking initialization either, though it's a good practice to always initialize a variable.

Here is the answer for those who give up.
The problem comes from "1/2". When a division concerns 2 integers, the compiler interprets it as an euclidian division. At least one of the numbers has to be declared as a float for it to be considered an actual division. 1 and 2 being 2 integers, the result of 1/2 is 0.

There are several solutions to fix this:
Code:
// Fixed in a very simple way
kin_energy = 0.5*mass*speed*speed;

// Write one of the numbers as a float (1 is an int, 1.0 is a float)
kin_energy = 1.0/2*mass*speed*speed;

//Or simply...
kin_energy = mass*speed*speed/2;
// The left term is a float, so it's fine.
:eek: :oops:
 

Altaïr

Space Stig, Master of gravity
Staff member
Head Moderator
Team Kolibri
Modder
TEAM HAWK
Atlas
Deja Vu
Under Pressure
Forum Legend
#84
C++:
#include <iostream>
using namespace std;

const char* append(const char* s1, const char* s2) {  
    string s(s1);  
    s += s2;
    return s.c_str();
}
void foo() {
    const char* total = append("abc", "def");
    cout<<"total = "<<total<<endl;
}

int main() {
  foo();
  return 0;
}

another one
The const char* returned by the append function is actually a pointer on local data that will be destroyed when exiting the function. The data is no longer valid, this is likely to crash when you try to display the string.
 
S

SFS for Life

Guest
#85
The const char* returned by the append function is actually a pointer on local data that will be destroyed when exiting the function. The data is no longer valid, this is likely to crash when you try to display the string.
hey nice
 

Altaïr

Space Stig, Master of gravity
Staff member
Head Moderator
Team Kolibri
Modder
TEAM HAWK
Atlas
Deja Vu
Under Pressure
Forum Legend
#87
hmm imma gonna love this thread since i do programming
C++:
#include <string>
#include <iostream>

int main(void)
{
    std::cout << "Welcome to the forum SFS for Life!" << std:: endl;
    return 0;
}
 
S

SFS for Life

Guest
#88
C++:
#include <string>
#include <iostream>

int main(void)
{
    std::cout << "THANK YOU!!!!" << std:: endl;
    return 0;
}
 

Tony

Registered
#91
Simple javascript test

Code:
<html>
<body>
<script Language = "JavaScript">

var n = "Test";
document.write ('welcome = ' + n);

</script>
</body>
</html>
 
Last edited:

Tony

Registered
#92
there must be a solution. using javascript or html in attached file. No problem for the forum. You can trust me.
 
Last edited:

Altaïr

Space Stig, Master of gravity
Staff member
Head Moderator
Team Kolibri
Modder
TEAM HAWK
Atlas
Deja Vu
Under Pressure
Forum Legend
#93
Simple javascript test

Code:
<html>
<body>
<script Language = "JavaScript">

var n = "Test";
document.write ('welcome = ' + n);

</script>
</body>
</html>
Does this write "welcome Test"?
I suppose there is a trap, but I don't know javascript.
 

Soyuzturtle

«★★★» Grand Admiral «★★★» // PT
Swingin' on a Star
Atlas
Fly me to the Moon
Registered
#94
Me who's coding skills stretch to the ability to bp edit : reads this thread

Also me :
hqdefault.jpg
 

Tony

Registered
#98
Does this write "welcome Test"?
I suppose there is a trap, but I don't know javascript.
There is no trap and no code error. I just wanted to do a test. to execute the code you just have to copy the code in a .txt file and rename it in .html Then open the file with the web browser of your choice.