Anonymous
#includes <stdio.h >
#includes <stdlib.h>
#includes <math.h>
# define pi 3.14
double w0=1.0, w=1.0, f1=0.0,a=0.0;
double f(double t , double theta ,double v) { return v;}
double g(double t,double theta ,double v )
{return -pow(w0,2)*sin(theta )-a*v*f1*cos(w*t);}
int main()
{
double h=0.01, t=0.0, a=0.0 , v=0.0 ;
double T=2*pi/w0 ,Tobs =3*T ,theta =pi/5 ;
FILE*fp;
fp=fopen("pendule_simple1.dat", "w");
while (t<=Tobs)
{ v=v+h*g(t,theta,v);
theta=theta+h*f(t,theta,v);
printf("%.16lf\t %.16lf\n",theta,v);
fprintf(fp,"%.16lf \t %.16lf\n",theta,v);
t=t+h;
}
fclose(fp);
return 0;
}
/* run
undefined reference to 'pow'
undefined reference to 'sin'
undefined reference to 'cos'*/
// where is error in this code ???
return 0;
}
Ammar
#includes <stdio.h >
#includes <stdlib.h>
#includes <math.h>
# define pi 3.14
double w0=1.0, w=1.0, f1=0.0,a=0.0;
double f(double t , double theta ,double v) { return v;}
double g(double t,double theta ,double v )
{return -pow(w0,2)*sin(theta )-a*v*f1*cos(w*t);}
int main()
{
double h=0.01, t=0.0, a=0.0 , v=0.0 ;
double T=2*pi/w0 ,Tobs =3*T ,theta =pi/5 ;
FILE*fp;
fp=fopen("pendule_simple1.dat", "w");
while (t<=Tobs)
{ v=v+h*g(t,theta,v);
theta=theta+h*f(t,theta,v);
printf("%.16lf\t %.16lf\n",theta,v);
fprintf(fp,"%.16lf \t %.16lf\n",theta,v);
t=t+h;
}
fclose(fp);
return 0;
}
/* run
undefined reference to 'pow'
undefined reference to 'sin'
undefined reference to 'cos'*/
// where is error in this code ???
return 0;
}
That's horrible indentation.
But I think that error is caused by missing library at linking time. Have you used -lm when linking?
Anonymous
Anonymous
no
klimi
if you wish to use math library, you need to link it (that's the -lm mentioned above)
Anonymous
shriman_deepak
How to read a file from command line ?
shriman_deepak
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc, char *argv[])
{
FILE *fp = fopen( "file1.txt", "r");
char line[50];
int count = 0;
if (fp == NULL)
{
printf("File opening Unsuccessful\n");
exit(1);
}
while (fgets(line , 30 , fp) != NULL)
{
strrev(line);
count = count + 1;
printf("%d%s",count,line);
}
fclose(fp) ;
return 0;
}
shriman_deepak
i wrote this code but i need to give filename from command line
Hanz
shriman_deepak
shriman_deepak
FILE *fp = fopen( argv[1], "r");
shriman_deepak
don't know where i am doing mistake
Anonymous
Hanz
shriman_deepak
yes the file exists
Hanz
Copy the error, I wanna see em
shriman_deepak
if i am giving the file name in code it runs
shriman_deepak
let me send you the code\
shriman_deepak
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc, char *argv[])
{
FILE *fp = fopen( argv[1], "r");
char line[50];
int count = 0;
if (fp == NULL)
{
printf("File opening Unsuccessful\n");
exit(1);
}
while (fgets(line , 30 , fp) != NULL)
{
strrev(line);
count = count + 1;
printf("%d%s",count,line);
}
fclose(fp) ;
return 0;
}
Hanz
shriman_deepak
the problem is that it don't give any erro
shriman_deepak
but it's not running too
Ammar
Ammar
The file to be passed in argument?
shriman_deepak
now it worked for me too
Hanz
Hanz
Some IDE closes the program almost immediately
shriman_deepak
shriman_deepak
Is there any another alternative for fgets ?
shriman_deepak
To read a file ?
Hanz
Hanz
shriman_deepak
how to get rid of fgets() new line character ?
shriman_deepak
shriman_deepak
while (fgets(line , 30 , fp) != NULL)
{
strrev(line);
count = count + 1;
printf("%d%s",count,line);
shriman_deepak
where should i put \0 here ?
klimi
buf[end] = '\0'
\Device\NUL
Please use pastebin
shriman_deepak
Here u can paste ur code and send the link
Ammar
Note: fgets doesn't always put a newline.
If the input doesn't contain a newline, then the written data doesn't contain a newline.
So the safer way is:
while (fgets(line, 30, fp) != NULL) {
size_t len = strlen(line);
if (len == 0)
continue;
if (line[len - 1] == '\n')
line[len - 1] = '\0';
strrev(line);
count = count + 1;
printf("%d%s", count, line);
}
shriman_deepak
Oh man please
shriman_deepak
Ammar
This is annoying.
shriman_deepak
Let me check.. by the way thankyou so much
Ammar
Will it work ?
Untested, but I am confident enough it will work.
shriman_deepak
shriman_deepak
shriman_deepak
but if u dont mind can u please expalin how did u do this ?
Ammar
but if u dont mind can u please expalin how did u do this ?
strlen() returns the length of the string. So strlen("aaaa\n") will return 5. From this we can check whether the last character is a newline or not. If it is a newline, then replace it with a NUL byte so that the string will get trimmed.
Anonymous
What walrus operator?
Search for it on Google. It is a new operator that makes assignment of a value an expression unlike the = operator that is a statement in Python.
Prabhat
Consider there is a class A that contains public data member int x;
and I create an instance.
A a1;
a1.x = 10;
is there any way if I print a1 (cout << a1 <<endl;) and it prints the value of x in that instance?
Pavel
Prabhat
Prabhat
but what if I wanna print it with printf
Wojak
Send offtopic group link
Anonymous
Anonymous
Also printf is faster (not that it matters most of the time)
J
Thanks
D
Imagine if everyone was so far from standards as you. that's would be sad, isn't it;)
Push yourself to standards. After a while you will be ok with that and will prefer them by yourself
Peace
How can we make list of Complex class objects when we have a
constructor also which takes parameters
class Complex{
int real,imag;
public:
Complex(int a, int b): real(a),imag(b){}
void showData(){
cout<<"Real: "<<real<<endl<<"Imaginary: "<<imag<<endl;
}
};
int main(){
Complex *ptr2= new Complex[4];//this is showing error
ptr2->showData();
return 0;
}
Laopigo
Bereket
Is it possible to call two different functions with the same function name?
If so how?
\Device\NUL
\Device\NUL
Input/output can be a performance bottleneck in C++ programs. By default, the standard iostreams (cin, cout, cerr, clog, and their wide-character counterparts) are synchronized with the C stdio streams (stdin, stdout, stderr), so that reads from cin and stdin, or writes to cout and stdout, can be freely intermixed. However, this coupling has a performance cost, because of the buffering in both kinds of streams. In the pre-standard "classic" iostreams library, unsynchronized mode was the default. If there is no need for a program to make calls to both standard C streams and C++ iostreams, synchronization can be turned off with this line of code:
std::ios_base::sync_with_stdio(false);
If any input or output operation has occurred using the standard streams prior to the call, the effect is implementation-defined. Otherwise, called with a false argument, it allows the standard streams to operate independently of the standard C streams (§IS-27.4.2.4). Another standard default is to flush all output to cout before reading from cin, for the purpose of displaying interactive prompts to the application user. If this synchronized flushing is not needed, some additional performance can be gained by disabling it:
std::cin.tie(0);
Ref: https://www.stroustrup.com/Performance-TR.pdf
Terleyeee
Hi gais. Does anybody know how to code a login module in c++ with mysql?? Please help me
Bereket