Hey i've been programming in C++ for two days son my problems here are expected for newbs like me, my program is for asking for intensity at a distance of 1m, and then asking for another value that represents the intensity at a certain distance which the user wishes to determine, then it calculates the intensity at the new distance using the inverse square law.
#include <iostream>
#include <string>
#include <sstream>
usingnamespace std;
int main()
{
int intense; //will use this for retaining intensity for 1m
float answer;
int distance;
int useless
cout << "What intensity is for 1 unit of measurement?";
getline (cin, intense);
cout << "What is the new distance for which you are looking to find the intensity of?";
getline (cin,distance);
1/(distance*distance)*intense = answer;
cout << "intensity is" << answer << "\n";
cout << "press enter to close this program.";
getline (cin,useless);
return 0;
}
it wouldn't compile and came up with a few confusing error messages, please keep in mind im a newb and only been doing this for two days
#include <iostream>
#include <string>
#include <sstream>
usingnamespace std;
int main()
{
int intense; //will use this for retaining intensity for 1m
int answer;
int distance;
int useless;
cout << "What intensity is for 1 unit of measurement?";
cin >> intense;
cout << "What is the new distance for which you are looking to find the intensity of?";
cin >> distance;
answer = 1/(distance*distance)*intense; //inverse square law, bullshit i learned in physics
cout << "intensity is " << answer << "\n";
cout << "press enter to close this program.";
cin >> useless;
return 0;
}
ok this is my new code, but everytime the answer is returned as 0, any ideas? i know its something obvious to the veterans who are here.
#include <iostream>
#include <string>
#include <sstream>
usingnamespace std;
int main()
{
double intense; //will use this for retaining intensity for 1m
double answer;
double distance;
double useless;
cout << "What intensity is for 1 unit of measurement?";
cin >> intense; // sends user input to the value of variable 'intense'
cout << "What is the new distance for which you are looking to find the intensity of?";
cin >> distance; // same deal except the inputs represents the variable 'distance'
answer =1/(distance*distance)*intense ; //inverse square law, bullshit i learned in physics, supposed to substitute the variables
cout << "intensity is " << answer << "\n";
cout << "close program? (Y/N)";
cin >> useless;
return 0;
}
ok it works perfectly, but could someone shed some light as to why i had to make all the values double??
In C and C++, the result of dividing integers by integers is also integral, not floating point.
That is, 1 / 5 == 0 according to integer arithmetic, but 1.0 / 5.0 == 0.2 if either the divisor or dividend are floating point.
Making both distance and intense floating point solved the problem, as would have making only one of them, so long as answer was a double.
ok, so thank you.
let me have a try out tomorrow to absorb these new ideas.
also, there are other equations that i want my program to compute, like things to do with calculating velocity or frequency or wavelength using any two other variables given to figure out the 3rd, but i havnt learnt yet how to get the main to act as a menu like thing, and then use my intensity and wavelength equations in the function, and make it all link together to work. i'll give it a tinker tomorrow and show how it all works out, i'll post some code and some1 could tell me where i went wrong, thanks.
#include <iostream>
#include <string>
#include <sstream>
usingnamespace std;
int main()
{
int intense;
double answer, distance, useless;
loop:
cout << "What intensity is for 1 unit of measurement?";
cin >> intense;
cout << "What is the new distance for which you are looking to find the intensity of?";
cin >> distance;
answer =1/(distance*distance)*intense ;
cout << "intensity is " << answer << " Lux\n";
if (5 != 3) goto loop;
cout << "close program? (Y/N)";
cin >> useless;
return 0;
}