Need help with aterik loops
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Need help with aterik loops

  1. #1
    Join Date
    Oct 2017
    Posts
    1

    Need help with aterik loops

    Basically I need to use loops to print the letters "T" "O" and "L". the program ask for a size and the letters. Any help would be appreciated.

    Code:
    #include<iostream>
    using namespace std;
    int main()
    {
    	char T, O, L;
    	char letter;
    	int number, size;
    	size = number % 2;
    
    		cout << "Welcome to the letter printer." << endl;
    		cout << "Enter the size : " << endl;
    		cin >> size;
    
    		while (size <= 5 || size == 0)
    		{
    			
    			cout << "Invalid size. Enter the size again: ";
    			cin >> size;
    		}
    
    		cout << "Enter the letter: " << endl;
    		cin >> letter;
    
    		while(letter != 'T' && letter != 'O' && letter != 'L')
    		{
    			cout << "Invalid Letter. Enter the letter again:";
    			cin >> letter;
    			{
    
    
    				while (letter == 'T')
    				{
    					for (int i = 0; i < size; i++)
    					{
    						for (int j = 0; j < size; j++)
    						{
    							cout << "*";
    						}
    						cout << endl;
    					}
    				}
    			}
    
    }
    Last edited by 2kaud; October 12th, 2017 at 04:02 AM. Reason: Added code tags

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,835

    Re: Need help with aterik loops

    When posting code, please use code tags so that the code is readable. Go advanced, select the formatted code and click '#'.

    What issues are you having - compile errors, run time problems??

    Code:
    int number, size;
    size = number % 2;
    You are using number to calculate the value of size, but at this point in the program number hasn't been initialised with a value so when used as part of the size calculation it's value is unknown and hence so is the value of size. But you then go on to enter a value for size which overwrites its previous value.

    Code:
    while (size <= 5 || size == 0)
    If size is 0 then this is less than 5, so the test for equality to 0 is not needed.

    Code:
    while(letter != 'T' && letter != 'O' && letter != 'L')
    {
         cout << "Invalid Letter. Enter the letter again:";
         cin >> letter;
        {
    If an invalid letter is entered then an error message is displayed, but then the code continues - it doesn't loop over the while again. Look at how the test for invalid size was coded.

    For displaying the letter, you are going to have to decide how these letters are to be constructed from *. The code you have just displays a block of *.
    eg for T, you need to display size * on one line and then for the next size - 1 lines display one * in the correct place.
    Last edited by 2kaud; October 12th, 2017 at 05:22 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,507

    Re: Need help with aterik loops

    What's the point of this line?
    size = number % 2;

  4. #4
    Join Date
    Sep 2017
    Posts
    1

    Re: Need help with aterik loops

    Hello guys! I'm working on very important project now and have problems with aterik loops too. I will try to use you advice, maybe it will help to fix errors in my program. Many thanks for useful link, I was trying to find answers at this [web site address removed] and http://forums.codeguru.com/showthrea...ork-assignment, but there is anything useful for problem in my case.
    Last edited by 2kaud; November 8th, 2017 at 06:00 AM.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)