CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Sep 2014
    Posts
    22

    Can someone help me get started?

    So we are working on loops now in class and I was hoping some one can help me get started with this program.

    Here is the problem.
    I know that external links are annoying but the problem is long and it will save space.
    http://pastebin.com/aTX9yRa1

    Here is my code so far.
    Code:
    #include <iostream>
    #include <cmath>
    #include <iomanip>
     
    using namespace std;
     
    int main()
    {    
        int northSouth, eastWest;
        float another;
         
         
        cout << "2014 Old Bruin Farm Fence Builder" << endl;
         
        do{  // North and South Side of Fence
        cout << "What is the Number of North/South Fence posts? ";
        cin >> northSouth;
        if(northSouth <= 1)
        cout << "Value must be at least 2 please try again." << endl;
        }while(northSouth <= 1);
         
        do{  // East and West Side of Fence
        cout << "What is the Number of East/West Fence posts? ";
        cin >> eastWest;
        if(eastWest <= 1)
        cout << "Value must be at least 2 please try again." << endl;
        }while(eastWest <= 1);
         
         
         
        return 0;
    }
    I believe it is working perfect on getting the data, but I am lost when it comes to building the fence.

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

    Re: Can someone help me get started?

    Sorry, but I don't open unknown links.

    Also see http://forums.codeguru.com/showthrea...ork-assignment

    What is your c++ question?
    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++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Sep 2014
    Posts
    22

    Re: Can someone help me get started?

    The code is meant to collect the data from the user on how many fence post are on each side(from 1 to 10).

    The program will then desing a fence that looks like this
    What is the Number of North/South Fence posts? 1
    Value must be at least 2 please try again
    What is the Number of North/South Fence posts? 4
    What is the Number of East/West Fence posts? 3


    |==|==|==|
    : :
    - -
    : :
    |==|==|==|
    if the number is over ten the code will make 10 default.

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

    Re: Can someone help me get started?

    You need three for loops. The first will write the north border of the fence, the second the east and west, and the third the south.

    A for loop is used when you need to perform an action a known number of times, such as when you need to write four fence posts. To create the north fence, with the exception of the last time, each time through the loop, you'll output a |==. The last time, you'll just write a |. See if you can come up with that loop.

  5. #5
    Join Date
    Sep 2014
    Posts
    22

    Re: Can someone help me get started?

    Alright. I am off to another class. I will look into for loops and try it later today.

  6. #6
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Can someone help me get started?

    Just start slow, and improve little by little. For example, this will print a rectange:

    Code:
        for (int i = 0 ; i < northSouth ; ++i)
        {
            for (int j = 0 ; j < eastWest ; ++j)
            {
                cout << "*";
            }
            cout << endl;
        }
    Build up from that.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

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

    Re: Can someone help me get started?

    Quote Originally Posted by monarch_dodra View Post
    Just start slow, and improve little by little. For example, this will print a rectange:

    Code:
        for (int i = 0 ; i < northSouth ; ++i)
        {
            for (int j = 0 ; j < eastWest ; ++j)
            {
                cout << "*";
            }
            cout << endl;
        }
    Build up from that.
    Because the first and last rows look different, I wouldn't use nested loops for this problem. I'd write the first row in one loop, the intermediate rows in another, and the bottom row in a third.

  8. #8
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Can someone help me get started?

    Quote Originally Posted by GCDEF View Post
    Because the first and last rows look different, I wouldn't use nested loops for this problem. I'd write the first row in one loop, the intermediate rows in another, and the bottom row in a third.
    Right, start slow.

    But you'd still need a form of nested loop though, even if you process first and last individually though?

    I'd also recommend using the named functions "printFirstLast" and "printIntermediary".
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

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

    Re: Can someone help me get started?

    Quote Originally Posted by NearFruitcake View Post
    Alright. I am off to another class. I will look into for loops and try it later today.
    You might like to look at http://www.learncpp.com/cpp-tutorial/57-for-statements/
    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++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #10
    Join Date
    Sep 2014
    Posts
    22

    Re: Can someone help me get started?

    So here is what I have so far.
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {    
    	int northSouth, eastWest;
    	float another;
    	
    	
    	cout << "2014 Old Bruin Farm Fence Builder" << endl;
    	
    	do{  // North and South Side of Fence
    	cout << "What is the Number of North/South Fence posts? ";
    	cin >> northSouth;
    	if(northSouth <= 1)
    	cout << "Value must be at least 2 please try again." << endl;
    	}while(northSouth <= 1);
    	
    	do{  // East and West Side of Fence
    	cout << "What is the Number of East/West Fence posts? ";
    	cin >> eastWest;
    	if(eastWest <= 1)
    	cout << "Value must be at least 2 please try again." << endl;
    	}while(eastWest <= 1);
    	
    		for (int i = 0 ; i < northSouth; ++i)
        {
        	cout << "|==";
        }
        	cout << "|" << endl;
        
    		for (int j = 0 ; j < eastWest; ++j)
            {
            cout << endl << ":" << endl;
        	cout << endl << "-" << endl;
            }
    	
    		for (int i = 0 ; i < northSouth; ++i)
        {
        	cout << "|==";
        }
        	cout << "|" << endl;
    	
    	return 0;
    }
    It has three problems that I cannot figure out how to do.

    1. When it is displaying the eastWest loop on the screen it will need to add another line on the right side of the posts. This should line up with the end of the northSouth. I am not to sure on what I will need to use to do this. I was thinking doing a setw for that part. Is that the way to go? If it is I am not sure how that would look.
    2. I need to cap the post at 10 for both northSouth and eastWest. I thought that I could do this with a "if statement, and if northSouth > 10 set northSouth 10." But not sure if that is the way to go or not.
    3. When the program asks the use the amount of posts and they enter in a 0 it goes into an endless loop.

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

    Re: Can someone help me get started?

    Quote Originally Posted by NearFruitcake View Post
    So here is what I have so far.
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {    
    	int northSouth, eastWest;
    	float another;
    	
    	
    	cout << "2014 Old Bruin Farm Fence Builder" << endl;
    	
    	do{  // North and South Side of Fence
    	cout << "What is the Number of North/South Fence posts? ";
    	cin >> northSouth;
    	if(northSouth <= 1)
    	cout << "Value must be at least 2 please try again." << endl;
    	}while(northSouth <= 1);
    	
    	do{  // East and West Side of Fence
    	cout << "What is the Number of East/West Fence posts? ";
    	cin >> eastWest;
    	if(eastWest <= 1)
    	cout << "Value must be at least 2 please try again." << endl;
    	}while(eastWest <= 1);
    	
    		for (int i = 0 ; i < northSouth; ++i)
        {
        	cout << "|==";
        }
        	cout << "|" << endl;
        
    		for (int j = 0 ; j < eastWest; ++j)
            {
            cout << endl << ":" << endl;
        	cout << endl << "-" << endl;
            }
    	
    		for (int i = 0 ; i < northSouth; ++i)
        {
        	cout << "|==";
        }
        	cout << "|" << endl;
    	
    	return 0;
    }
    It has three problems that I cannot figure out how to do.

    1. When it is displaying the eastWest loop on the screen it will need to add another line on the right side of the posts. This should line up with the end of the northSouth. I am not to sure on what I will need to use to do this. I was thinking doing a setw for that part. Is that the way to go? If it is I am not sure how that would look.
    2. I need to cap the post at 10 for both northSouth and eastWest. I thought that I could do this with a "if statement, and if northSouth > 10 set northSouth 10." But not sure if that is the way to go or not.
    3. When the program asks the use the amount of posts and they enter in a 0 it goes into an endless loop.
    First thing to do is format your code so that it's readable.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {    
    	int northSouth, eastWest;
    	float another;
    
    
    	cout << "2014 Old Bruin Farm Fence Builder" << endl;
    
    	do{  // North and South Side of Fence
    		cout << "What is the Number of North/South Fence posts? ";
    		cin >> northSouth;
    		if(northSouth <= 1)
    			cout << "Value must be at least 2 please try again." << endl;
    	}while(northSouth <= 1);
    
    	do{  // East and West Side of Fence
    		cout << "What is the Number of East/West Fence posts? ";
    		cin >> eastWest;
    		if(eastWest <= 1)
    			cout << "Value must be at least 2 please try again." << endl;
    	}while(eastWest <= 1);
    
    	for (int i = 0 ; i < northSouth; ++i)
    	{
    		cout << "|==";
    	}
    	cout << "|" << endl;
    
    	for (int j = 0 ; j < eastWest; ++j)
    	{
    		cout << endl << ":" << endl;
    		cout << endl << "-" << endl;
    	}
    
    	for (int i = 0 ; i < northSouth; ++i)
    	{
    		cout << "|==";
    	}
    	cout << "|" << endl;
    
    	return 0;
    }

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

    Re: Can someone help me get started?

    1). This is where the nested loop monarch_dodra was referring to comes in. Inside your loop that's writing the west side, you'd need a second loop to output spaces to match the north and south boundaries, then output the east boundary.

    2) Modify your do/while to test for values > 10 as well as <= 1

    3) I can't reproduce that.

  13. #13
    Join Date
    Sep 2014
    Posts
    22

    Re: Can someone help me get started?

    How would i line up the boundaries?

    The teacher wants me to accept the value > 10 but when it makes the fence it should max out at 10.

  14. #14
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Can someone help me get started?

    Quote Originally Posted by NearFruitcake
    I need to cap the post at 10 for both northSouth and eastWest. I thought that I could do this with a "if statement, and if northSouth > 10 set northSouth 10." But not sure if that is the way to go or not.
    Sounds about right.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  15. #15
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Can someone help me get started?

    Quote Originally Posted by NearFruitcake View Post
    How would i line up the boundaries?
    Just in case it wasn't clear, you "lineup" the boundaries by printing extra spaces. You aren't asked to move the cursor around or anything like that.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

Page 1 of 2 12 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured