I've been messing around with vectors and bool to test for unwanted or unknown
input. It's causing my program to crash. Any solution on how to execute this better or could it be a compile glitch?
// While loop that prints smallest and largest numbers so far with units included
/* Bugs---
When testing for illegal units (units that aren't in the vector or don't exist in this program,
the program crashes. Ex. I enter 12b. Instant crash.
*/
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
usingnamespace std;
inlinevoid keep_window_open(){char ch; cin>>ch;}
int main()
{
/*
1m == 100cm
1in == 2.54cm
1ft == 12in
*/
double test_input1;
double test_input2;
double input;
double smallest_input;
double largest_input;
double large_converter;
double small_converter;
string test_unit1 = "???";
string test_unit2 = "???";
string unit;
string smallest_unit;
string largest_unit;
bool legit1 = true;
bool legit2 = true;
vector<string>legal_units(4); // Intending to check all strings in the vector
legal_units [0] = "cm";
legal_units [1] = "m";
legal_units [2] = "in";
legal_units [3] = "ft";
// New
constdouble mm_per_cm = 10;
constdouble mm_per_m = 1000;
constdouble mm_per_in = 25.40;
constdouble mm_per_ft = 304.80;
cout << "Enter a number and unit \n";
cin >> test_input1 >> test_unit1;
for (int a = 0; test_unit1 != legal_units[a]; ++a) // Variable "a" is a counter variable. Each test_unit1 proves to not be equal the value of "a"
if (a == 4){ // Stored in the vector legal_units, a is incremented. If variable "a" finnaly reaches 4,
legit1 = false; // A value outside of a vector's placeholder, Legit1 a bool becomes false. The same applies with the second for statement
} // With different variables.
cin >> test_input2 >> test_unit2;
cout << "\n";
for (int b = 0; test_unit2 != legal_units[b]; ++b)
if (b == 4){
legit2 = false;
}
if (legit1 = true, legit2 = true){
Blah Blah Blah code
}
else
cout << "You have entered illegal units. Reboot" << endl;
return 0;
}
Compile glitches are highly rare, and this doesn't appear to be one.
Your loop on line 53 will attempt to access an element of legal_units beyond its size (specifically, it will attempt to access an element at index 4, which does not exist), assuming that the unit is invalid.
Why are you using std::vectors and not std::arrays? O.O
If your vector absolutely never ever changes size, then you can also use std::array (NOT to be confused with C-style arrays with [] notation, though you can use [] with them as well).
1 2 3 4 5 6 7 8
#include <array> //C++11 feature!
int main() {
std::array<type,length> name;
name[i] = value; //Works just fine.
name.size(); //Get the length value you initialized it with.
name.empty(); //Same as the function in vectors, check if the array is empty.
name.fill(value); //Set all elements of the array as value.
}
EDIT: Just noticed that you said course book rather than textbook. If you're programming in C++ for a course, then you may want to check with your professor before using these.
Well it's actually sort of a book that can be used for courses. I don't know if they do, but it's the Barjne Stroustrup programming principals book. There's drills and exercise that really do me well, but I have to find a way to do with with a vector or if statement. This is drill 8 and it carries to like 4 more drills built around those.
The condition in the loop should be separated by logical operators, either or( || ) or and ( && ) not comma
which in your case, i think should be &&
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When testing for illegal units, the program crashes
you should find a way to check if the input entered is not in the vector.
( hint: do this inside the for loop )
And your loops can be greatly simplified:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
bool hasMatch = false;
cout << "Enter number and unit: ";
cin >> test_input1 >> test_unit1;
for ( int i = 0; i < 4; ++i )
{
// search for a match in the vector, if it finds a match,
// hasMatch variable, is set to true
if( test_unit1 == legal_units[ i ] )
hasMatch = true;
}
// now, if hasMatch is true, continue the program, if false, exit
if( hasMatch ) { // this is the same as if( hasMatch == true ) { ...
// do things ...
} else {
return 0; // exit
}