Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 02-17-2006, 03:11 PM   #1
Daemon Poster
 
mayorredbeard's Avatar
 
Join Date: Nov 2004
Posts: 1,246
Default Help with using variables in C++

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.
__________________

__________________
Karma/rep is always appreciated
01010010011001010110010001100010011001010110000101 11001001100100
010011110111010101110100

There are only 10 kinds of people in this world. Those who can read binary, and those who can't.
mayorredbeard is offline   Reply With Quote
Old 02-17-2006, 05:26 PM   #2
Daemon Poster
 
RewtGuy's Avatar
 
Join Date: Dec 2004
Posts: 595
Send a message via AIM to RewtGuy
Default Re: Help with using variables in C++

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
__________________

__________________
Windows: A thirty-two bit extension and GUI shell to a sixteen bit patch to an eight bit operating system originally coded for a four bit microprocessor and sold by a two-bit company that can't stand one bit of competition.
RewtGuy is offline   Reply With Quote
Old 02-18-2006, 12:51 PM   #3
Daemon Poster
 
mayorredbeard's Avatar
 
Join Date: Nov 2004
Posts: 1,246
Default Re: Help with using variables in C++

#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):



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.
__________________
Karma/rep is always appreciated
01010010011001010110010001100010011001010110000101 11001001100100
010011110111010101110100

There are only 10 kinds of people in this world. Those who can read binary, and those who can't.
mayorredbeard is offline   Reply With Quote
Old 02-18-2006, 02:53 PM   #4
Daemon Poster
 
RewtGuy's Avatar
 
Join Date: Dec 2004
Posts: 595
Send a message via AIM to RewtGuy
Default Re: Help with using variables in C++

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.
__________________
Windows: A thirty-two bit extension and GUI shell to a sixteen bit patch to an eight bit operating system originally coded for a four bit microprocessor and sold by a two-bit company that can't stand one bit of competition.
RewtGuy is offline   Reply With Quote
Old 02-19-2006, 09:46 PM   #5
Daemon Poster
 
mayorredbeard's Avatar
 
Join Date: Nov 2004
Posts: 1,246
Default Re: Help with using variables in C++

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
__________________
Karma/rep is always appreciated
01010010011001010110010001100010011001010110000101 11001001100100
010011110111010101110100

There are only 10 kinds of people in this world. Those who can read binary, and those who can't.
mayorredbeard is offline   Reply With Quote
Old 02-19-2006, 10:41 PM   #6
BSOD
 
Computergen's Avatar
 
Join Date: Dec 2004
Posts: 3,175
Default Re: Help with using variables in C++

By adding

return 0;

}


at the end it reduces the errors from 27 to 26...every little bit helps?
Computergen is offline   Reply With Quote
Old 02-20-2006, 01:08 AM   #7
Daemon Poster
 
RewtGuy's Avatar
 
Join Date: Dec 2004
Posts: 595
Send a message via AIM to RewtGuy
Default Re: Help with using variables in C++

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;
}
__________________
Windows: A thirty-two bit extension and GUI shell to a sixteen bit patch to an eight bit operating system originally coded for a four bit microprocessor and sold by a two-bit company that can't stand one bit of competition.
RewtGuy is offline   Reply With Quote
Old 02-20-2006, 01:20 AM   #8
Daemon Poster
 
RewtGuy's Avatar
 
Join Date: Dec 2004
Posts: 595
Send a message via AIM to RewtGuy
Default Re: Help with using variables in C++

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
__________________
Windows: A thirty-two bit extension and GUI shell to a sixteen bit patch to an eight bit operating system originally coded for a four bit microprocessor and sold by a two-bit company that can't stand one bit of competition.
RewtGuy is offline   Reply With Quote
Old 02-20-2006, 12:04 PM   #9
Daemon Poster
 
mayorredbeard's Avatar
 
Join Date: Nov 2004
Posts: 1,246
Default Re: Help with using variables in C++

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."
__________________
Karma/rep is always appreciated
01010010011001010110010001100010011001010110000101 11001001100100
010011110111010101110100

There are only 10 kinds of people in this world. Those who can read binary, and those who can't.
mayorredbeard is offline   Reply With Quote
Old 02-20-2006, 02:14 PM   #10
Daemon Poster
 
RewtGuy's Avatar
 
Join Date: Dec 2004
Posts: 595
Send a message via AIM to RewtGuy
Default Re: Help with using variables in C++

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
}
__________________

__________________
Windows: A thirty-two bit extension and GUI shell to a sixteen bit patch to an eight bit operating system originally coded for a four bit microprocessor and sold by a two-bit company that can't stand one bit of competition.
RewtGuy is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 02:04 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0