Hey everyone. I just finished a cross product calculator program for the fun of it trying to test stuff outside of my rage of knowledge. I tried a couple new things like strings and cin.fail() so if someone would read over it and tell me what I did right/wrong that would be awesome.

//
//  Cross Product Calculator
//
//  Created by Matthew Porter on 03/26/12.
//
//      Update Log: 03/28/12
//                  03/30/12
//                  04/01/12
//                  04/02/12
//
//  Copyright (c) 2012. All rights reserved.
//

#include <iostream>
#include <cmath>
#include <string>
#include <cctype> //for isdigit() & isalpha()?
#include <locale> //for isdigit() & isalpha()?

// Can't remember why I put cctype and locale. Anyone know an argument for/against
// which I should use?

using namespace std;

void header();
void user_input1(double&,double&,double&,double&,double&,double&);
void user_input2(string&,string&,string&,string&,string&,string&);
double ihat(double,double,double,double);
double jhat(double,double,double,double);
double khat(double,double,double,double);
void output(double,double,double,double,double,double);
void outputmitvar(double,double,double,double,double,double,string,string,string,string,string,string);
bool restart();
bool varquestion();
double check_value(string);

int main (){


    double q,w,e,a,s,d;
    string i1,j1,k1,i2,j2,k2;

    header();

    do {

        user_input1(q,w,e,a,s,d);

        if (varquestion()==true){

            user_input2(i1,j1,k1,i2,j2,k2);

            outputmitvar(q, w, e, a, s, d, i1, j1, k1, i2, j2, k2);

        }
        else {
            output(q,w,e,a,s,d);
        }

    } while (restart() == true);

    return 0;

}

void header(){

    cout << "***************************************" << endl
         << "**                                   **" << endl
         << "**  Cross Product Calculator (2012)  **" << endl
         << "**                                   **" << endl
         << "**     Created by Matthew Porter     **" << endl
         << "**                                   **" << endl
         << "***************************************" << endl << endl;

    cout << "***************************************" << endl
         << "**                                   **" << endl
         << "** NOTE:  FIRST INPUT SECTION IS FOR **" << endl 
         << "** COEFFICIENTS ONLY! VARIABLES ARE  **" << endl
         << "** ENTERED IN INPUT SECTION TWO!     **" << endl
         << "**                                   **" << endl
         << "***************************************" << endl;


}

void user_input1(double& q, double& w, double& e, double& a, double& s, double& d)
{

        cout << "\vInput Section 1 (Coeficients):" << endl;

        cout << "\v\tPlease enter your first set of values:" << endl;

            do{
                cout << "\t\ti << ";
                cin >> q;
                if (cin.fail()){
                    cout << "** Error! A valid value was not entered!" << endl;
                    cout << "** Please re-enter an appropriate value for i." << endl;
                }
                else break;
            } while(cin.fail(),cin.clear(),cin.ignore(1000,'\n'));

            do{
                cout << "\t\tj << ";
                cin >> w;
                if (cin.fail()){
                    cout << "** Error! A valid value was not entered!" << endl;
                    cout << "** Please re-enter an appropriate value for j." << endl;
                }
                else break;
            } while(cin.fail(),cin.clear(),cin.ignore(1000,'\n'));

            do{
                cout << "\t\tk << ";
                cin >> e;
                if (cin.fail()){
                    cout << "** Error! A valid value was not entered!" << endl;
                    cout << "** Please re-enter an appropriate value for k." << endl;
                }
                else break;
            } while(cin.fail(),cin.clear(),cin.ignore(1000,'\n'));

        cout << "\v\tPlease enter your second set of values:" << endl;

            do{
                cout << "\t\ti << ";
                cin >> a;
                if (cin.fail()){
                    cout << "** Error! A valid value was not entered!" << endl;
                    cout << "** Please re-enter an appropriate value for i." << endl;
                }
                else break;
            } while(cin.fail(),cin.clear(),cin.ignore(1000,'\n'));

            do{
                cout << "\t\tj << ";
                cin >> s;
                if (cin.fail()){
                    cout << "** Error! A valid value was not entered!" << endl;
                    cout << "** Please re-enter an appropriate value for j." << endl;
                }
                else break;
            } while(cin.fail(),cin.clear(),cin.ignore(1000,'\n'));

            do{
                cout << "\t\tk << ";
                cin >> d;
                if (cin.fail()){
                    cout << "** Error! A valid value was not entered!" << endl;
                    cout << "** Please re-enter an appropriate value for k." << endl;
                }
                else break;
            } while(cin.fail(),cin.clear(),cin.ignore(1000,'\n'));

}

void user_input2(string& i1, string& j1, string& k1, string& i2, string& j2, string& k2)
{

    cout << "\vInput Section 2 (Variables):" << endl
        << "Warning: If numbers are input, the correct values will not be calculated!!!" << endl;

    cout << "\v\tPlease enter your first set of variables:" << endl;
    cout << "\t\ti << ";
        getline(cin,i1);
    cout << "\t\tj << ";
        getline(cin,j1);
    cout << "\t\tk << ";
        getline(cin,k1);

    cout << "\v\tPlease enter your second set of variables:" << endl;
    cout << "\t\ti << ";
        getline(cin,i2);
    cout << "\t\tj << ";
        getline(cin,j2);
    cout << "\t\tk << ";
        getline(cin,k2);

}

void output(double q, double w, double e, double a, double s, double d){

    cout << "\vResult: " << "(" << ihat(w,d,e,s) << ")i - (" << jhat(q,d,e,a) << ")j + (" << khat(q,s,w,a) << ")k" << endl;

}

void outputmitvar(double q, double w, double e, double a, double s, double d, string i1, string j1, string k1, string i2, string j2, string k2){

    cout << "\vResult: "<< "(" << j1 << w << "*" << k2 << d << "-" << k1 << e << "*" << j2 << s << ")i - "
        << "(" << i1 << q << "*" << k2 << d << "-" << k1 << e << "*" << i2 << a << ")j + "
        << "(" << i1 << q << "*" << j2 << s << "-" << j1 << w << "*" << i2 << a << ")k" << endl;

}

double ihat(double u, double i, double o,double p){

    double temp = (u*i)-(o*p);
    return(temp);

}

double jhat(double h, double j, double k, double l){

    double temp = (h*j)-(k*l);
    return(temp);

}

double khat(double v, double b, double n, double m){

    double temp = (v*b)-(n*m);
    return(temp);

}

bool restart(){

    char answer;

    cout << "\vWould you like to restart the program? (Y/N)" << endl;
    cout << "\t>> ";
    cin >> answer;

    answer = toupper(answer);

    if (answer=='Y'){
        return true;
    }
    else {
        return false;
    } 
}

bool varquestion(){

    char answer;

    do {

        cout << "\v\tAre there variables to be entered? (Y/N)" << endl;
        cout << "\t\t>> ";
        cin >> answer;
        answer = toupper(answer);

        if (isdigit(answer)||ispunct(answer)){
            cout << "\v\tA valid character was not entered. Try again.";
        }
        if ((answer==!'Y')||(answer==!'N')){
            cout << "\v\tA valid character was not entered. Try again.";
        }

    } while(isdigit(answer)||!isalpha(answer));

    if (answer=='Y'){
        return true;
    }
    else {
        return false;
    }

}

Recommended Answers

All 2 Replies

if someone would read over it and tell me what I did right/wrong

is there a specific problem in your program? maybe an error message or it's not outputting what it suppose to

no. basically what i meant is to see if im using certain aspects of the code right since a lot of this i taught to myself and havent learned in class yet.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.