Simple C++ program to find the area of a circle - c++

#include<iostream>
#include<conio.h>
using namespace std;
//function declaration:
double circleArea(double);
int main(){
double rad1;
double rad2;
double ringArea;
cout<<"plz enter number";
cin>>rad1;
cout<<"plz enter number";
cin>>rad2;
ringArea = circleArea(rad1)-circleArea(rad2);
cout<<ringArea<<endl;
system("pause");
}
// function definition:
double circleArea(double rah){
return(3.1415926*rah*rah);
}
In above code if I enter 5 then it return value 2.66454e-015 but if I make changes in function definition like
double circleArea(double rah){
int confuse;
confuse =(3.1415926*rah*rah);
return confuse;
}
and enter value of 5,5 or 6,6 or 7,7 then it return value of 0.but when I enter 5,6 or 6,7 other than same values then it return some other value which is not matching with part in which I have not declared variable confuse.

Your variable declaration is wrong
double circleArea(double rah){
double confuse;
confuse =(3.1415926*rah*rah);
return confuse;
}

try that
double circleArea(double rah) {
double confuse;
confuse = (3.1415926*rah*rah);
return confuse;
}

Related

(C++) Why this program is giving the "0" or "nan" in the output?

I have this simple C++ program where I have to input the marks of 5 subjects and calculate it's percentage. But I have no idea why in the output it shows "0" and if I remove those return 0 statements under input() and percentage() functions then it shows "nan" in the output.
Here is the code:
#include<iostream>
using namespace std;
class Marks
{
float s1,s2,s3,s4,s5;
float p;
public:
float input();
float percentage();
};
float Marks::input()
{
cout<<endl<<"Enter the marks of your 5 subjects."<<endl;
cin>>s1>>s2>>s3>>s4>>s5;
cout<<"Subject 1="<<s1<<endl<<"Subject 2="<<s2<<endl<<"Subject 3="<<s3<<endl<<"Subject 4="<<s4<<endl<<"Subject 5="<<s5<<endl;
//return 0;
}
float Marks::percentage()
{
p=s1+s2+s3+s4+s5;
p=p/5;
cout<<p<<endl;
//return 0;
}
int main()
{
Marks student1;
cout<<"Marks of student 1 are as follow:"<<endl<<student1.input()<<endl;
cout<<"Student 1: Percentage = "<<student1.percentage()<<endl;
return 0;
}
And this is the output:
Your functions are promising to return a float, but they don't. That is undefined behaviour. And once your program contains UB anywhere, it becomes meaningless in its entirety.

Error: expected ',' or ';' before '{' token

I'm sure there's a very very simple answer to this, but I've looked for hours and can't figure it out. As the Title says I get an error which I commented in the code. Everything seems to be in the correct syntax, so I have no idea what it could be.
#include <iostream>
#include <math.h>
using namespace std;
float x, y, k, q, base, exponent;
/*float atan(q)
{
//I had made my own functions for all the different math functions that
would have normally been included in the math.h file, but I had no idea
how to tackle making a function for an arc tangent as all the information
I could find online just had you kinda guess what the end result might be,
so I used the math.h file for this and only this function.
}*/
float sqrot(k)
{ // <----- the error is here
float guess, divide, average, z;
guess=rand()%k;
for(z;z<500;z++)
{
divide=k/guess;
average=(guess+divide)/2;
guess=average;
}
}
float cosine(y)
{
y=1-(((y*y)/2)+((y*y*y*y)/24)-((y*y*y*y*y*y)/720));
return y;
}
float sine(x)
{
x=x-(((x*x*x)/6)+((x*x*x*x*x)/120)-((x*x*x*x*x*x*x)/5040));
return x;
}
float power(base, exponent)
{
while(exponent>1)
{
base=base*base;
exponent-1;
}
return base;
}
float haversine(lat1,long1,lat2,long2)
{
float degree_to_rad, pi=3.14159;
int d_lat, d_long, a, c, mi;
degree_to_rad=pi/180;
d_lat=(lat2-lat1)*degree_to_rad;
d_long=(long2-long1)*degree_to_rad;
a=power(sine(d_lat/2),2)+cosine(lat*degree_to_rad)*cosine(lat2*degree_to_rad)*power(sine(d_long/2),2);
c=2*atan((sqrot(a))/(sqrot(1-a)));
mi=3956*c;
return mi;
}
int main()
{
int answer;
cout<<"Enter the Latitude of your starting location: ";
cin>>lat1;
cout<<"Enter the Longitude of your starting location: ";
cin>>long1;
cout<<"Enter the Latitude of your ending location: ";
cin>>lat2;
cout<<"Enter the Longitude of your ending location: ";
cin>>long2;
answer=haversine(lat1, long1, lat2, long2);
cout<<"The distance between the two points is: "<< answer;
return 0;
}
Formal arguments must have a type
float sqrot(k)
should have been
float sqrot(type k)
// ^^^^
and similarly with all other functions

Getting wrong output. Garbage value

Problem is, on execution, the value of roundCost I'm getting is
something like -1220673834. I post the entire program because I'm not
sure where I'm going wrong.
Note: I was asked to take all variables as double type and later,
roundCost should be of type int. So I used type conversion there.
#include<iostream>
using namespace std;
class Restaurant{
private:
double tip, tax,totalCost,mealCost, tipPercent, taxPercent;
int roundCost;
public:
int tipCalc(double)
{
tip=mealCost*(tipPercent/100);
return tip;
}
int taxCalc(double)
{
tax=mealCost*(taxPercent/100);
return tax;
}
int totalCost1()
{
totalCost=mealCost+tip+tax;
return totalCost;
}
int roundCost1(double)
{
roundCost=(int)totalCost;
return roundCost;
}
}; // class ends
int main()
{
double mealCost, tipPercent, taxPercent, totalCost;
int roundCost;
Restaurant ob1;
cout<<"\n Enter mealCost \n";
cin>>mealCost;
cout<<"\n Enter mealtipPercent \n";
cin>>tipPercent;
cout<<"\n Enter mealtaxPercent \n";
cin>>taxPercent;
ob1.tipCalc(tipPercent);
ob1.taxCalc(taxPercent);
ob1.totalCost1();
ob1.roundCost1(totalCost);
cout<<"\n Round of cost is "<<roundCost<<endl;
return 0;
}
One thing you seem to be missing is that variables in your class have a different scope then variables in your main. You set the mealcost in your main from cin but you never passed this variable to the class. I changed this to be done using a constructor that sets the meal cost on creation. In every class you make you should always add a constructor. Also, you should be naming the variables your passing to functions and then using the same name in the function. For example in the tax percent function i pass double t, t is the percent, we then use t in the calculation. Your round cost variable was also private so you needed to output it via a function.
Also int functions will return a value, if you are using this type of function you should be assigning the return variable to something, but since you are just setting things in your class you can use void functions for most. The only time you use a value in the main is in the roundcost so this one is good to have it return a value. As it is int (which i assumed you wanted) it will get no decimal points and it will simply cut off any decimals in the total cost (ie 75.75 would become 75).
#include<iostream>
using namespace std;
class Restaurant{
private:
double tip, tax,totalCost,mealCost;
int roundCost;
public:
Restaurant (double m)
{
mealCost = m;
}
void tipCalc(double t)
{
tip=mealCost*(t/100.0);
}
void taxCalc(double t)
{
tax=mealCost*(t/100.0);
}
void totalCost1()
{
totalCost=mealCost+tip+tax;
}
int roundCost1()
{
roundCost=(int)totalCost;
return roundCost;
}
}; // class ends
int main()
{
double mealCost, tipPercent, taxPercent, totalCost;
int roundCost;
cout<<"\n Enter mealCost \n";
cin>>mealCost;
Restaurant ob1(mealCost);
cout<<"\n Enter mealtipPercent \n";
cin>>tipPercent;
cout<<"\n Enter mealtaxPercent \n";
cin>>taxPercent;
ob1.tipCalc(tipPercent);
ob1.taxCalc(taxPercent);
ob1.totalCost1();
cout<<"\n Round of cost is "<<ob1.roundCost1()<<endl;
return 0;
}
Try to do a bit more research next time by using a debugger, outputting cout statements regularly and searching for the errors you find but this will give you a working code this time.

Not declared in Scope

I have wrote a simple average calculation program trying to calculate a semester average. When I compile the code I get an error telling me my 'inputExam' function was not declared in this scope. I've researched the error message and I can't figure out what to do to fix it.
I also get this error for the other functions, but once I understand my error I think I can fix the others.
#include <iostream>
using namespace std;
int main()
{
double finalExam=0.0;
double midterm = 0.0;
double quizzes = 0.0;
double labs = 0.0;
double semGrade=0.0;
midterm=inputExam("Midterm");
finalExam=inputExam("Final");
quizzes=inputAndAvgQuizzes();
labs=inputAndAvgLabs();
semGrade=(midterm*.2)+(finalExam*.2)+(labs*.5)+(quizzes*.1);
cout<<"Your End of Semester Grade is: " semGrade;
return 0;
}
double inputExam(string examType)
{
double grade;
cout<< "Enter the " examType " Score: ";
cin>>grade;
return (grade);
}
double inputAndAvgLabs()
{
double num [4];
double sum;
double avg;
if (int a=0, a<3,a++)
{
cout<<"What is the grade?"<<endl;
cin>>num[a]>>endl;
}
if (int a=0, a<3, a++)
{
sum=sum+num[a];
}
avg=sum/4;
return avg;
}
double inputAndAvgQuizzes()
{
double num[3];
double sum;
double avg;
double lowest = num[0];
if (int a=0, a<2,a++)
{
cout<<"What is the grade?"<<endl;
cin>>num[a]>>endl;
}
if (lowest>num[1])
{
lowest=num[1];
}
if (lowest>num[2])
{
lowest=num[2];
}
sum=num[1]+num[2]+num[3]-lowest;
avg=sum/2;
return avg;
}
You need to let it be known that those functions exist so add prototypes for your functions above main or define your functions there. Like so:
...
double inputExam(string examType);
double inputAndAvgLabs();
double inputAndAvgQuizzes();
int main() { ... }
//definitions after main
..or copy paste all those definitions above the call to main like so:
...
// Function defs here
// Prototypes no longer needed
...
int main() {...}
// Defs no longer needed here
Alternatively you can put all the definitions in an external file and compile it into the project via a make file or better yet, and as you progress, create classes in header and implementation files and include them in your file the same way(sort of) that you do #include <iostream>.
Another small nugget of advice would be to avoid using namespace std;. If not only in theory it's bad practice and can lead to namespace clashing in larger projects. If you, like me, hate typing std::string ... then add using std::string; to your code for the same ease of use.
In C/C++ you need to declare the function before you use it. In this case, it simply means declaring function prototypes before your main function and then implementing them after the main function.
Example:
// declare a prototype
double Function(int variable);
int main()
{
Function(5);
return 0;
}
// Implement the function
double Function(int variable)
{
/* Do Something */
}
Alternatively, you could change your code to the form:
// Implement the function first
double Function(int variable)
{
/* Do Something */
}
int main()
{
Function("Testing");
return 0;
}

Parameter Passing Converting Floats error

I'm having an issue with passing parameters for this simple program. I am getting a "cannot convert 'float*' to 'float' argument '2' to 'void getData(std::string, float, float, float, float)'" error when trying to compile this program. Can anyone figure out what I'm doing wrong? I've been trying forever. Note: Please ignore deprecated stuff like system("PAUSE") and a few other things. This is simple the way my teacher has thought me to code and this is what he wants me using for this program. I am aware of getchar() and I use it for practice and final work. Plus this shouldn't affect the program as I have been using it without issues before on small programs for my C++ class.
Here's the code:
#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
void getData(string,float,float,float,float);
void getCalc(int,float,float,float,float,float,float,float,float,float,float);
void getPrint(float,float,float);
int main()
{
int const acres=1000;
string crop;
float cpa[4];
float yield[4];
float per[4];
float increase[4];
float cost[4];
float grossmin[4];
float grossmax[4];
float netmin[4];
float netmax[4];
float netave[4];
getData(crop,cpa,yield,per,increase);
getCalc(acres,cpa,yield,per,increase,cost,grossmin,grossmax,netmin,netmax,netave);
getPrint(netmin,netmax,netave);
system("PAUSE");
return 0;
}
void getData(string fcrop,float fcpa[],float fyield[],float fper[],float fincrease[])
{
for (int i=0;i<4;i++)
{
cout<<"Enter the crop: ";
getline(cin,fcrop);
cout<<"Enter the cost per acre:$ ";
cin>>fcpa[i];
cout<<"Enter the yield: ";
cin>>fyield[i];
cout<<"Enter $/bishell: ";
cin>>fper[i];
cout<<"Enter the percentage increase: ";
cin>>fincrease[i];
cin.ignore(80,'\n');
}
}
void getCalc(int acres,float fcpa[],float fyield[],float fper[],float fincrease[],float fcost[],float fgrossmin[],float fgrossmax[],float fnetmin[],float fnetmax[],float fnetave[])
{
for (int i=0;i<4;i++)
{
int acres=1000;
fcost[i]=acres*fcpa[i];
fgrossmin[i]=acres*fyield[i]*fper[i];
fgrossmax[i]=fgrossmin[i]+(fgrossmin[i]*fincrease[i]/100);
fnetmin[i]=fgrossmin[i]-fcost[i];
fnetmax[i]=fgrossmax[i]-fcost[i];
fnetave[i]=(fnetmin[i]+fnetmax[i])/2;
}
}
void getPrint(float fnetmin[],float fnetmax[],float fnetave[])
{
for (int i=0;i<4;i++)
{
cout<<"The minumum profit is:$ "<<fnetmin[i]<<endl;
cout<<"The maximum profit is:$ "<<fnetmax[i]<<endl;
cout<<"The average profit is:$ "<<fnetave[i]<<endl;
}
}
In the prototype you have written at the start of the program, this is written.
void getData(string,float,float,float,float) ;
It should be this instead identical to the one in its definition.
void getData(string,float[],float[],float[],float[]);
The function prototype should be same in its declaration and implementation.