First

Discussion in 'C++' started by matviy, Aug 4, 2007.

  1. matviy

    matviy New Member

    Joined:
    Aug 4, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    I started out with C++ a few days ago and im trying to create my first program. Its supposed to prompt you for a character (example: 5,#,%,+) anything. Then draw a 20 x 10 box using that character.

    ########
    #..............#
    #..............#
    ########

    Something like that. Since i'm learning new concepts, i tried to use loops and "if" statements to get this done. Each time it draws a line, it adds +1 to a "line counting" variable. When the couting variable = 1, it draws a full line, when its anywhere between 1-10 it draws the character once, a huge space in between (char space) then it draws the character once again. When it finishes drawing the 9th line, and the "line couting" variable becomes 10, it draws the last line, adds +1 again, and then breaks out of the loop because the "line counting" value is no longer < 11

    For some reason, this doesn't work at all. Try running it for yourself. line > 11 starts to loop, line < 11 dosnt do anything.

    Code:
    #include <iostream.h>
    
    int main()
    {
    	int line = 1; //line counting cariable
    	char space[20] = "                  "; //Space variable
    	char c[2] = ""; //Character to draw the box with.
    		
    		//Prompts user to give "c" a vale.
    		cout << "Please enter a single character (eg. *,%,R,7): ";
    		cin.getline(c,2);
    	
    		//Should begin the loop
    	while (11 < line); //for some reason, this dosnt start the loop. 11 < line does start it.
    	{
    		if ((line = 1) || (line = 10)); //Should draw the first, or last line.
    		{
    			cout << "\n" << c << c << c << c << c << c << c << c << c << c << c << c << c << c << c << c << c << c << c << c;
    			line = 1 + line;
    		}
    	
    		if ((line > 1) && (line < 10)); //Should draw all the lines in between, of the line value is between 1-10 
    		{
    			cout << "\n" << c << space << c;
    			line = 1 + line;
    		}
    	}
    }
    
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Well, you set line to 1, then you say "while (11 < line)". Last time I looked, 11 wasn't less than 1. I wouldn't start, either.
     
  3. matviy

    matviy New Member

    Joined:
    Aug 4, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Apologies, i made a big typo.

    The only way for me to start the loops is (line > 11) or (11 < line). I cant imagine why it works. When i try (line = 1) it still wont start. If i try (line != 11) it STILL doesn't start. It simply prompts me to enter a character, then when i hit enter, it just hangs.

    When i do (line > 11) or (11 < line) and the loops seems to start, i get only part of my box:

    ################
    #...............................#

    So it seems to go through the loop one time, then come to its senses, realizes that 1 isn't greater than 11, and quits the loop. I am really clueless about what might be the problem. Could someone try running this on their own system and seeing if it works? Maybe its just a problem with my setup.
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    The culprit is condition
    Code:
    ((line = 1) || (line = 10)
    You should use == instead of = or else it will assign the value 1.

    [COMMENT]Also I like the idea of making space into the post but you could have used the code block.[/COMMENT]
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice