Making a pyramid of stars help

This is a discussion on Making a pyramid of stars help within the C++ Programming forums, part of the General Programming Boards category; Hi, I just joined this forum today. I have a problem where my program keeps looping infinitely if i enter ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    4

    Making a pyramid of stars help

    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.

    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;
          
      }
    Can anyone tell me what I did wrong?

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    It works for me when I enter a number. Do you mean if you enter "T" or something when it is prompting, why does it loop forever?

    Try:

    Code:
       if (!(cin>>x)) {
           cout << "Enter a number dummy." << endl;
           continue;
       }
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  3. #3
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Oops. Sorry, you need:

    Code:
       if (!(cin>>x)) {
           cin.clear();
           cin.ignore(INT_MAX,'\n');
           cout << "Enter a number dummy." << endl;
           continue;
       }
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Cannonball Pyramid Calculator (tetrahedron)
    By Zedaine in forum C++ Programming
    Replies: 3
    Last Post: 01-13-2009, 04:52 AM
  2. Program to make stars mostly done, need help?
    By patso in forum C Programming
    Replies: 4
    Last Post: 04-07-2008, 11:45 PM
  3. For Loops (Making Stars)
    By GCNDoug in forum C++ Programming
    Replies: 5
    Last Post: 10-05-2007, 12:34 AM
  4. Making a pyramid using recursive functions
    By MikeBahu in forum C++ Programming
    Replies: 7
    Last Post: 07-22-2007, 12:58 AM
  5. Making a pyramid of Xs
    By Tride in forum C++ Programming
    Replies: 8
    Last Post: 12-12-2003, 05:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21