When i build and run the code, i get the next error: "terminate called after throwing an instance of 'std::out_of_range'
what<>: vector::_M_range_check: __n <which is 0> >= this->size<> <which is 0>
I don't know how to fix this problem, I am new to coding!
What I'm trying to do is using a collection (list) of integers and allowing the user to select options from a menu to perform operations on the list.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector <int>numbers{};
int nr_to_add{};
char selection{};
double average{};
int min = numbers.at(0);
int max = numbers.at(0);
do{
cout << "P - Print numbers!" << endl;
cout << "A - Add numbers!" << endl;
cout << "M - Display mean of the numbers!" << endl;
cout << "S - Display the smallest number!" << endl;
cout << "L - Display the largest number!" << endl;
cout << "Q - Quit" << endl;
cout << "Enter your selection: ";
cin >> selection;
if(selection == 'p' || selection == 'P'){
for(int i = 0; i < numbers.size(); i++)
cout << "The numbers are: " << numbers[i];
if(numbers.empty())
cout << "[] - the list is empty! " << "\nYou should add some numbers!" << endl;
cout << "Add some numbers: ";
cin >> nr_to_add;
numbers.push_back(nr_to_add);
}else if(selection == 'a' || selection == 'A'){
cout << "Add the number you want: ";
cin >> nr_to_add;
cout << "\n" << nr_to_add << " has been added successfully!" << endl;
numbers.push_back(nr_to_add);
}else if(selection == 'm' || selection == 'M'){
if(numbers.empty())
cout << "Unable to calculate de average - no data introduced!" << endl;
if(numbers.size() != 0)
average = accumulate(numbers.begin(),numbers.end(), 0.0)/numbers.size();
cout << "The average is: " << average << endl;
}else if(selection == 's' || selection == 'S'){
if(numbers.empty())
cout << "Unable to determine the smallest number - list is empty!";
if(numbers.size() != 0)
for(int i = 0,n = 0; i <= n; i++){
if(numbers.at(i) < min)
min = numbers[i];
cout << "The smallest number is: " << min << endl;
}
}else if(selection == 'l' || selection == 'L'){
if(numbers.empty())
cout << "Unable to determine the largest number - list is empty!";
if(numbers.size() != 0)
for(int i = 0,n = 0; i <= n; i++){
if(numbers.at(i) > max)
max = numbers[i];
cout << "The largest number is: " << max << endl;
}
}else if(selection == 'q' || selection == 'Q'){
cout << "Goodbye..." << endl;
} else {
cout << "Unknown selection, please try again" << endl;
}
} while(selection != 'q' && selection != 'Q');
return 0;
}
The problem is that you are calling at(0) on an empty vector. That is what the out_of_range error is telling you. You need to move the calls into your if statements where the values are actually being used, after you perform your empty() checks.
Also, your min and max loops are coded wrong. And you don't need to use loops at all, you can use the standard std::min_element() and std::max_element() algorithms instead (you are already using the std::accumulate() algorithm).
Try something more like this:
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <limits>
using namespace std;
int main()
{
vector<int> numbers{};
int nr_to_add;
char selection;
double average_nr;
int min_nr, max_nr;
do {
cout << "P - Print numbers!" << endl;
cout << "A - Add numbers!" << endl;
cout << "M - Display mean of the numbers!" << endl;
cout << "S - Display the smallest number!" << endl;
cout << "L - Display the largest number!" << endl;
cout << "Q - Quit" << endl;
cout << "Enter your selection: ";
if (!(cin >> selection)) {
selection = '\0';
cin.clear();
}
cin.ignore(numeric_limits<streamsize>::max(), '\n');
if (selection == 'p' || selection == 'P') {
if (numbers.empty()) {
cout << "The list is empty! " << "\nYou should add some numbers!" << endl;
} else {
cout << "The numbers are:"
for(size_t i = 0; i < numbers.size(); ++i) {
cout << " " << numbers[i];
}
cout << endl;
}
}
else if (selection == 'a' || selection == 'A') {
cout << "Add the number you want: ";
if (cin >> nr_to_add) {
numbers.push_back(nr_to_add);
cout << "\n" << nr_to_add << " has been added successfully!" << endl;
} else {
cout << "Bad input, please try again" << endl;
cin.clear();
}
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
else if (selection == 'm' || selection == 'M'){
if (numbers.empty()) {
cout << "Unable to calculate the average - list is empty!" << endl;
} else {
average_nr = accumulate(numbers.begin(), numbers.end(), 0.0) / numbers.size();
cout << "The average is: " << average_nr << endl;
}
}
else if (selection == 's' || selection == 'S') {
if (numbers.empty()) {
cout << "Unable to determine the smallest number - list is empty!";
} else {
/*
min_nr = numbers[0];
for(size_t i = 1; i < numbers.size(); ++i){
if (numbers[i] < min_nr) {
min_nr = numbers[i];
}
}
*/
min_nr = *min_element(numbers.begin(), numbers.end());
cout << "The smallest number is: " << min_nr << endl;
}
}
else if (selection == 'l' || selection == 'L') {
if (numbers.empty()) {
cout << "Unable to determine the largest number - list is empty!";
} else {
/*
max_nr = numbers[0];
for(size_t i = 1; i < numbers.size(); ++i){
if (numbers[i] > max_nr) {
max_nr = numbers[i];
}
}
*/
max_nr = *max_element(numbers.begin(), numbers.end());
cout << "The largest number is: " << max_nr << endl;
}
}
else if (selection == 'q' || selection == 'Q') {
cout << "Goodbye..." << endl;
}
else {
cout << "Unknown selection, please try again" << endl;
}
}
while (selection != 'q' && selection != 'Q');
return 0;
}
Try this
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector <int>numbers{};
int nr_to_add{};
char selection{};
double average{};
int min = 0;
int max = 0;
do {
cout << "P - Print numbers!" << endl;
cout << "A - Add numbers!" << endl;
cout << "M - Display mean of the numbers!" << endl;
cout << "S - Display the smallest number!" << endl;
cout << "L - Display the largest number!" << endl;
cout << "Q - Quit" << endl;
cout << "Enter your selection: ";
cin >> selection;
if (selection == 'p' || selection == 'P') {
for (int i = 0; i < numbers.size(); i++)
cout << "The numbers are: " << numbers[i];
if (numbers.empty()) {
cout << "[] - the list is empty! " << "\nYou should add some numbers!" << endl;
cout << "Add some numbers: ";
cin >> nr_to_add;
numbers.push_back(nr_to_add);
min = nr_to_add;
max = nr_to_add;
}
}
else if (selection == 'a' || selection == 'A') {
cout << "Add the number you want: ";
cin >> nr_to_add;
cout << "\n" << nr_to_add << " has been added successfully!" << endl;
if (numbers.empty) {
min = nr_to_add;
max = nr_to_add;
}
else {
if (min > nr_to_add) min = nr_to_add;
if (max < nr_to_add) max = nr_to_add;
}
numbers.push_back(nr_to_add);
}
else if (selection == 'm' || selection == 'M') {
if (numbers.empty())
cout << "Unable to calculate de average - no data introduced!" << endl;
if (numbers.size() != 0)
average = accumulate(numbers.begin(), numbers.end(), 0.0) / numbers.size();
cout << "The average is: " << average << endl;
}
else if (selection == 's' || selection == 'S') {
if (numbers.empty())
cout << "Unable to determine the smallest number - list is empty!";
else
cout << "The smallest number is: " << min << endl;
}
else if (selection == 'l' || selection == 'L') {
if (numbers.empty())
cout << "Unable to determine the largest number - list is empty!";
else
cout << "The largest number is: " << max << endl;
}
else if (selection == 'q' || selection == 'Q') {
cout << "Goodbye..." << endl;
}
else {
cout << "Unknown selection, please try again" << endl;
}
} while (selection != 'q' && selection != 'Q');
return 0;
Related
The goal is to create a five-in-one program for various math stuff. The individual blocks work. After selecting and using a block, the program should then send a prompt asking the user whether to go back to the menu or terminate the program, this also works.
However, the function which checks whether you've inputted "Y", "y", "N", or "n" does not work. Instead, it automatically returns you to the main menu even after inputting "N" or "n", which should terminate the program.
#include <iostream>
#include<cctype>
#include<cstdlib>
#include <ctime>
#include <bits/stdc++.h>
using namespace std;
bool Primecheck(int x){
// checks if prime is positive or nah
if (x <= 1)
return false;
// Check from 2 to n-1
for (int i = 2; i < x; i++)
if (x % i == 0)
return false;
return true;
}
void Repeatcheck(char repeat){
//checks whether you inputted anything other than Y, y, N, or n
while (repeat != 'Y' && repeat != 'y' && repeat != 'N' && repeat != 'n' ){
system("cls");
std::cout << "that's not an option, dumbass." << std::endl;
std::cout << "\n Return to main menu? Y/N" << std::endl;
cin >> repeat;
}
}
int main()
{
//Option selection
int OptSel;
//repeat selection
char repeat;
repeat = 'Y';
//Optsel 1
int prime, i, primed2=0, flag=0;
//Optsel 2
int j,rows;
//Optsel 3
int t1 = 0, t2 = 1, nextTerm = 0;
//Optsel 4
int factorialinput;
long factorial = 1.0;
do{
system("cls");
repeat = 'Y';
startoptsel:
std::cout << "\n----MATHS PROGRAM----" << std::endl;
std::cout << "\n wats poppin?" << std::endl;
std::cout << "1.) prime number checker" << std::endl;
std::cout << "2.) right triangle drawer" << std::endl;
std::cout << "3.) fibonacci" << std::endl;
std::cout << "4.) factorial" << std::endl;
std::cout << "5.) exit" << std::endl;
std::cin >> OptSel;
//check whether option seleccted is available.
if (!std::cin || OptSel <= 0 || OptSel > 5) {
system("cls");
std::cin.clear();
std::cin.ignore(1000, '\n');
std::cout << "\nThat's not an option, dumbass." << std::endl;
std::cout << "\nInput numbers from 1-5." << std::endl;
goto startoptsel;
}
if (OptSel == 1) {
system("cls");
primechecker:
std::cout << "\n----Prime number checker----" << std::endl;
std::cout << "Enter number: " << std::endl;
std::cin >> prime;
// auto filters everything as not prime below 1
primed2=prime/2;
for(i = 2; i <= primed2; i++){
if(prime % i == 0)
{
cout << "\n " << prime << " is not a prime."<<endl;
flag=1;
break;
}
}
if (flag==0)
cout << "\n " << prime << " is a prime."<<endl;
if (!cin) {
system("cls");
std::cin.clear();
std::cin.ignore(1000, '\n');
std::cout << "please input a number" << std::endl;
goto primechecker;
}
std::cout << "\n Return to main menu? Y/N" << std::endl;
cin>>repeat;
Repeatcheck(repeat);
}
if (OptSel == 2) {
system("cls");
triangledrawer:
cout << "\n----RIGHT TRIANGLE DRAWER----" << std::endl;
cout << "\nInput number of rows: ";
cin >> rows;
for(i=1;i<=rows;i++){
for(j=1;j<=i;j++)
cout<< ' ' << j;
cout<<endl;
}
if (!cin) {
system("cls");
std::cin.clear();
std::cin.ignore(1000, '\n');
std::cout << "please input a number" << std::endl;
goto triangledrawer;
}
std::cout << "\n Return to main menu? Y/N" << std::endl;
cin>>repeat;
Repeatcheck(repeat);
}
if (OptSel == 3){
system("cls");
fibonacciprinter:
cout << "\n----Fibonacci printer----";
cout << "\nEnter the number of terms: ";
cin >> prime;
cout << "Fibonacci Series: ";
for (int i = 1; i <= prime; ++i) {
// Prints the first two terms.
if(i == 1) {
cout << t1 << ", ";
continue;
}
if(i == 2) {
cout << t2 << ", ";
continue;
}
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
cout << nextTerm << ", ";
}
if (!cin) {
system("cls");
std::cin.clear();
std::cin.ignore(1000, '\n');
std::cout << "please input a number" << std::endl;
goto fibonacciprinter;
}
std::cout << "\n Return to main menu? Y/N" << std::endl;
cin>>repeat;
Repeatcheck(repeat);
}
if (OptSel == 4){
system("cls");
cout << "Enter a positive integer: ";
cin >> factorialinput;
cout << "Factorial of ";
if (factorialinput < 0)
cout << "Error! Factorial of a negative number doesn't exist.";
else {
for(int i = 1; i <= factorialinput; ++i) {
cout << i << " ";
factorial *= i;
}
cout << "= " << factorial;
}
std::cout << "\n Return to main menu? Y/N" << std::endl;
cin>>repeat;
Repeatcheck(repeat);
repeat = repeat;
}
if (repeat == 'n' || repeat == 'N'){
system("cls");
std::cout << "\nJob done? JOB DONE! Aight wrap it up lesgo." << std::endl;
return 0;
}
}while (OptSel != 5);
system("cls");
std::cout << "\nJob done? JOB DONE! Aight wrap it up lesgo." << std::endl;
return 0;
}
Rewriting the program to instead run when repeat = Y or y and removing the specific instance of terminating the program when N or n is entered instead results in the program terminating automatically no matter what is inputted when the Repeatcheck function is called.
#include <iostream>
#include<cctype>
#include<cstdlib>
#include <ctime>
#include <bits/stdc++.h>
using namespace std;
bool Primecheck(int x){
...
}
void Repeatcheck(char repeat){
while (repeat != 'Y' && repeat != 'y' && repeat != 'N' && repeat != 'n' ){
system("cls");
std::cout << "that's not an option, dumbass." << std::endl;
std::cout << "\n Return to main menu? Y/N" << std::endl;
cin >> repeat;
}
}
int main()
{
...
do{
...
}while (OptSel != 5 && repeat == 'Y' || repeat == 'y');
system("cls");
std::cout << "\nJob done? JOB DONE! Aight wrap it up lesgo." << std::endl;
return 0;
}
I'm doing a school project that creates a blackjack game, but unfortunately, I'm stumped at a few errors I must have made earlier on. The errors appear on lines 165 and 173. It states that I'm missing several elements that I cannot piece together. Could someone help inform me of what's wrong with this program that won't compile?
#include <iostream>
#include <string>
#include <ctime>
#include <limits>
using namespace std;
int getCard()
{
return rand() % 10 + 1;
}
char readChar()
{
char c;
cin >> c;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return c;
}
int main()
{
//set srand to seed system clock
//to generate random number
srand((int)time(0));
char yesno = 'y';
do
{
int dealer = getCard();
int first = getCard(), second = getCard();
int total = first + second;
if (yesno == 'y' || yesno == 'Y')
{
cout << "The dealer starts with a " << dealer << endl;
cout << "Your first cards: " << first << ", " << second << endl;
cout << "Total: " << total << endl;
do
{
cout << "hit? (y/n): ";
yesno = readChar();
if (yesno == 'y' || yesno == 'Y')
{
int newCard = getCard();
total += newCard;
cout << "Card: " << newCard << endl;
cout << "Total: " << total << endl;
if (total > 21)
{
cout << "Bust" << endl;
yesno = 'n';
break;
}
else if(total == 21)
{
cout << "Blackjack" << endl;
}
else if (yesno == 'n' || yesno == 'N')
{
//Dealer must take more cards if less than 17
while (dealer < 17)
{
cout << "Dealer has a " << dealer << "..." << endl;
char c;
do {
cout << "(c to continue) ";
c = readChar();
} while (c != 'c' && c != 'C');
int newDealerCard = getCard();
cout << "Dealer gets a " << newDealerCard << endl;
dealer += newDealerCard;
cout << "Total: " << dealer << endl;
}
//determine winner
if (dealer == 21 && dealer != total)
cout << "Lose" << endl;
else if (dealer == total)
cout << "Push" << endl;
else if (dealer > 21 && total > dealer && total < 21)
cout << "Win" << endl;
else if (dealer > total && dealer <= 21)
cout << "Lose" << endl;
break;
}
} while (yesno != 'n' && yesno != 'N');
}
cout << "Would you like to play again? (y/n) : ";
yesno = readChar();
} while (yesno != 'n' && yesno != 'N');
return 0;
}
Since I was a bit lost at the end, I wasn't sure where to add my missing elements.
There are at least two do {...} while (...) loops in there that do not have while clauses.
I've been working on my introduction to C++ project which is to make a game of NIM. I've written program and I thought it should run smoothly but I've countered the following errors.
I know too much code but I couldn't figure out what is the issue that is stopping me from running the program.
I'm new to the programming word so be understanding if the issue is very simple.
thank you in advance.
Error Message
#include <iostream>
#include <iomanip>
using namespace std;
int stonesNum;
char goFirst;
int turn = 0; // 0 = player, 1 = computer
int stonesRemove;
int main() {
do
{
cout << "Enter number of starting stones:" << endl;
cin >> stonesNum;
cout << "Would you like to go first? (y/n)" << endl;
cin >> goFirst;
if (goFirst == 'y' || goFirst == 'Y')
{
turn = 0;
}
else
{
turn = 1;
}
while (stonesNum > 0)
{
if (turn == 0)
{
cout << "How many would you like to remove: 1 or 2?" << endl;
cin >> stonesRemove;
stonesNum = stonesNum - 1;
cout << "The number of stones left is " << stonesNum << endl;
}
else
{
if (stonesNum % 3 == 0)
{
stonesNum = stonesNum - 2;
cout << "The computer removes 2 stones." << endl;
cout << "The number of stones left is " << stonesNum << endl;
}
else
{
stonesNum = stonesNum - 1;
cout << "The computer removes 1 stones." << endl;
cout << "The number of stones left is " << stonesNum << endl;
}
}
if (stonesNum == 0)
{
if (turn == 0)
{
cout << "The computer wins!" << endl;
}
else
{
cout << "you won!" << endl;
}
}
}
}
return 0;
}
You are missing an ending while condition after your do loop. The syntax is :
do{
//do stuff here
}while(condition);
In your case the condition would be (stonesNum!=0)
OR
Do
if (stonesNum == 0)
{
if (turn == 0)
{
cout << "The computer wins!" << endl;
return;
}
else
{
cout << "you won!" << endl;
return;
}
}
and put condition (true)
You actually don't require the do loop at all, are you trying to loop the game endlessly? If so then you should put an appropriate prompt at the beginning and put condition (true) in the do loop.
The complete code:
do
{
cout << "Enter number of starting stones:" << endl;
cin >> stonesNum;
cout << "Would you like to go first? (y/n)" << endl;
cin >> goFirst;
if (goFirst == 'y' || goFirst == 'Y')
{
turn = 0;
}
else
{
turn = 1;
}
while (stonesNum > 0)
{
if (turn == 0)
{
cout << "How many would you like to remove: 1 or 2?" << endl;
cin >> stonesRemove;
stonesNum = stonesNum - 1;
cout << "The number of stones left is " << stonesNum << endl;
}
else
{
if (stonesNum % 3 == 0)
{
stonesNum = stonesNum - 2;
cout << "The computer removes 2 stones." << endl;
cout << "The number of stones left is " << stonesNum << endl;
}
else
{
stonesNum = stonesNum - 1;
cout << "The computer removes 1 stones." << endl;
cout << "The number of stones left is " << stonesNum << endl;
}
}
}
if (turn == 0)
{
cout << "The computer wins!" << endl;
}
else
{
cout << "you won!" << endl;
}
}while(true);
I'm writing a program that tells a user when they enter a negative or positive and even or odd number
then the program ask a question, " Would you like to enter another number? y(es) or n(o)
I need to account for the user entering in something else besides 'y' and 'n'
and I need to account for if the user does not enter an integer. Last if the user selects yes, the program will need to go through the loop process of determining if they enter an integer and if its (positive or negative and odd or even)
int value;
char choice;
cout << "Please enter a number" << endl;
cin >> value;
while (!(cin >> value)) {
cin.clear();
cin.ignore(999, '\n');
cout << "Invalid data type! Please enter 'value' again" << endl;
}
if (value > 0 && value % 2 == 0) {
cout << value << " is even" << endl;
cout << value << " is positive" << endl;
}
else if (value < 0 && value % 2 != 0) {
cout << value << " is odd" << endl;
cout << value << " is negative" << endl;
}
else if (value > 0 && value % 2 != 0) {
cout << value << " is odd" << endl;
cout << value << " is postive" << endl;
}
else if (value < 0 && value % 2 == 0) {
cout << value << " is even" << endl;
cout << value << " is negative" << endl;
}
cout << "Would you like to try another number? Say y(es) or n(o)" << endl;
cin >> choice;
while (choice != 'y' &&choice != 'n') {
cin.clear();
cin.ignore(999, '\n');
cout << "Invalid response! Please enter 'choice' again" << endl;
}
do {
if (choice == 'y') {
cout << "Please enter a number" << endl;
cin >> value;
if (!(cin >> value)) {
cin.clear();
cin.ignore(999, '\n');
cout << "Invalid data type! Please enter 'value' again" << endl;
if (value > 0 && value % 2 == 0) {
cout << value << " is even" << endl;
cout << value << " is positive" << endl;
}
else if (value < 0 && value % 2 != 0) {
cout << value << " is odd" << endl;
cout << value << " is negative" << endl;
}
else if (value > 0 && value % 2 != 0) {
cout << value << " is odd" << endl;
cout << value << " is postive" << endl;
}
else if (value < 0 && value % 2 == 0) {
cout << value << " is even" << endl;
cout << value << " is negative" << endl;
}
cout << "Would you like to try another number? Say y(es) or n(o)" << endl;
cin >> choice;
}
}
} while (choice == 'n');
cout << "Thank you for using my program. Goodbye!" << endl;
return 0;
}
It would take nested do-while loops to check all your conditions.
Here I am using cin.fail(). cin.fail() detects whether the value entered fits the value defined in the variable.
int value;
char choice;
do{
cout << "Please enter a number" << endl;
cin >> value;
if(cin.fail()) // check if input is int
{
cout<<"Not an int";
choice = 'y';
}
else
{
if (value > 0 && value % 2 == 0)
{
cout << value << " is even" << endl;
cout << value << " is positive" << endl;
}
else if (value < 0 && value % 2 != 0)
{
cout << value << " is odd" << endl;
cout << value << " is negative" << endl;
}
else if (value > 0 && value % 2 != 0)
{
cout << value << " is odd" << endl;
cout << value << " is postive" << endl;
}
else if (value < 0 && value % 2 == 0)
{
cout << value << " is even" << endl;
cout << value << " is negative" << endl;
}
do{
cout << "Would you like to try another number? Say y(es) or n(o)" << endl;
cin >> choice;
}while(choice != 'y' || choice != 'n');
}
}while (choice == 'n');
Also you should read this: Checking input value is an integer
When providing the program shown below the input -1.3 and -1.1 for the low and high values, respectively, it prints the error message "Error: high gallon value must be larger than or equal to the low gallon value.". However, the test for this error is if(lowGallon > highGallon), which in this given case it clearly is not. What is the explanation for this output error?
The specific section where this input validation is located is under the section with the comment //checking for numerical input errors.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double lowGallon,
highGallon,
literConvert;
const int INCREMENTER = 1;
char charVal;
bool quitting = false,
lowIsNeg = false,
highIsNeg = false,
highIsLessThanLow = false;
cout << "This program creates a gallons to liters conversion table." << endl << endl;
do {
cout << "Enter the lowest gallon value to display (q to quit): ";
cin >> lowGallon;
cout << endl;
do {
//checking for data type input errors
if (cin.fail()) {
cin.clear();
cin >> charVal;
if (charVal == 'q') {
quitting = true;
cout << endl << "Aborting; no conversion performed." << endl;
} else {
cout << "You entered an illegal character: (" << charVal << ")" << endl << endl;
cout << "Enter the lowest gallon value to display (q to quit): ";
cin >> lowGallon;
cout << endl;
}
}
} while (cin.fail() && quitting == false);
if (quitting == false) {
lowGallon = static_cast<int>(lowGallon);
cout << "Enter the highest gallon value to display (q to quit): ";
cin >> highGallon;
cout << endl;
do {
//checking for data type input errors
if (cin.fail()) {
cin.clear();
cin >> charVal;
if (charVal == 'q') {
quitting = true;
cout << endl << "Aborting; no conversion performed." << endl;
} else {
cout << "You entered an illegal character: (" << charVal << ")" << endl << endl;
cout << "Enter the highest gallon value to display (q to quit): ";
cin >> highGallon;
cout << endl;
}
}
} while (cin.fail() && quitting == false);
//checking for numerical input errors
if (quitting == false) {
cout << endl;
if(lowGallon < 0) {
cout << "Error: low gallon value must not be negative." << endl;
lowIsNeg = true;
} else {
lowIsNeg = false;
}
if(highGallon < 0) {
cout << "Error: high gallon value must not be negative." << endl;
highIsNeg = true;
} else {
highIsNeg = false;
}
if(lowGallon > highGallon) {
cout << "Error: high gallon value must be larger than or equal to the low gallon value." << endl;
highIsLessThanLow = true;
} else {
highIsLessThanLow = false;
}
}
if (quitting == false && lowIsNeg == false && highIsNeg == false && highIsLessThanLow == false) {
if (highGallon - static_cast<int>(highGallon) > 0) {
highGallon = static_cast<int>(highGallon) + 1;
}
cout << fixed << setprecision(1) << "The conversion table will be created for the gallon range" << endl;
cout << "of " << lowGallon << " to " << highGallon << " in increments of " << static_cast<double>(INCREMENTER) << endl << endl;
cout << " GALLONS TO LITERS" << endl;
cout << " CONVERSION TABLE" << endl;
cout << " Gallons " << "Liters" << endl;
cout << " ======= " << "=======" << endl;
for(int counter = lowGallon; counter <= highGallon; counter += INCREMENTER) {
cout << setw(9) << setprecision(1) << static_cast<double>(counter);
literConvert = counter * 3.785;
cout << setw(11) << setprecision(3) << literConvert << endl;
}
} else if (quitting == false) {
cout << "Please re-enter low and high gallon values correctly." << endl << endl << endl;
}
}
} while(quitting == false && (lowIsNeg == true || highIsNeg == true || highIsLessThanLow == true));
return 0;
}
In your code you do
lowGallon = static_cast<int>(lowGallon);
which truncates the lowGallon value from -1.3 to -1.0. But you never truncate highGallon value.
The rest follows. -1.0 is indeed greater than -1.1, hence the error message.
Why are you doing this? What's the point of that intermediate conversion to int?