Katas Posted October 4, 2010 Posted October 4, 2010 You could just do something like answer = [(a + b) / (c- d) * (x^2) / y] pow(answer, 2) Share this post Link to post Share on other sites More sharing options...
CowKing Posted October 4, 2010 Posted October 4, 2010 uhm, You should probably document this stuff. If you can't explain it to yourself how are you going to under stand it? I believe C++ uses "//" as a remark line Share this post Link to post Share on other sites More sharing options...
Miek Posted October 4, 2010 Posted October 4, 2010 (edited) I'm making a plethora of comments as I go. Comments are my friend or I'd come back to work on the program later and think, "lolwut is this?" /* blah blah blah */ is also a comment. Useful for commenting out a block of code or something like that. OK! I think I have the program complete. I removed a lot of comments because they were no longer needed (some were as simple as //come back and check on this). // Using user assigned values to a, b, c, d, x, y, and z. #include <iostream> #include <cmath> #include <iomanip> using namespace std; int main() { std::cout << setprecision(4); //Limiting the size of possibly very large numbers and long decimals. float a, b, c, d, x, y, z; cout<<"This program will accept user inputted values and then perform a few\nmathematical operations with them."<<endl; cout<<"Please only use numbers for all of the following. Use only one decimal point\n(.) if it is needed. For example, 1.23"<<endl; cout<<""<<endl; cout<<"Please enter a value for a."<<endl; cin>>a; cout<<"Please enter a value for b."<<endl; cin>>b; cout<<"Please enter a value for c."<<endl; cin>>c; cout<<"Please enter a value for d."<<endl; cin>>d; cout<<"Please enter a value for x."<<endl; cin>>x; cout<<"Please enter a value for y."<<endl; cin>>y; cout<<"Please enter a value for z. This is the final number entry."<<endl; cin>>z; cout<<""<<endl; cout<<"You have selected: "<< endl; cout<<"a = "<<a<<endl; cout<<"b = "<<b<<endl; cout<<"c = "<<c<<endl; cout<<"d = "<<d<<endl; cout<<"x = "<<x<<endl; cout<<"y = "<<y<<endl; cout<<"z = "<<z<<endl; cout<<""<<endl; cout<<"The following are some mathematical formulas performed using your values."<<endl; cout<<""<<endl; cout<<"x + 2y + z/2 = "<<x+(2*y)+(z/2)<<endl; cout<<""<<endl; cout<<"a^2 + 2ab + b^2 = "<<(pow(a, 2))+(2*a*b)+(pow(b, 2))<<endl; cout<<""<<endl; float answer_power = (pow(x, 2))/y; cout<<"(a + b)/(c- d)*[(x^2)/y]^2 = "<<(a+b)/(c-d)*(pow(answer_power, 2))<<endl; cout<<""<<endl; float answer_sqrt = pow((a+b), 2); cout<<"x + square_root[(a + b)^2] = "<<x+(sqrt(answer_sqrt))<<endl; cout<<""<<endl; cout<<"-x^[sin(y)] = "<<(-x)+(sin(y))<<endl; cout<<""<<endl; return 0; cin.get() } No errors while compiling, and it doesn't crash! ...Unless you type in a letter when it asks for a number. Then it goes all wtf and does some funky output with e (mathematical constant). I would upload the .exe, but I guess I can't. You'll just have to take my word for it that it's all good. XD Edited October 4, 2010 by Miek Share this post Link to post Share on other sites More sharing options...
CowKing Posted October 4, 2010 Posted October 4, 2010 You could try to write it where it checks whether it's a real number or a letter. Like, the program not accepting the input, prompting the user to use a real number, and forcing them to input again. Share this post Link to post Share on other sites More sharing options...
Miek Posted October 5, 2010 Posted October 5, 2010 OK. Something weird is happening: Even with cin.get() at the end of the program regardless if I put it after or before return 0; the program will NOT pause saying "Press any key to continue..." It works with system("PAUSE"), but I want to use something else because that is a Windows specific command. Share this post Link to post Share on other sites More sharing options...
DanielB Posted October 17, 2010 Posted October 17, 2010 Sorry if this thread is a bit old but the question has not been answered and I feel some things need to be explained. I think part of the problem you have is you don't understand why some things are happening. The reason your program appears to be blinking on and off is because nothing is stopping it, call the program from cmd.exe and you wont have any problems. Just type: cd "working directory with quotes" program.exe Mine would look like cd "C:\Users\Dan\Documents\Visual Studio 2008\Projects\WindowsApplication1\WindowsApplication1\bin\Debug" WindowsApplication1.exe Now the program will run, give its console output and not close the window. Next up you need to understand the nature of the return command. The return command ends the function (or in this case the program), absolutely nothing runs if it is placed after a return statement. It is convention to use return 0 with a main() function, it becomes important later. Just know that when a function calls the return statement, it ends. Since console programs in real-world applications are designed to either not have a long term pretense or be ran form cmd.exe there is no "good" way to pause a console program from exiting. Using the Windows specific system call is the best way. I don't understand why cin.get is not working, try the following code #include <iostream> using namespace std; int main(int argc, char **argv) { cout << "Hello World"; cin.get(); return 0; } Share this post Link to post Share on other sites More sharing options...
Flibo Posted October 17, 2010 Posted October 17, 2010 (edited) I don't understand why cin.get is not working Didn't you just answer it yourself? There's return 0; before cin.get(); in his code. EDIT: Oh, he corrected that, my bad However, his piece of code doesn't feature a semicolon after cin.get(), but wouldn't the compiler give a warning in that case? Edited October 17, 2010 by Flibo Share this post Link to post Share on other sites More sharing options...
Miek Posted October 18, 2010 Posted October 18, 2010 (edited) EDIT: I figured out me previous issues. Now, though, I have a new problem. It reads: "Write a C++ program that reads data from a file whose name is input by the user, and that outputs the first word following each of the first three commas in the file. For example, if the file contains the text of this problem, then the program would output and if then Assume that a comma appears within at least every 200 characters in the file. Be sure to use proper formatting and appropriate comments in your code. Provide appropriate prompts to the user. The output should be labeled clearly and formatted neatly." I know how to read and write from one file to another using <fstream>, but I've got no idea how to allow the user to name the INPUT file. I'm really lost here. I figured out how to assign a user defined name to an OUTPUT file... But not an extension (l want to use the .txt extension). Any ideas? This is due tomorrow (has to be submitted online by 11:55pm). I've been working on this assignment all week and I just can't get this figured out. Any kind of help at all is GREATLY appreciated. Well... I've been trying to make SOMETHING with this program work for the past seven hours (albeit with breaks for things like a bite to eat and using the washroom) and came up empty. My plan: Wake up, check for responses. Go to classes for the day (which include Comp-Sci). Come home, arriving at 3:30pm. Code. Code. Check for more responses from people that know what they're doing. Code. Code. Code. Hopefully, make something that works. Even if I don't come up with anything that works, I can still submit what source code I have for partial marks. I'd rather get a poor mark than a zero. Edited October 18, 2010 by Miek Share this post Link to post Share on other sites More sharing options...
Dariuas Posted October 18, 2010 Posted October 18, 2010 Well, I am not going to do this for you But look at the way fstream works ostream fileName; fileName.Open("this is a string"); So, it would seem to me that if you apply a variable to the fileName.Open instead of typing in the text allowing the user to Input that file name would be attainable by asking the user for a variable and putting that variable in place of "this is a string" Share this post Link to post Share on other sites More sharing options...
DanielB Posted October 18, 2010 Posted October 18, 2010 (edited) Try something like #include <iostream> #include <string> using namespace std; string filename; cin >> filename; filename = filename + ".txt"; cout << filename.c_str(); //c_str outputs the string as a normal char* format, you will need that to use it in most standard inputs. I just wrote that and have not tested it, but that is a basic proof of concept on how to allow the user to input a name and append an extension after it. Edited October 18, 2010 by DanielB Share this post Link to post Share on other sites More sharing options...
Miek Posted October 18, 2010 Posted October 18, 2010 (edited) I figured out how to assign a user-defined string variable to the input or output. My main trouble was figuring out exactly what to call the input file when I created the .dat and put some text in it. I couldn't figure out how to define the extension of the user-named file, either. It always showed up as just "type: file." I'll work on this more today after class, hopefully we'll go over something in the lecture session that will help. Edited October 18, 2010 by Miek Share this post Link to post Share on other sites More sharing options...
Miek Posted October 18, 2010 Posted October 18, 2010 (edited) EDIT: Nevermind, I've got what I had asked figured out. I'm just still having trouble allowing a user-defined input file. For now, I have an input file aptly called "input.dat" My program. #include <iostream> #include <fstream> #include <string> using namespace std; int main() { string filename, after_comma1, after_comma2, after_comma3; cout << "This program will let the user choose a filename. Then, it will show the first\nword following each of the first three commas in the file." << endl; cout << "\nPlease enter your desired filename." << endl; cin >> filename; filename = filename + ".dat"; ifstream inData; inData.open(filename); inData.ignore(200, ','); inData >> after_comma1; cout << after_comma1 << endl; inData.ignore(200, ','); inData >> after_comma2; cout << after_comma2 << endl; inData.ignore(200, ','); inData >> after_comma3; cout << after_comma3 << endl; inData.close(); return 0; } So far it works if you choose to enter "input" as the filename. All I need is for the name of the input file to be able to dynamically change depending on user input. Edited October 18, 2010 by Miek Share this post Link to post Share on other sites More sharing options...
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now