Thread: c++ programming help please

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    3

    c++ programming help please

    Hi this is my first year at uni. Im studying engineering. Ive been given an assignment but im not sure what to do. If anybody can help me start off it would be great. Thanks in advance.




    Problem Statement:

    Write a program that displays the following menu:
    First Year Physics Calculator
    1. Calculate the period of oscillation of a pendulum
    2. Calculate the maximum projectile height
    3. Quit

    Enter your choice (1-3):
    If the user enters 1, the program should ask for the length of the pendulum in meters and then display the period of oscillation in seconds. Use the following formula:

    where
    T = the period in seconds
    L = the length of the pendulum in meters
    g = the acceleration due to gravity in ms-2.
    Use 3.14159 for π. The program should display an error message if the length of the pendulum is less than 0 meters.

    For valid inputs, the period of oscillation displayed should be formatted in fixed-point notation, with 2 decimal points of precision.

    If the user enters 2, the program should ask for the initial velocity of the projectile in ms-1 and the initial angle of projectile in degrees. If the user enters an angle greater than 90 degrees or less than 0 degrees, the program should display an error message. Also if the user enters a velocity less than 0 ms-1, the program should display an error message. For valid inputs calculate and display the maximum projectile height using the following formula:

    where
    v = the initial velocity of the projectile in ms-1
    α = the initial angle of the projectile
    g = the acceleration due to gravity in ms-2.

    For valid inputs, the period of oscillation displayed should be formatted in fixed-point notation, with 3 decimal points of precision.
    If the user enters 3, the program should exit by displaying a message “Quitting …”.

    For all other inputs the program should display an error message.
    Use 9.8 m/s for the acceleration due to gravity (value of g).

    For choices 1 and 2, the output for valid inputs should also be written to a file called prob1_<studentID>.txt in C:\Temp directory where <studentID> is your actual student ID. The input values must also be shown in the output file. The following show samples of the output file:

    Sample output file for Choice 1:
    Calculation of the period of oscillation of a pendulum
    The length of the pendulum = 10.12 m/s
    Period of oscillation = 6.39 seconds

    Sample output file for Choice 2:
    Calculation of the maximum projectile height
    Initial velocity of projectile = 10.323 m/s
    Initial angle of projectile = 45.343 degrees
    The maximum height of projectile = 3.868 meters

    If an error occurred in opening the output file, an error message should be displayed. For invalid inputs it is not necessary to write any outputs to the file.




    EDIT: Im not asking for anyone to do my homework, Im asking for help.
    Last edited by farah0718; 03-24-2008 at 06:17 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Please read our homework policy.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Quote Originally Posted by farah0718 View Post
    EDIT: Im not asking for anyone to do my homework, Im asking for help.
    But you are asking for someone to do your homework. You gave the full description of the problem, but you asked no specific question about it. What is giving you trouble? Is this a class teaching you C++ programming? Does the class have a book? Have you opened and read the book? Almost always, a homework assignment follows closely with reading in the book.

    If you have specific questions on a piece of the problem you are stuck on, people might be able to guide you. But until you show us something you've tried that isn't working, it just looks like you are trying to get someone else to do the work.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    62
    read the hw policy ask specific question

  5. #5
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179
    Well, what you need to do is write a program that displays the following menu:
    First Year Physics Calculator
    1. Calculate the period of oscillation of a pendulum
    2. Calculate the maximum projectile height
    3. Quit

    Enter your choice (1-3):

    and then reads input from the user. If the user enters 1, the program call a function (I'd recommend pendulum() ) which would ask for the length of the pendulum in meters and then calculate the period of oscillation in seconds using your formula.

    You can use printf to display formatted output in fixed-point notation, with 2 decimal points of precision like this:
    Code:
    printf("%.2f", period);
    where period is a float or double that you calculated.

    If the user enters 2, the program should call a function that ask for the initial velocity of the projectile in ms-1 and the initial angle of projectile in degrees. Check if the user enters an angle greater than 90 degrees or less than 0 degrees, and display an error message. Also if the user enters a velocity less than 0 ms-1, the program should display an error message. For valid inputs calculate the maximum projectile height using your formula. You can use printf as above.

    If the user enters 3, the program should exit.

    Once you've got that, writing to a file is simply a matter of doubling your output to a file you opened with a second statement, probably sprintf(filename, output, variables), after every printf() statement.

    Hope this helps.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    3
    guesst thank u for the help, at least you have a purpose being on this site unlike other boring people

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Quote Originally Posted by farah0718 View Post
    guesst thank u for the help, at least you have a purpose being on this site unlike other boring people
    I'm very amused that you found his reply that helpful. He just paraphrased what you wrote. And any actual helpful information would have been found in the beginning chapters of a C++ book, which I'm sure you have if this is for a class.

    If you plan on being successful in your Engineering studies, you need to put forth a lot more personal effort. This is not meant to be mean. I graduated with a BS in Mech Engr, and later studied Computer Science. This intro class is nothing compared to the work you will have to do later on. If this class is so hard, maybe Engineering isn't for you.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    That's OK...he/she will just outsource the work.

  9. #9
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179
    Quote Originally Posted by DaveH View Post
    I'm very amused that you found his reply that helpful. He just paraphrased what you wrote.
    See, that's what's so funny. Paraphrased? I totally copied and pasted his original post, basically just changing the tense AND HE THANKED ME FOR IT. Dude, this is not normally a rude community but seriously, if you're planning on going to school to build cars or design satellites, maybe you should be considering botany instead.

    Okay, I'm being a bit flippant, so in penance, here check out Pick Up Sticks, it's probably got more BASIC I/O processing examples in it than you need.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Quote Originally Posted by guesst View Post
    See, that's what's so funny. Paraphrased? I totally copied and pasted his original post, basically just changing the tense AND HE THANKED ME FOR IT. Dude, this is not normally a rude community but seriously, if you're planning on going to school to build cars or design satellites, maybe you should be considering botany instead.

    Okay, I'm being a bit flippant, so in penance, here check out Pick Up Sticks, it's probably got more BASIC I/O processing examples in it than you need.
    Your post was shorter, so I assumed paraphrased. I didn't actually compare them that closely.

  11. #11
    Registered User
    Join Date
    Mar 2008
    Posts
    4
    I also have the same assaignment, I'm not asking anyone to do it, but maybe someone could give me some pointer.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
    char choice;
    
    		
    	//Display a menu to and get choice;
    
    	cout << "\t\tFirst year physics calulator\n\";
    	cout << "1. Calculation of the period of oscillationof a pendulum\n";
    	cout << "2. Calculate the maximum projectile height\n";
    	cout << "3. Quit\n";
    	cout << "Enter your choice\n";
    	cin >> choice;
    
    	// Respond to the user's menue selection;
    	switch (choice);
    float Period, Lenght, Gravity;
    
    		case 1: cout << "Enter the lenght of the string\n";
                    cin >> Lenght;
    
    				Period = (2*3.14159) sqrt (Lenght/9.8);
    				cout << fixed << showpoint << setprecision(2);
                    cout << " The period is " << Period << "\n";
    		
    				float Velocity, angle, height, gravity;
    		case 2:	cout << "Enter the velocity\n";
    			cin >> Velocity;
    				cout << "Enter the angle"\n;
    			cin >> angle;
    				cout << "Enter gravity"\n;
    				cin >> gravity;
    			height = ((Velocity) ^ 2 * (0.17937 * angle) ^ 2)/(2 * gravity);
                    cout << fixed << showpoint << setprecision(2);
    		cout << " The height is " << height << "\n";
    		case 3: cout << "Exiting\n";
    
    	
    	
    	cout << fixed << showpoint << setprecision(2);
    	
    
    	return 0;
    
    }

  12. #12
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Some small style tips:

    Dont use capitals for the first letter of variable names, these are usually reserved for Class, structs and enum names.

    Dont use float - use double instead.

    And also, your switch statement looks a bit odd. You need to make it all one unit:

    Code:
    switch ( choice )
    {
    case 1:
       break;
    
    case 2:
       break;
    
    default:
       break;
    };
    Yours seems to be broken up, and not incased in the braces.
    Double Helix STL

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by shock View Post
    I also have the same assaignment, I'm not asking anyone to do it, but maybe someone could give me some pointer.
    Here: &this. [/bad programming humor]

    Seriously: switch deals with the next statement or block thereof, which for you is ";". Therefore nothing much is going to happen there -- I would recommend putting what you want to happen on the switch into curlicues and getting rid of that semicolon. You'll also need breaks after each case.

    You never use Gravity.

    ^ will surprise you. (Hint: Most definitely not a power.)

  14. #14
    Registered User
    Join Date
    Mar 2008
    Posts
    4
    thanks for the help guys but i seem to be getting more errors now.

    Code:
    1>------ Build started: Project: test, Configuration: Debug Win32 ------
    1>Compiling...
    1>assaignment1.cpp
    1>.\assaignment1.cpp(13) : error C2001: newline in constant
    1>.\assaignment1.cpp(25) : error C2046: illegal case
    1>.\assaignment1.cpp(26) : error C2065: 'Lenght' : undeclared identifier
    1>.\assaignment1.cpp(28) : error C2065: 'Period' : undeclared identifier
    1>.\assaignment1.cpp(28) : error C3861: 'sqrt': identifier not found
    1>.\assaignment1.cpp(29) : error C3861: 'setprecision': identifier not found
    1>.\assaignment1.cpp(33) : error C2043: illegal break
    1>.\assaignment1.cpp(34) : error C2046: illegal case
    1>.\assaignment1.cpp(37) : error C2017: illegal escape sequence
    1>.\assaignment1.cpp(37) : error C2146: syntax error : missing ';' before identifier 'n'
    1>.\assaignment1.cpp(37) : error C2065: 'n' : undeclared identifier
    1>.\assaignment1.cpp(39) : error C2017: illegal escape sequence
    1>.\assaignment1.cpp(41) : error C2296: '^' : illegal, left operand has type 'double'
    1>.\assaignment1.cpp(41) : error C2297: '^' : illegal, right operand has type 'double'
    1>.\assaignment1.cpp(42) : error C3861: 'setprecision': identifier not found
    1>.\assaignment1.cpp(44) : error C2043: illegal break
    1>.\assaignment1.cpp(45) : error C2046: illegal case
    1>.\assaignment1.cpp(49) : error C3861: 'setprecision': identifier not found
    1>Build log was saved at "file://c:\Documents and Settings\Jerzy\My Documents\Visual Studio 2005\Projects\test\test\Debug\BuildLog.htm"
    1>test - 18 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    I must be the biggest noob out there.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You need to #include <cmath> to get sqrt. You need #include<iostream> to get setprecision. You've managed to continue some of your string literals past the quotes in which they are supposed to be enclosed. You've been pointed to the other errors already, you just didn't fix them.

Popular pages Recent additions subscribe to a feed