Results 1 to 12 of 12

Thread: C++ beginner's problem

  1. #1
    Member
    Join Date
    Oct 2001
    Location
    Gainesville, FL
    Posts
    155

    C++ beginner's problem

    I need to write a program that inputs an integer and a character, and outputs a diamond composed of the character and extending the width specified by the integer. I can only use 'while' loops at this stage of the course, and I know it has to be nested 'while' loops. I can not use 'for/next' loops. I have done this before in VB6, so I am somewhat exasperated that I can not even get past firstbase with this same problem in C++. And I like C++, strangely enough. I know I would be better off thinking this out for myself, but at this point, time is of the essence and I need help fast.

    Any suggestions would be greatly appreciated.

  2. #2
    Member
    Join Date
    Apr 2001
    Posts
    187
    diamond like this?
    a, 5


    a
    aa
    aaa
    aaaa
    aaaaa
    aaaa
    aaa
    aa
    a


    not quite sure on ur specs..



    it cut out the leading spaces...but, you get the idea..

  3. #3
    Member
    Join Date
    Oct 2001
    Location
    Gainesville, FL
    Posts
    155
    Yes, but with the leading spaces, so that using your letter and number, it would be 5 rows, 5 wide:

    a
    aaa
    aaaaa
    aaa
    a

    The leading spaces is what is throwing me: the leading spaces decrease and the a's increase.
    (yeh, this system is throwing out the leading spaces)

  4. #4
    Senior Member Banti's Avatar
    Join Date
    Dec 1999
    Location
    No Longer at Sysopt
    Posts
    903
    number of space will be equal to integer entered - number of current a's divided by 2

    (i-a+1)/2

    The +1 takes care of the case of the integer entered being even. Although it will still look a little funny.

    Banti

  5. #5
    Senior Member Banti's Avatar
    Join Date
    Dec 1999
    Location
    No Longer at Sysopt
    Posts
    903
    by the way to get the

    Code:
      *
     ***
    *****
     ***
      *
    affect use the [ code ] [ /code ] options without the spaces


    Banti
    Last edited by Banti; 10-09-2001 at 09:56 AM.

  6. #6
    Junior Member
    Join Date
    Aug 2001
    Posts
    6
    Code:
    #include <iostream.h>
    
    int main(void)
    {
    	while(0)
    		while(0){
    			//obligatory while statements
    		}
    
    	cout << "  *  \n";
    	cout << " *** \n";
    	cout << "*****\n";
    	cout << " *** \n";
    	cout << "  *  \n";
    	return 0;
    }

  7. #7
    Junior Member
    Join Date
    Aug 2001
    Posts
    6
    ok seriously,

    Is it a req for you to use nested while's ? I think it's a bit easier using just one while loop. (not to mention more readable). I assumed you're allowed at least one if statement. if not...
    Code:
    #include <iostream.h>
    #include <math.h>
    
    int main(void)
    {
    	float MaxWidth; 
    	float RowCount, ColCount, MidMarker, yEquiv, xEquiv;
    	char DisplayChar;
    
    	cout << "\nDisplay Character> ";
    	cin >> DisplayChar;
    	cout << "\nEnter Max Width> ";
    	cin >> MaxWidth;
    		
    	MidMarker = ( MaxWidth + 1 )/2;
    	
    	RowCount = MaxWidth;
    	while( RowCount ){
    		ColCount = MaxWidth;
    		while( ColCount ){
    			yEquiv = RowCount-MidMarker;
    			xEquiv = ColCount-MidMarker;
    			// (sqrt of FLOAT*FLOAT) is used to take abs() of a float
    			if( sqrt(yEquiv * yEquiv) + sqrt(xEquiv * xEquiv) < MidMarker )
    				cout.put(DisplayChar);
    			else
    				cout << " ";
    			ColCount--;
    		}
    		cout << endl;
    		RowCount--;
    	}
    	return 0;
    }
    There's prob a better way to take the abs of a float but i didn't bother skimming through math.h.

    Dunno when your hw is due. good luck.

  8. #8
    Member
    Join Date
    Oct 2001
    Location
    Gainesville, FL
    Posts
    155
    This is what I finally did:

    int main()

    {
    int width = 0;
    char sym;
    char space = ' ';
    int count = 0;
    int line = 0;
    int w = 0;
    int numChar = 0;
    // get input
    cout << "Enter a symbol, and press Enter: ";
    cin >> sym; //Put the value in variable.

    cout << "Specify a width in a whole positive integer, and press Enter: ";
    cin >> width;//Put the value in variable.

    if (width%2 != 1)
    { width++;
    }
    w = width/2;
    count = 0;
    line = 0;
    numChar = 1;

    while ( line <= w )//Start the line counter.
    {
    while (count <= w )
    {
    cout <<space;// display output
    count ++;//Increment
    }
    count = 0;//Reset the counter for this loop.
    while (count < numChar )
    { cout <<sym; // display output
    count ++;//increase counter.
    }
    numChar+=2;//Increment by two for proper format.
    count =0; //Reset counter.
    cout <<endl;//
    w--;//Decrement space counter.
    }
    w = width/2;
    count = 0;
    line = 1;
    numChar -= 4;

    while ( line <= w )
    {
    while (count <= line)//Start the spaces counter.
    {
    cout <<space;// display output.
    count ++; //increase counter.
    }

    count = 0; //Reset counter for this loop.
    while (count < numChar )//Start counter.
    {
    cout <<sym;
    count ++; //Increment.
    }
    numChar-=2; //Reduce number of characters by two.
    count = 0; //Reset counter for space loop.
    line++; //Increment line counter.
    cout <<endl; }
    // code to pause the program. Will pause the .exe
    cout << "\n\n\t\t Hit Enter" << endl;
    cin.get(); // the program waits

    return 0;
    }

    And a big thankyou.

  9. #9
    Banned qball's Avatar
    Join Date
    Oct 1999
    Location
    Rockaway, NJ 07866
    Posts
    1,730
    Oh, boy...

    tightlines,

    your code doesn't compile in Borland C++5.5.

    HyperSlug,

    Your code does compile and work, but you have a problem. It hangs (infinite loop, I think) iffin user just hits 'enter'. One should validate all user input.

  10. #10
    Member
    Join Date
    Oct 2001
    Location
    Gainesville, FL
    Posts
    155
    Yes, your right. Good idea, I'm going to amend it immediately.
    Thanks.
    I'm using Visual C++ 6.0

    Originally posted by qball
    Oh, boy...

    tightlines,

    your code doesn't compile in Borland C++5.5.

    HyperSlug,

    Your code does compile and work, but you have a problem. It hangs (infinite loop, I think) iffin user just hits 'enter'. One should validate all user input.
    Last edited by tightlines; 10-09-2001 at 09:10 PM.

  11. #11
    Junior Member
    Join Date
    Aug 2001
    Posts
    6
    true, i didn't really have robustness on my mind.

    my old CS teacher told me to make the code good enough "so a monkey could bang on the keyboard."

    besides, i figured tightlines would handle the error checking.

    but you're right, all production level code needs to fail gracefully.

    BTW, tightlines' code will compile if you put in a #include<iostream.h>

  12. #12
    Member
    Join Date
    Oct 2001
    Location
    Gainesville, FL
    Posts
    155
    I put in just about ALL the header files just to be safe at this juncture.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •