equations

Started by
2 comments, last by sundaythedog 22 years, 2 months ago
How would I write something simple like a = 3 + c and have the user input A and the computer output C? would it be something like:
  
int a, b, c;

cout << "The format is a = 3 + c./nInsert a: ";
cin >> a;
if (a < 3)
{
c = a - b;
else 
cout << "Enter a number greater than 3 until I can get this buggernut figured out, OK?!!?";
}
cout << "c = " << c;
  
I don''t have any compilers with me right now.... I''ve just never tried an equation...is there an easier way? The Light shine on you
The Light shine on you
Advertisement
Ok, I took a second look. Try this:

    int a=0,    b=3,    c=0;cout << "The format is a = 3 + c./nInsert a: ";cin >> a;if (a < 3){c = a - b;}else cout << "Enter a number greater than 3 until I can get this buggernut figured out, OK?!!?";cout << "c = " << c;      


It's a good idea to initialize all your variables as soon as possible. You didn't tell the compiler that 'b' was assigned the value '3' and 'c' was output regardless of the outcome of the if statement. Your brackets were off too.



Edited by - ShadeStorm on February 19, 2002 8:57:57 AM
ShadeStorm, the Day_Glo Fish
C++ does not have the syntax to allow intentional representation of mathematical equations. You have to restructure the equation in a manner that can be expressed in C++ notation. So your example would become c = a - 3, where a has already been captured from the user.

--
1st law of programming: Any given program, when running, is obsolete.
    #include<iostream>using namespace std;main(){	int a = 0;	int b = 3;		cout << "The format is a = 3 + c.\nInsert a: ";	cin >> a;	int c = a - b;	cout << "c = " << c << endl;	cout << a << " = " << b << " + " << c < endl;	return 0;}    

I took out the if because it was just for practice.
I also added the full ewuation at the end.

Time for funsctions!!

The Light shine on you

Edited by - sundaythedog on February 19, 2002 6:49:47 PM
The Light shine on you

This topic is closed to new replies.

Advertisement