Adding a Character input error but i need assistance

Hello folks, i am having trouble with this one...if i am to input a character value into an integer inside a while loop...it keeps on looping..here is a sample of my code:-
do
{


printf("Enter sideI: \n");
scanf("%d%*c", &sideI);
printf("Enter sideII: \n");
scanf("%d%*c", &sideII);
printf("Enter sideIII: \n");
scanf("%d%*c", &sideIII);

if(sideI || sideII || sideII)
printf("invalid integer");


if((sideI + sideII <= sideIII) || (sideI + sideIII <= sideII) || (sideII + sideIII <= sideI))
printf("Invalid Triangle \n");
else
if((sideI == sideII) && (sideII == sideIII))
printf("The Triangle is Equilateral\n");
else
if((sideI == sideII) || (sideII == sideIII) || (sideI == sideII))
printf("The Triangle is Isosceles\n");
else
printf("The Triangle is Scalene\n");
}while(sideI || sideII || sideIII !=0);

what i am trying to do is adding a character input printf error instead of looping around the condition quickly.What am i doing wrong?
Last edited on
Instead of using scanf (which I believe is deprecated), you could use cin, checking to make sure the input is appropriate. Here's a little hint:

1
2
3
4
5
6
7
8
9
10
		printf("Enter sideI: \n");

		cin >> sideI;
		while(cin.fail())
		{
			cin.clear();    // Repair input stream
			cin >> dummy;     // Clear buffer
			cout << "Enter again..." << endl;
			cin >> sideI;
		}


Also, you need to check the logic in your statement:

1
2
if(sideI || sideII || sideII)
   ...


Hope that gives you a start...

P.S. Please use the code tags ('<>') when posting code.
mm...seems fesible but there is one thing though..i dont use the C++ compiler program what i use is Notepad++ on writing the source code etc and using Borland C Compiler to do the execution job in Windows cmd...i don't know if the one you mentioned above is similar to what i am trying to fix but i would think that it would apply to the C++ that starts with:-
<code>
#include<stdio.h>
#include <ctype.h>
</code>

Last edited on
Oh, so you can't use c++. In that case I don't think you can use the nice IO abstractions like cin. You might need to replace my code above with the C equivalent. A quick search turned up the following page:

http://www.daniweb.com/code/snippet216595.html#

P.S. The code tags can be accessed from the right ('<>') when posting. They are inserted with square braces...
Topic archived. No new replies allowed.