Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 9th, 2005, 4:16 AM   #1
Ade
Hobbyist Programmer
 
Ade's Avatar
 
Join Date: Oct 2004
Location: England, UK
Posts: 139
Rep Power: 0 Ade is an unknown quantity at this point
restricting cin

Hi guys, me again with another pissing college assignment......

Basically we need to make a programme that will simulate a telephone. It all works fine and is great and everthing is good in the world. Until, I'm doing error testing and find that when I input a coin value as a character such as 'h' it completly trips out and goes into an infinate loop!

Here's the code for the function.....

int coins () // the coin input is stored as a function as it is used twice
{
 
  for(;;)
  {
  cin>> value;
  
    if (value == 0)
    {
		break;
    }
    
    else
    {}

	if (value != 10 &&
	    value != 20 &&
	    value != 50 &&
	    value != 100)
	{
		value = 1;
	}
  
  switch(value)
  {
    case 10:
    total = total + value;
    remaining = total / charge;
    cout<< endl << "You entered " << value << "p. Your total is "
      << total << "p. That equals " << remaining << " seconds. "
        << endl << "Enter another coin or '0' to dial."
          << endl << endl;
	break;

	case 20:
    total = total + value;
    remaining = total / charge;
    cout<< endl << "You entered " << value << "p. Your total is "
      << total << "p. That equals " << remaining << " seconds. "
        << endl << "Enter another coin or '0' to dial."
          << endl << endl;
	break;
	
	case 50:
    total = total + value;
    remaining = total / charge;
    cout<< endl << "You entered " << value << "p. Your total is "
      << total << "p. That equals " << remaining << " seconds. "
        << endl << "Enter another coin or '0' to dial."
          << endl << endl;
	break;
	
	case 100:
    total = total + value;
    remaining = total / charge;
    cout<< endl << "You entered " << value << "p. Your total is "
      << total << "p. That equals " << remaining << " seconds. "
        << endl << "Enter another coin or '0' to dial."
          << endl << endl;
	break;

	default:
    cout<< endl << "Your last coin was not valid, please try again"
      << " using one of the accepted coins."
        << endl << endl;
	break;

  }

  cout.flush();

  }

  return 0;
}

My teacher suggested that cout.flush() would solve the problem, but needless to say it didn't.

basically I need to be able to make it so the only values that can be entered are 10, 20 ,50 and 100.



cheers guys
Ade
__________________
Don't wound what you can't kill

Last edited by Ade; Feb 9th, 2005 at 4:20 AM.
Ade is offline   Reply With Quote
Old Feb 9th, 2005, 8:23 AM   #2
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 4 Eggbert is on a distinguished road
Aside from using a nonstandard method to read keystrokes, the only good way to go about what you want is to validate the input:
#include <iostream>
#include <limits>

using namespace std;

// Clear stream state
template <typename Ch, typename Traits>
basic_ios<Ch, Traits>& clear ( basic_ios<Ch, Traits>& io )
{
  io.clear();
  return io;
}

bool valid_coin ( int coin )
{
  return coin == 10 || coin == 20 || coin == 50 || coin == 100;
}

int main()
{
  int coin;

  while ( true ) {
    cout<<"Enter 10, 20, 50, or 100: ";
    if ( !( cin>> coin >> clear ) || !valid_coin ( coin ) ) {
      cerr<<"Invalid entry, please try again"<<endl;
      // Empty the stream of bad characters
      cin.ignore ( cin.rdbuf()->in_avail() );
    }
    else
      break; // Everything is okay
  }

  cout<<"You entered: "<< coin <<endl;
  cin.get();
}
Eggbert is offline   Reply With Quote
Old Feb 9th, 2005, 9:44 AM   #3
Ade
Hobbyist Programmer
 
Ade's Avatar
 
Join Date: Oct 2004
Location: England, UK
Posts: 139
Rep Power: 0 Ade is an unknown quantity at this point
Oh my god!!

Eggbert, you're not dead!!

PS thanks for the reply !
__________________
Don't wound what you can't kill
Ade is offline   Reply With Quote
Old Feb 9th, 2005, 4:40 PM   #4
Ade
Hobbyist Programmer
 
Ade's Avatar
 
Join Date: Oct 2004
Location: England, UK
Posts: 139
Rep Power: 0 Ade is an unknown quantity at this point
OK, I just tested your code Egg and it does the same as mine, if I enter a character such as L, y, t etc. it enters an infinate loop.

I have no idea why????
__________________
Don't wound what you can't kill
Ade is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:53 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC