3 Replies - 264 Views - Last Post: 21 November 2011 - 07:44 PM Rate Topic: -----

#1 nathanhux  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-November 11

Help with two programs (need help asap)

Posted 21 November 2011 - 05:23 PM

Alright my these two programs im having problems with. There due at 11 and i just cant figure out what I'm doing wrong i really need some help.

First Program :
This program has no errors, but when i run it in consol it will just let me put in my name and then it will shoot everything up not letting me put in anymore of the numbers. So basically it lets me put my name in then does the whole program itself without any of the other information given.

Heres the screen shot of what im talking about :
http://i43.tinypic.com/e9zrx4.jpg

Here is the code :
#include <iostream>
#include <cstdlib>
using namespace std;

int main ()
{

	
	cout << " Please enter your name : "; 
	char name;
	cin >> name;

	
	cout << " Please enter hours worked and minutes ";
	double hours_worked;
	cin >> hours_worked;

	
	cout << " Please enter your hourly wage : ";
	double hourly_wage;
	cin >> hourly_wage;

	int wage;
	
	
	if (hours_worked < 40)
	{
		wage = (40 - hours_worked) * (hourly_wage * (hourly_wage / 2));
	}
	
	else 
	{
		wage = hours_worked * hourly_wage;
	}
	
	cout << " Employe Name : " << name << "\n";
	cout << " Hourse Worked : " << hours_worked << "\n";
	cout << " Hourly Wage : " << hourly_wage << "\n";
	cout << " Money Earned : " << wage << "\n";
	
	return 0;

}







Now to the second program :

This program is giving me errors. I have copied the formula's down from our book where our teacher showed us to do it. But the formula is giving me the errors. It keeps showing "Call of overload 'pow(int&, int)' is ambiguous " ... For the life of me i dont know what is going on. Im just getting into programming and i dont know at all what this means.

Here is the code :
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;


int main ()
{
	cout << " Please enter a, b, and c (example 5 4 2) : ";
	int a;
	int b; 
	int c;
	cin >> a >> b >> c;
	
	cout << " Please enter a, b, and c (example 5 4 2) : ";
	int a1;
	int b1; 
	int c1;
	cin >> a1 >> b1 >> c1;
	
	cout << " Please enter a, b, and c (example 5 4 2) : ";
	int a2;
	int b2; 
	int c2;
	cin >> a2 >> b2 >> c2;
	
	int root1;
	int root2;
	double disc1 = pow(b, 2) - 4.0 * a * c;
	root1 = (-b + sqrt(disc1)) / (2.0 * a);
	root2 = (-b - sqrt(disc1)) / (2.0 * a);
	
	int root3;
	int root4;
	double disc2 = pow(b2, 2) - 4.0 * a2 * c2;
	root3 = (-b2 + sqrt(disc2)) / (2.0 * a2);
	root4 = (-b2 - sqrt(disc2)) / (2.0 * a2);
	
	int root5;
	int root6;
	double disc3 = pow(b2, 2) - 4.0 * a2 * c2;
	root5 = (-b2 + sqrt(disc3)) / (2.0 * a2);
	root6 = (-b2 - sqrt(disc3)) / (2.0 * a2);
	
	cout << root1 << root2 << root3 << root4 << root5 << root6;
	
	
	return 0;

}




If anyone could help me out that would be much appreciated

Thanks, Nathan

Is This A Good Question/Topic? 0
  • +

Replies To: Help with two programs (need help asap)

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,350
  • Joined: 25-December 09

Re: Help with two programs (need help asap)

Posted 21 November 2011 - 06:01 PM

For the first program.
In the following snippet:
	cout << " Please enter your name : ";
	char name;
	cin >> name;



A char can only hold 1 character. How many characters are in the value you entered? I would suggest that you use a std::string for this variable.

For the second program I suggest that you look up the pow() and sqrt() functions. Look at the number and the type of parameters these functions are expecting. Make sure the parameters you are supplying match what is expected. These functions expect floating point numbers and will usually have problems with integers.

Jim

This post has been edited by jimblumberg: 21 November 2011 - 06:06 PM

Was This Post Helpful? 1
  • +
  • -

#3 nathanhux  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-November 11

Re: Help with two programs (need help asap)

Posted 21 November 2011 - 06:07 PM

That helped with the first problem thanks alot!

But i re-edited the second program and cant figure at all how to do it i think ive put it in right but here is the problem my teacher asked :

Calculation of roots to a quadratic equation
--------------------------------------------

Write a C++ program that prints all solutions to the quadratic
equation ax^2 + bx + c = 0.

Your program should interactively read in the values for the
quadratic equation parameters a, b, c and then use
the quadratic formula to determine the roots. Please look
at the example 3.2, on page 130 of your textbook.

In addition to printing the value of the two roots, your program
should state the number of distinct real roots.

3 Test Cases:
-------------
In your screenshot(s) show the following test cases,

1. no real solutions
--------------------
take the formula for a parabola y = x^2 and push it up, for
example y = x^2 + 1. The graph of such a parabola does not
cross the x-axis, so there are no real solutions.

x^2 + 0*x + 1 = 0, has no real solutions

2. two distinct real solutions
------------------------------
x^2 + 0x - 1 = 0, has two distinct roots, namely 1 and -1

3. one solution
--------------
x^2 - 2x + 1 = 0, has one distinct real root, namely at 1


This is now my code :

#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

double quad1 (double a, double b, double c);
double quad2 (double a1, double b1, double c1);
double quad3 (double a2, double b2, double c2);

int main ()
{
	cout << " Please enter a, b, and c (example 5 4 2) : ";
	double a;
	double b; 
	double c;
	cin >> a >> b >> c;
	
	cout << " Please enter a, b, and c (example 5 4 2) : ";
	double a1;
	double b1; 
	double c1;
	cin >> a1 >> b1 >> c1;
	
	cout << " Please enter a, b, and c (example 5 4 2) : ";
	double a2;
	double b2; 
	double c2;
	cin >> a2 >> b2 >> c2;
	
	double root1;
	double root2;
	double root3;
	double root4;
	double root5;
	double root6;
	
	cout << root1 << root2 << root3 << root4 << root5 << root6;
	
	
	return 0;

}
double quad1 (double a, double b, double c)
{
int root1;
int root2;
double disc1 = pow(b, 2) - 4.0 * a * c;
root1 = (-b + sqrt(disc1)) / (2.0 * a);
root2 = (-b - sqrt(disc1)) / (2.0 * a);
	return root1, root2;
}
double quad2 (double a1, double b1, double c1)
{
int root3;
int root4;
double disc2 = pow(b1, 2) - 4.0 * a1 * c1;
root3 = (-b1 + sqrt(disc2)) / (2.0 * a1);
root4 = (-b1 - sqrt(disc2)) / (2.0 * a1);
	return root3, root4;
}
double quad3 (double a2, double b2, double c2)
{
int root5;
int root6;
double disc3 = pow(b2, 2) - 4.0 * a2 * c2;
root5 = (-b2 + sqrt(disc3)) / (2.0 * a2);
root6 = (-b2 - sqrt(disc3)) / (2.0 * a2);
	return root5, root6;
}


Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6051
  • View blog
  • Posts: 23,483
  • Joined: 23-August 08

Re: Help with two programs (need help asap)

Posted 21 November 2011 - 07:44 PM

return root3, root4;


You can only return a single value through a return statement. Check out the function tutorials linked in my signature for pass-by-reference, which will allow you to return more than one value from a function.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1