CSCI 229 C++ for Engineers Summer 2007 Program 2 Due date: Tuesday, June 26, 2006; no later than 10:00 p.m.

Problem Description Students at a local elementary school that you have been tutoring are having some difficulty with some mathematical concepts they are learning in their math class. You have decided to write a simple computer program that will help with a few of these concepts. Program Directions Write a C++ program that uses the information found in the formula section to help students to practice their math skills. The program must do the following: • Ask the user to input the two numbers. • Ask the user to select the mathematical operator. o Note if the user inputs a real value to use with the modulus operator, alert the user that the real value inputted cannot be used with the modulus operator. • Ask the user for the answer. • Calculate the correct answer using the input of the numbers and the math operator. • Display to the screen the correct answer as calculated by the formula and a statement telling the user if he/she was correct. • Allow the user to continue with the program until he/she no longer wants to continue to use the program. The program should take both upper and lower case input. Formulas + * / % MATHEMATICAL OPERATORS Addition Subtraction Multiplication Division Modulus

Program Output The program should display the following: • A question asking the user if he/she wants to begin using the program and continue using the program • A menu displaying the operator selection choices • The correct answers as calculated by the formulas • A statement if the answer is correct or incorrect • An alert that the modulus cannot be used with real values. Program Calculations Failure to include the formulas in the program will result in a deduction of points on the assignment. Program Header Information • Name • Program number • Collaboration statement • The answers to the following question: o What is the purpose of the program? o What is the input? (Is there any?) o What is the output? Sample Run Below is a sample run. DO NOT include additional input or output streams in your program.

/* Adonis Brown Program 2 I worked on the assignment by myself and received no additional assistance. 1. The purpose of this program is to accept numbers from the user, then succesfully perform the operation that they have selected upon the numbers. It also allows them to continue as long as they are willing. 2. The inputs are one input to start and continue the program, 2 numbers, and one input for the operator of the user's liking. 3. The outputs are many. The user is asked if they want to begin. The user is then asked for two numbers and their preferred operator. If the user inputs numbers, and the program performs operations upon them, the user is asked to input their answer to the operation. The program then informs the user if they are correct or incorrect. If during the operator selection process the modulous is selected, and either or both of the numbers are real, the computer informs the user of this and requests that the user selects a different number (or numbers) for usage with the modulous. After this, the program continues as usual. */ #include <iostream.h> #include <stdlib.h> #include <string> int main() { double num1, num2, u_ans, p_ans = 0; char oper, redo; cout<<"Enter 'Y' if you would like to begin."<<endl; cin>>redo; while ((redo == 'Y')||(redo == 'y')) { cout<<"Please enter 2 numbers: "; cin>>num1>>num2; cout<<endl<<"Please select your mathematical operator."<<endl; cout<<endl<<"+ = Addition"<<endl<<"- = Subtraction"<<endl<< "* = Multiplication"<<endl<<"/ = Division"<<endl <<"% = Modulous Division (Provides the remainder)"<<endl<<endl; cin>>oper; cout<<endl; while (oper == '%') { if ((oper == '%')&&(num1 != int(num1))&&(num2 != int(num2))) { cout<<num1<<" and "<<num2<<" are real numbers and cannot be used with modulous"<<endl; cout<<"Please re-enter non-real values: "; cin>>num1>>num2; }

else if (oper == '%') { if ((oper == '%')&&(num1 != int(num1))) { cout<<num1<<" is a real number and cannot be used with modulous."<<endl <<endl; cout<<"Please enter a non-real value for your first number: "; cin>>num1; break; } if ((oper == '%')&&(num2 != int(num2))) { cout<<num2<<" is a real number and cannot be used with modulous."<<endl <<endl; cout<<"Please enter a non-real value for your second number: "; cin>>num2; break; } } else break; } while ((oper != '+')&&(oper != '-')&&(oper != '/')&&(oper != '*')&&(oper != '%')) { cout<<"Invalid Entry!"<<endl<<"Enter a new operator: "; cin>>oper; } cout<<endl; switch(oper) { case '+': p_ans = num1 + num2; break; case '-': p_ans = num1 - num2; break; case '*': p_ans = num1 * num2; break; case '/': p_ans = num1 / num2; break; case '%': p_ans = int(num1) % int(num2); break; default: cout<<"Invalid Entry!"<<endl; break; } cout<<"Please enter your answer."<<endl<<endl<<"(Your answer)"<<endl<< num1<<" "<<oper<<" "<<num2<<" = "; cin>>u_ans;

cout<<endl; if (u_ans == p_ans) cout<<"(Program answer)"<<endl<<num1<<" "<<oper<<" "<<num2<<" = "<< p_ans<<endl<<"You got it right!"<<endl<<endl; else cout<<"(Program answer)"<<endl<<num1<<" "<<oper<<" "<<num2<<" = "<< p_ans<<endl<<"Try again."<<endl<<endl; cout<<"Enter 'Y' to continue. "; cin>>redo; cout<<endl; } system("PAUSE"); return 0; }

Master your semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master your semester with Scribd & The New York Times

Cancel anytime.