Hi, I just joined this forum today. I have a problem where my program keeps looping infinitely if i enter a character. I've read the programming FAQ but I think its in c and not c++. I'm sorry I'm just starting to learn this.
I've tried isdigit but it doesnt work I guess. I've tried doin it another way, declaring x as char then converting it to ASCI but it only works for single digit numbers. I'm stumped. I think I'm missing something but I'm not sure what.
This is an assignment question but I've already passed it up. My interview with my lecturer is this Monday so I'm thinking what I'm gonna answer if he enters 'a' and my program loops infinitely.
Can anyone tell me what I did wrong?Code:/* wo0dy in the lab March 22nd 2006 Wednesday 9am CR3050 Assignment 2 (4) Write a program to print a triangle of stars. The program should take as input an integer (no. of lines to be printed). The program should display an error message if user enters the no. of lines less than 0 or greater than 20. Some sample outputs are given below. Hint: Use while loop and for loops. */ #include <iostream> #include <cctype> #define VIEW '*' using namespace std; int main() { int i, j, x; bool repeat=true; while ( true ){ cout << "\nEnter the number of star lines (1 to 20) to be printed: "; cin >> x; cout <<endl; if ( x<=20 && x>0 ){ repeat=false; for ( i=1; i<=x; i++ ) { for ( j=1; j<=x-i; j++ ) cout<<" "; for ( j=1; j<=2*i-1; j++ ) cout<<VIEW; cout<<"\n"; } } //else if ( isdigit (x)){ // cout<<"Please enter an integer."<<endl; // } if ( x<=20 && x>0 ) break; } cout<<"\a"<<endl; system ("PAUSE"); return 0; }