-2

I'm currently writing a C++ program for school that involves taking an input as an amount of change, then telling the user how many quarters, dimes, nickels, and pennies they require to make said change. However, if the user inputs any sort of character or string the program goes into an infinite loop printing two or three of my messages indefinitely. Is there a function or some other method I can use to prevent this from happening?

Edit: Here's some of my code that I think represents the issue

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cctype>
#include <sstream>
using namespace std;
int main ()
{
        cout << "\nMake Change v0.6.4\n";
        cout << "Type 0 at any time to exit the program.\n";
        char confirmExit;
        int amount;
        while (tolower(confirmExit) != 'y')
        // allows the user to continue using the program with having to type a.out everytime
        // but quit the application at any time with two keystrokes and
        // confirmation so as to not accidentally exit the program
        {
                cout << "\nEnter the amount of change as an integer: ";
                // input total cents to be worked with
                cin >> amount;
                if ((amount)!int)
                {
                        cout << "\nMake sure to type an integer!\n";
                }
                else if (amount == 0)
                {
                        cout << "Are you sure you want to exit the program(y/n)? ";
                        cin >> confirmExit;
                        // confirmation to prevent accidentally exiting out
                }
    cout << "\n";
    return (0);
}
Thomas Ahrens
  • 13
  • 1
  • 2
  • 9
  • Post your code and someone may be able to help. – Paul Rooney Jan 19 '17 at 04:33
  • Sorry about that, I've made some edits. – Thomas Ahrens Jan 19 '17 at 04:47
  • I don't see any loops. Your code demonstrating the problem is not complete. Ideally you should provide a sample (after deleting everything irrelevant to your problem) that can be copy/pasted, compiled and run to demonstrate the problem. – Disillusioned Jan 19 '17 at 04:53
  • Got it. I've added everything except the portion that works with the inputted integer now, I think. – Thomas Ahrens Jan 19 '17 at 05:01
  • Consider this post: http://stackoverflow.com/questions/545907/what-is-the-best-way-to-do-input-validation-in-c-with-cin – Mikel F Jan 19 '17 at 05:29
  • Your brackets don't match up which still means you don't have something that can be copy/pasted from your question to reproduce your problem. Indentation suggests you need another closing brace `}` before `cout << "\n";` – Disillusioned Jan 19 '17 at 10:39

2 Answers2

1

In C++ everything is bits and implicit conversion can takes place to convert string or other forms of data to int value. You can use limits header file in standard library and set the bound for max and min value of input.If you can't get it then comment below. I will post code.

This link might be useful- Visit http://lolengine.net/blog/2012/02/08/selectively-restrict-implicit-conversions

Sourabh Mittal
  • 261
  • 1
  • 2
  • 7
0

see the functions defined below:

atoi

strtol

ufukgun
  • 6,889
  • 8
  • 33
  • 55