Hey,
Thanks for posting that code, it's very helpful in understanding cin.good().
I am trying to utilize the features of your code that help with my code, and I encountered three problems.
First, Ctrl + D does not terminate input. Is that the cost of using cin.clear or cin.ignore? I want my program to be able to do that, and I am curious if there is a work-around.
Second, the way my output is formatted is that whatever N the user inputs, each vector outputs N amount of 0.0's, then it outputs the numbers that I want after those 0.0's and calculates the dot product as intended. I am not sure how to fix this. =/
Third, if I try to make my vectorSize 1, aka fill in two values to calculate, it accepts four, as if I had put in vectorSize of 2. Not sure about that one.
I'm going to repost the code implementing most of your code, and show an output that I get:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
/************************************************************/
// System includes
#include <iostream>
#include <string>
#include <cstdlib>
#include <vector>
#include <stdexcept>
#include <limits>
/************************************************************/
// Using declarations
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::numeric_limits;
using std::ios;
using std::streamsize;
/************************************************************/
// Function prototypes/global vars/typedefs
bool
validInput (double value);
double
getValue ();
void
setOutputDigits (int digits);
double
calculateDotProduct (const vector<double>& v1, const vector<double>& v2);
void
printVectors (const vector<double> p, string s);
/************************************************************/
int
main ()
{
int vectorSize= 0;
cout << "Enter size of vector => ";
cin >> vectorSize;
vector<double> v1 (vectorSize, 0);
vector<double> v2 (vectorSize, 0);
cout << "Maximum # of values to input is " << vectorSize * 2
<< ". Any additional input is disregarded." << endl;
cout << "Enter " << vectorSize*2
<< " floating point values (Ctrl + D to terminate): ";
for (int i = 0; i < vectorSize; ++i)
{
v1.push_back (getValue ());
v2.push_back (getValue ());
}
setOutputDigits (1);
printVectors (v1, "Vector 1 => ");
cout << endl;
printVectors (v2, "Vector 2 => ");
cout << endl;
double dotProduct = calculateDotProduct(v1, v2);
cout << "The dot product is " << dotProduct << endl;
return EXIT_SUCCESS;
}
void
printVectors (const vector<double> v, string s)
{
cout << s;
for (unsigned int i = 0; i < v.size (); i++)
{
cout << "[" << i << "] " << v[i] << " ";
}
cout << endl;
}
double
calculateDotProduct (const vector<double>& v1, const vector<double>& v2)
{
double product = 0;
for (unsigned int i = 0; i <= v1.size()-1; i++)
{
product += (v1[i])* (v2[i]);
}
return product;
}
// set pretty output format and number of digits after decimal
void
setOutputDigits (int digits)
{
cout.precision (digits);
cout.setf (ios::fixed | ios::dec | ios::showpoint);
}
bool validInput (double value)
{
return value <= 100.0 && value >= 0.0;
}
double getValue ()
{
const char* prompt = "> ";
bool inputIsValid = false;
double value;
while (!inputIsValid)
{
cout << prompt;
cin >> value;
inputIsValid = cin.good () && validInput (value);
if (!inputIsValid)
{
cout << "Invalid input! Try again!\n";
cin.clear ();
cin.ignore (numeric_limits<streamsize>::max (), '\n');
}
}
return value;
}
/************************************************************/
/************************************************************/
|
Error with vectorSize of 2:
1 2 3 4 5 6 7 8 9 10 11 12
|
Enter size of vector
1
Maximum # of values to input is 2. Any additional input is disregarded.
Enter 2 floating point values (Ctrl + D to terminate): > 1
> 2
> 2
> 3
Vector 1 => [0] 0.0 [1] 1.0 [2] 2.0
Vector 2 => [0] 0.0 [1] 2.0 [2] 3.0
The dot product is 8.0
|
That shows both the fact that I needed 4 values instead of 2, and that v[0] was 0.0, which I don't intend.
Hopefully somebody can point me in the right direction. I feel like it's an error with my printVectors function, but I am confused as to how to fix it.
*EDIT*
Just realized that the 1 more was an off by one error with my for loop. Went to i <= vectorSize instead of i < vectorSize. Oops!
The 0.0 is still an issue, as well as Ctrl + D.