How to solve this problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
int main ()
{
	double x, y ;
		cout<<"Please enter the value for \"x\", this program will prints the value of \"y\""<<endl;
		cin>>x ;
		if (x==0) y= x+2 ;
		else if (x>0) y= x-6 ;
		else if (x<0) y= 1/3(x+1) ;
		else if (x==-1) 
			cout <<" The value of denominator is 0, math error, please enter other value " <<endl;
		else ;
cout <<"the value of \"y\" is"<<y<<endl ;
return 0;
}

I am doing my assignment, the compiler give error but I just dont know where it is -______-"

compiler give : 1>d:\desktop\cs115\assignment 1\assignment 1\question 1.cpp(10) : error C2064: term does not evaluate to a function taking 1 arguments
Maybe
y= 1/3(x+1) ; (line 10)
becomes
y= 1/3 * (x+1) ;
else if (x<0) y= 1/3(x+1) ;

This is correct on paper. It's not correct C++ syntax.

else if (x<0) y= (1/3.0)*(x+1) ;
or perhaps you meant
else if (x<0) y= 1/(3.0*(x+1));

Do be sure to read this too:
http://cnx.org/content/m18717/latest/

Thank you :)
Last edited on
LOL shame on me . super noob
one more q, when I enter -1 it didn't pop out
cout <<" The value of denominator is 0, math error, please enter other value " <<endl;
10
11
12
else if (x<0) y= 1/3(x+1) ;
else if (x==-1) 
cout <<" The value of denominator is 0, math error, please enter other value " <<endl;

You should probably put the else if (x==-1) check before the else if (x<0) line.

Otherwise it'll see that x < 0 first, and try to set y to 1/3(x+1). (and then skip the rest of the ifs)

You also don't need to write
else ; on line 13 in your code.
Last edited on
1
2
else if (x<0) y= 1/3(x+1) ;
else if (x==-1) cout << ..... 

You will never reach line 2 like this, because if x == -1, x < 0, and if x < 0, your program will never check for x == -1.
Maybe you may want to invert their order like this:
1
2
else if (x==-1) cout << .....
else if (x<0) y= 1/3(x+1) ;
never mind got it I must put them in the top
// ------------------------------
// This prog gets a DEPTH, and returns Celsius
// and Farenheit Temps.
// version #:
// written by:
// date written:
//
// modified to change local vars to global ones.
// and added local vars within functions as temp vars.
//---------------------------------------


#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;

float celsius_at_depth (int d, float c);
float fahrenheit_at_depth (float c, float f);

// Init'l the vars outside of main to make them global
// this way they can still be 'seen' inside of functions.
// Can be combined, but seperate until finished testing.
int depth = 0;
float cel = 0.0;
float fer = 0.0;

int main (void){

printf("\nPlease enter depth (in kilometres) inside the earth : ");
scanf_s( "%d", &depth );

celsius_at_depth (depth, cel);
fahrenheit_at_depth (cel, fer);

printf("\nHi dear, my name is Farah.\n");
printf("\nFor the depth of %d km, the temperatures are: ", depth);
printf("\n%.2f degrees Celsius or %.2f degrees Fahrenheit.\n\n", cel, fer);

system("PAUSE");
return (0);
}

float celsius_at_depth (int d, float c)
{
// Created a temp var to hold the calculation
// when ready, moved the temp var to the live-one
// to be returned & to be printed.
float val_celsius = (10 * d)+ 20;
cel = val_celsius;
return cel;
}

float fahrenheit_at_depth (float c, float f)
{
// Created a temp var to hold the calculation
// when ready, moved the temp var to the live-one
// to be returned & to be printed.
float val_fahrenheit = (1.8 * c )+ 32;
fer = val_fahrenheit;
return fer;

}



//the problem is the celsius and farenheit is not compile???
Topic archived. No new replies allowed.