Help with using variables in C++

mayorredbeard

Daemon Poster
Messages
1,246
My code so far:

--------------------------------------------------------------------------

#include <iostream>
#include <conio.h>
#include <math.h>

//declare variables
using std::cin;
using std::cout;
int main()
{
double rateOne, rateTwo, mOne, mTwo;
cout << "Enter rate one: ";
cin >> rateOne;
cout << "Enter rate two: ";
cin >> rateTwo;
cout << "Enter 'm' one: ";
cin >> mOne;
cout << "Enter 'm' two: ";
cin >> mTwo;

//Peform calculations

double calculatedRate = rateOne * rateTwo;
double dividedM = mTwo * mOne;
double calculatedM = sqrt(dividedM);
cout << calculatedM << "=" << calculatedRate;
getche();
return 0;

}

--------------------------------------------------------------------------

Alright, so what this program does is it allows me to enter in four variables. It's basically the first of many programs to allow me to do some of my chem homework, (even though this is so much harder then just doing it, hehe). Anyways i've only recently gotten into C++ programming, and for that matter programming at all. What I want to be able to do is enter in a variable for rateOne, rateTwo, mOne, or mTwo, and then solve for the variable. But it doesn't work, obviously. How could I rewrite this code to allow me to do it. I don't want someone to edit it for me and give me the code, I want someone to tell me what I need to learn to do this myself.
 
Solve for what variable? If you had an equation for us to solve for I could help explain what you'd want to do. Also that wouldn't always be a true statement. calculatedM = cauclatedRate isn't always true. I can't really tell what you're asking for
 
#include <iostream>
#include <conio.h>
#include <math.h>

//declare variables
using std::cin;
using std::cout;
int main()
{
double rateOne, rateTwo, mOne, mTwo;
cout << "Enter rate one: ";
cin >> rateOne;
cout << "Enter rate two: ";
cin >> rateTwo;
cout << "Enter 'm' one: ";
cin >> mOne;
cout << "Enter 'm' two: ";
cin >> mTwo;

//Peform calculations

double calculatedRate = rateOne * rateTwo;
double calculatedM = mTwo * mOne;
double sqauredRate = calculatedRate * calculatedRate;
cout << calculatedM << "=" << calculatedRate;
getche();
return 0;

}
-----------------------------------------------------

Okay thats the latest part of my code. I am trying to have this program, basically solve this algebraic expression (grahams law):

cf34796180a7df6bbc0f7fbf132e8efd.png


Now to solve this in real life without the use of a computer program you would be given the value of three variables. Let's say I was given rateTwo, mOne, and mTwo. Given those three numbers, you could solve for the unknown variable, rateOne. I'll use some made up numbers so you can see how I would usually do this:

rateOne = 'X'
rateTwo = 8
mOne = 4
mTwo = 16

1: X/8 = The square root of (16/4)
2: 1/8x = The sqaure root of 4
3: 1/8x = 2
4: x = 16

However the problem I run into is if I enter in 'X' for either rateOne, rateTwo, mOne, or mTwo, it automatically assigns X a value and uses that number ,rather then following the process above, and allowing me to continue to solve for x.
 
calculatedM / rate2 = rate1

calculatedM * rate1 = rate2

calculatedRate^2 * m2 = m1

calculatedRate^2 / m1 = m2

I believe my math is correct. If I did the algebra right. One way to sovle this is to have a switch case.

Ask for what you're solving for, depending upon what you're solving for you can use the approperiate equation.

I think it'd be easier if you broke this down into functions and had each one have its own purpose. Like one ask for what you're solving for, another to get the values of the variables, a 3rd to solve, and possibly a 4th to display an answer if you don't include that in 3rd.
 
completly redid the code, but ive got some problems due to my limited knowledge.

-------------

//INCLUDE FILES

#include <iostream>
#include <conio.h>
#include <math.h>

using std::cin;
using std::cout;

//MAIN FUNCTION
int main()

{
char problem;
cout << "Are you solving for rateOne, rateTwo, mOne, or mTwo?: ";
cin >> problem;

if problem = rateOne;
//SOLVING FOR RATE ONE

//Declare the variables

double rateOne, rateTwo, mOne, mTwo answer;
cout << "Enter rate two: ";
cin >> rateTwo;
cout << "Enter 'm' one: ";
cin >> mOne;
cout << "Enter 'm' two: ";
cin >> mTwo;

//Peform Calculations and display result
answer = rateTwo * (sqrt (mOne / mTwo));
cout << "Rate one equals: " answer;

if problem = rateTwo;
//SOLVING FOR RATE TWO

//DECLARE THE VARIABLES
double rateOne, rateTwo, mOne, mTwo answer2;
cout << "Enter rate one: ";
cin >> rateOne;
cout << "Enter 'm' one: ";
cin >> mOne;
cout << "Enter 'm' two: ";
cin >> mTwo;

//Peform Calculations and display result
answer2 = rateOne / (sqrt (mOne / mTwo));
cout << "Rate two equals: " answer2;

if problem = mOne;
//SOLVING FOR M ONE

//DECLARE THE VARIABLES
double rateOne, rateTwo, mOne, mTwo, answer3;
cout << "Enter rate one: ";
cin >> rateOne;
cout << "Enter 'm' one: ";
cin >> mOne;
cout << "Enter 'm' two: ";
cin >> mTwo;

//Peform Calculations and display result
answer3 = mTwo / ((rateOne / rateTwo) * (rateOne / rateTwo));
cout << "M one equals: " answer3;

if problem = mTwo;
//SOLVING FOR M TWO

//DECLARE THE VARIABLES
double rateOne, rateTwo, mOne, mTwo, answer4;
cout << "Enter rate one: ";
cin >> rateOne;
cout <<"Enter rate two: ";
cin >> rateTwo;
cout << "Enter 'm' one: ";
cin >> mOne;
cout << "Enter 'm' two: ";
cin >> mTwo;

//Peform Calculations And Display Result
answer4 = mOne * ((rateOne / rateTwo) * (rateOne / rateTwo));
cout << "M two equals: " answer4;

else;
goto line 1;



-----

I get a variety of errors with that. Help? :-D
 
First of all... char is one character. Not a string of chars. You either want to include strings, or use a character array.

Second of all = is the assign operator. == is to check if things are equal

You don't need a ; after if. And usually if is used like this

if (condition true)
{
execute code between these brackets
}
Also include the brackets, or if will only execute the preceding line if true. If you don't, it'd ask you for the variables 4 times.

Another thing you can't compare it to just rateOne. The compiler thinks rateOne is a varaible and then says it's not declared. use "quotes" to seperate variables from words, data whatever.

//example code to give you an idea of how to use them things...

#include <iostream>
#include <string>

using namespace std;

int main ()

{
string str1;

cin >> str1;

if (str1=="asdf")
{
cout << "string1 " << str1 << endl;
}

else
{
exit(1);
}

return 0;
}
 
Few more errors I just noticed...
If you want to cout << "stuff here" << then_a_variable;
you're going to have to use << again.

So
cout << "M two equals: " answer4;
should be...
cout << "M two equals: " << answer4;

Also you left out some commas when declaring variables.

Don't forget to close int main off with the last close bracket }...

If you want a working version of your source code
http://rewtguy.sweon.net/grahamslaw.cpp
 
okay got it! and i did it without just copying and pastign your code and using it :p, but by finding the bug myself. hehe thanks a lot rewtguy! how would i put in a option to restart the program if something invalid is entered for 'problem'. Like if you typed in, cheesedoodles, instead of mOne, mTwo, rateOne, or rateTwo, how would i include a piece of code to give the user the option to restart from the beginning rather then just have it say, "Invalid selection, bailing."
 
Well if you want to do it again. I guess you could goto, or have a loop, break them down into functions and have an if statement that recalls the beginning function. If you do decide to do that though you should include an option to exit.

if (string == "exit")
{
exit (0); // zero this time cause there's no error
}
 
Back
Top Bottom