Thread: On the output screen, print a diamond of stars, for loop c++ help?

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    6

    On the output screen, print a diamond of stars, for loop c++ help?

    Code:
    #include<iostream>
    using namespace std;
    int main(void)
    {//declaration of variables
    
    int num;    //value of first integer
    int count;  //value of second integer
    cout<<"Please enter an integer between 1 and 10"<<endl;  //tells user to enter a number bet. 1-10
    cin>>num;
    if (num<1 || num>10)     //reenter if number is less than 1 or greater than 10
    {
          cout<<" Please insert correct number"<<endl;
          cin>>num;
    }
    
    for(count=1; count<num; count+=2)  // counting stars by 2 each time from beginning 1 to the number user entered 
    {
    for(int num = 0; num < count; num++)
    cout<<"*";
    cout<<endl;
    }
    
    for(count; count>0; count-=2)   // subtraction stars by 2 each times from the num user entered to 1
    {
    for(int num = 0; num < count; num++) 
    cout<<"*";        
    cout<<endl;
    }
        
     system("PAUSE");
     return 0;
    }
    
    
    
    somehow my printed diamond program doesn't work with even number i entered.  anyone know what's wrong with it?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    The first question that comes to mind is: how is your program intended to work?

    That raises another question: how exactly does your program fail to work as intended?

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    6
    the first part is like a pyramid and the second part is pyramid but upside down.
    Should i use a mod operator to test for even or odd first cuz isn't pyramid pgm will only work for odd numbers.. oh another question is how i line up the star like a triangle

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    Well first of all its unnecessary to put void as an argument in main. If the user fails to input a number between 1 and 10 a second time it is accepted input. You should change that to something like this
    Code:
    int number = 0; 
    while(!(number > 1 && number < 10))
    {
        cout <<"Please Input A Number Between 1 and 10";
        cin >> number;
        system("cls"); // YAY FOR UNRELIABLE RESOURCE HEAVY CLEARSCREEN //
    }
    Then i would stop and look at my next block of code and see that i declared a local variable with the same name as a variable in a outer scope. Defaultly the one you created is the one used. So change that to something like this. Also the way you had it if the user inputed lets say 5 it would go to that function and output something like this.

    Code:
    for(count = 0;count < number;count += 2) //Dunno why count was one//
    {
         cout <<"*" << endl;
    }
    
    Then
    
    for(;count > 0; count -= 2)
    {
       cout <<"*"<<endl;
    }
    but really i dont know what your trying to do :/
    Last edited by Nathan the noob; 07-30-2010 at 08:38 PM.
    Who needs a signature?

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    6
    ----*
    ---***
    --*****
    -*******
    *********
    -*******
    --*****
    ---***
    ----*
    that is sample kind of code that i wanted to print out...without dotted line

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    So you want the user to type in how many lines the diamond takes up?
    Like 5 would = too
    ----*
    ---**
    --***
    ---**
    ----*
    Who needs a signature?

  7. #7
    Registered User
    Join Date
    Jun 2010
    Posts
    6
    YEA i'm pretty sure that what i have to do but at increment of 2 i think.


    When user enters a number, check if it is even or odd using mod operator, if it is even then add one to the number. Diamond pgm would work only for odd numbers. However, to simplify the assignment we made the assumption that user will enter only odd numbers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. constantly print an array to the screen?
    By .exe in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2008, 10:41 PM
  2. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 05:15 AM
  3. controlling output to screen
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 03-23-2002, 04:23 PM
  4. screen output design
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 02-08-2002, 10:14 PM