Dear all,

when i run the prog below it works fine when I input an integer but if I enter a char by mistake the output to the screen goes mad.

what i want to do is write a piece of code that

1) if char input then print to screen "you have chosen to Exit"

Any idea what the code might be.

regards

Tim


#include <iostream>
#include <stdlib.h>
using namespace std;

int main(int argc, char *argv[])
{
int no;

do{


cout<<"Enter a Number"<<endl;
cin>>no;
cout<<no<<endl;
} while ( no !=5);


system("PAUSE");
return 0;
}

Recommended Answers

All 2 Replies

#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
	char no ;

	do{
		cout<<"Enter a Number"<<endl;
		cin>>no;
		cout<<no<<endl;
	} while ( no != '5' );


	system("PAUSE");
	return 0;
}

This is much better. Wont go mad at characters. But you will have to guard against multiple characters entered in the same line.

simple yet just what I was looking for.
thx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.