I was working on this code and it worked fine in the university lab but when I rewrote the code at home it keeps giving this error
expected ';' before 'fr'
and after I put a ';' before fr, I got
expected ';' before 'cout'
it worked afterwards but during lectures, they did not inform us to do so. Can someone please explain to me why and what's wrong with my code?
Moreover, .exe file kept closing before calculating the results so my lecturer said to include #include <conio.h> at the beginning and getch () at the end. they didn't mention why. Can someone please explain to me what is that and why we include it? Why does.exe file close before showing the calculated result?
Thank you
[code]
#include <iostream>
#include <cmath>
#include <conio.h>
using namespace std;
int main()
{
const double pi=3.14159;
double i, e, r, f, l, c, fr;
cout <<"Enter the Frequency(Heartz): "<< endl;
cin >> f;
cout <<"Enter the Resistance(Ohms): "<< endl;
cin >> r;
cout <<"Enter the Capacitance(Farads): "<< endl;
cin >> c;
cout <<"Enter the Inductance(Henrys): "<< endl;
cin >> l;
cout <<"Enter the Electromotive Force(Volts): "<< endl;
cin >> e;
double eq1=2*pi*f*l;
double eq2=1/(2*pi*f*c);
i=e/sqrt(pow(r,2)+pow(eq1-eq2,2))
fr=1/(2*pi*sqrt(l*c))
cout <<"The Current is " << i << "A" << endl;
cout <<"The Reasonant Frequency is " << fr << "Hz" << endl;
oh, I didn't know that we needed to add ; after a mathematical equation. It works fine now! Also about #include<conio.h> and getch(), can you please explain the use and function of it?
Thank you so much!
Sorry I did not see this in your code. It became more noticable in Furry Guy's post.
salem c once wrote:
#include<conio.h>
Obsolete since 1990, when the world stopped using DOS as a primary operating system.
Using "conio.h" may work for you at school, but not every one has this header file to use and if they do happen to have it the file is not what it use to be.
C++ does not have a replacement for "getch()", but this code I use is a fair replacement for getch() or system("pause").
1 2 3 4 5 6 7
/ A fair C++ replacement for"system("pause")". Or a way to pause the program.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();
return 0; // <--- Not required, but makes a good break point.
Line 3 is the most portable way of writing this line although you could use std::cin.ignore(1000, '\n');. You just need a large number for the first parameter. Also you can change "std::cin" to the name of a file stream when working with a file.