Anonymous
#include<iostream> #include<string> using namespace std; class Books { public: string author; float cost; string tittle; string publisher; float account_number; int copies; void setdata(); }; { cout<<"\n Enter the following details:\n "; cout<<"\n Authour name:"; gets(author); cout<<"\n Tittle"; gets(tittle); cout<< "\n Publisher:"; gets(publisher); cout<<"\n cost:"; getline(cin,cost); cout<<"\n Number of copies:"; getline(cin,copies); cout<<"\n Enter account nunber:"; getline(cin, account_number); } ///functions to dipaly data enterred { cout<<"\n Tittle of the book :"<<tittle<<endl; cout<<"\n Authour of the book:"<<authour<<endl; cout<<"\n Publisher of the book:"<<publisher<<endl; cout<<"\n Cost of the book:"<<cost<<endl; cout<<"/n Number of copies of the book:"<<copies<<endl; cout<<"\n Acouunt number :"<<account_number<<endl; cout<<"\n -------------------\n"; }
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; }
Anonymous
no
klimi
if you wish to use math library, you need to link it (that's the -lm mentioned above)
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
shriman_deepak
shriman_deepak
FILE *fp = fopen( argv[1], "r");
shriman_deepak
don't know where i am doing mistake
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; }
shriman_deepak
the problem is that it don't give any erro
shriman_deepak
but it's not running too
Ammar
but it's not running too
How does your program behave?
Hanz
but it's not running too
Ok I will try your code on my machine
shriman_deepak
How does your program behave?
after taking file name from command line it changes line and do nothing
Hanz
Ok I will try your code on my machine
It works on my machine @shriman_deepak
Ammar
The file to be passed in argument?
shriman_deepak
It works on my machine @shriman_deepak
i am really very sorry i am so noob 🙃, my IDE sucks
shriman_deepak
now it worked for me too
Hanz
i am really very sorry i am so noob 🙃, my IDE sucks
Oh you use IDE? Put getch() before return 0;
Hanz
Some IDE closes the program almost immediately
shriman_deepak
Oh you use IDE? Put getch() before return 0;
Alright tell me one more thing
shriman_deepak
Is there any another alternative for fgets ?
shriman_deepak
To read a file ?
shriman_deepak
fgetc, fgetchar
can i send pm ?
shriman_deepak
how to get rid of fgets() new line character ?
klimi
how to get rid of fgets() new line character ?
well... you could just rewrite it to '\0'
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
Is there any another alternative for fgets ?
How bout getline ? But that's not standard
\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
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?
Prabhat
but what if I wanna print it with printf
Laopigo
but what if I wanna print it with printf
You shouldn't use printf in C++, its C function
Wojak
Send offtopic group link
Anonymous
Also printf is faster (not that it matters most of the time)
J
Thanks
Laopigo
It's pretty common tho
As far as I know, using C functions like printf, scanf etc is not C++ style
Anonymous
As far as I know, using C functions like printf, scanf etc is not C++ style
I think u are right. I just prefer some C functions better which is technically not the standard ig
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; }
Bereket
Is it possible to call two different functions with the same function name? If so how?
\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
Laopigo
Is it possible to call two different functions with the same function name? If so how?
Maybe this is what you mean #include <iostream> void f1() { std::cout << "Hello World!" << std::endl; } void f2() { std::cout << "Bye World!" << std::endl; } void (*f)(); int main(int argc, char **argv) { f = f1; f(); f = f2; f(); return EXIT_SUCCESS; }