Alright, I have a project due in a couple of days, and i'm stuck, but I also need some suggestion. I'm doing a Sine/Cosine Law program, and I must have 2 functions(1 of them need must be a recursive function), a string, and arrays included in my program. The thing is, I have no clue how to incorporate strings and arrays into my program. I have include 2 functions, but neither of them are recursive as I know so far. Also, it isn't compiling properly. Can you guys give suggestions and figure out the problem with my program?
#include <iostream>
#include <string>
#include <math.h>
usingnamespace std;
void sineLaw(void);
void cosLaw(void); //change parameters(explained in function below)
int main(){
char select;
double a,b,A,B; //might want to turn this into an array for your assignment, maybe labeled "Triangle[4]"
//Triangle[0] -- a
//Triangle[1] -- b
//Triangle[2] -- A
//triangle[3] -- B
cout<<"What would you like to figure out?"<<endl;
cout<<"a)SineLaw"<<endl<<"b)CosineLaw"<<endl;
cout<<"Select either a or b"<<endl;
cin>>select;
if(select=='a'){
cout<<"Enter the side length of 'a' & 'b'"<<endl;
cout<<"Enter the size of angle 'A' & 'B' "<<endl;
cout<<"a:";
cin>>a;
cout<<endl;
cout<<"b:";
cin>>b;
cout<<endl;
cout<<"A:";
cin>>A;
cout<<endl;
cout<<"B:";
cin>>B;
cout<<endl;
sineLaw(//Triangle);
}
elseif(select=='b'){
}
else
cout<<"Your input is invalid. Valid options are a or b"<<endl;
return 0;
}
void sineLaw(void){
double a,b,c,A,B,C; //you create 6 variables here, all of these values are initially set to zero
//you need to pass the values from main over to this function, the best option would be to pass
//an array holding these values into the function using parameters(void sineLaw(double Ar[]))
double PI=3.14;
C=180-(A+B);
c = (A*(sin((C*PI/180))))/((sin(A*PI/180)));
cout<<"Angle C:"<<C;
cout<<"Length of c:"<<c;
}
Your functions are not correctly defined.
You have indicated void for return and void for arguments passed by the user. I would expect that you want the values input by the user to be processed by the function. That means the function must be written so it accepts input and returns a value.
hth
@ Need4Sleep
Ahh that must be why it wasn't compiling properly. Thanks. Oh, do you mind explaining the how to turn a,b,A,B into arrays.
@ResidentBiscuit
I'm new to these forums, so I have no idea how to use code tags.
Tradechat: he tells you how in his comment. If you don't understand you should consult your textbook looking under pass by value arguements to functions. Any text on the screen can be made a string.
I know your trying to get this done but your questions seem like you just want us to do the work for you. These questions are explained thoroughly in any textbook OR online tutorial. If you read them and then don't understand the concepts I would suggest coming back and asking for a more in depth explination.
@Senjai
dont be so harsh, if you read his first post:
Can you guys give suggestions and figure out the problem with my program?
he doesn't ask for any of it to be done for him, he is just asking for help. Please don't go around spitting on the newer users, it discourages them from posting anything.
With the topic:
i recommended scratching the whole double values and instead creating an array to hold the data. Like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
std::string Gerald,John,Jacob,Johnson; //pretend the names correspond to the data they hold(Gerald = "Gerald")
//passing these as arguments can be a pain:
void NameSorter(int i, int i2, int i3... etc... etc..)
//int main:
//calling function with parameters
NameSorter(Gerald,John,Jacob,Johnson....); //can get messy.
//-------------------------------------------------------------
std::string Names[4] = {//Data}
//passing an array through a function is neat and organized
void NameSorter(std::string Ar[]);
//int main:
//calling function with parameters
NameSorter(Names);
//Note** Arrays are passed by reference through functions by default(modifying the contents of the array inside a function will do the same
//for the array outside of the function)
@Need4Sleep
Somewhat makes sense, but I what I don't understand is how I would put these steps into my program.
EDIT: Also, it seems it gives an error when information from NameSorter goes to NameSorter("the 4 strings")
Error is: No suitable conversation function from "std::string" to "int exist. Can you elaborate on that? Thanks
void sineLaw(void) // Something's wrong with the contents of ( )
{
double a, b, c, A, B, C; // You've declared some new variables
double PI = 3.14;
C = 180 - ( A + B ); // Here's a hint, if I told you to write down the value of A and B on a piece of paper, what would you write down?
c = ( A * ( sin ( ( C * PI / 180 ) ) ) ) / ( ( sin ( A * PI / 180 ) ) ); // Probably a little too much in terms of spacing, but if you want others
// to read your code, this is along the lines of what you want to do
cout << "Angle C:" << C << endl;
cout << "Length of c:"<< c << endl;
}