-
October 20th, 2014, 08:38 AM
#1
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.
-
October 20th, 2014, 08:47 AM
#2
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)
-
October 20th, 2014, 08:54 AM
#3
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.
-
October 20th, 2014, 09:14 AM
#4
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.
-
October 20th, 2014, 09:18 AM
#5
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.
-
October 20th, 2014, 09:31 AM
#6
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.
-
October 20th, 2014, 09:57 AM
#7
Re: Can someone help me get started?
Originally Posted by monarch_dodra
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.
-
October 20th, 2014, 10:05 AM
#8
Re: Can someone help me get started?
Originally Posted by GCDEF
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.
-
October 20th, 2014, 11:21 AM
#9
Re: Can someone help me get started?
Originally Posted by NearFruitcake
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)
-
October 20th, 2014, 06:50 PM
#10
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.
- 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.
- 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.
- When the program asks the use the amount of posts and they enter in a 0 it goes into an endless loop.
-
October 20th, 2014, 07:49 PM
#11
Re: Can someone help me get started?
Originally Posted by NearFruitcake
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.
- 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.
- 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.
- 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;
}
-
October 20th, 2014, 07:57 PM
#12
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.
-
October 20th, 2014, 08:56 PM
#13
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.
-
October 21st, 2014, 02:38 AM
#14
Re: Can someone help me get started?
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.
-
October 21st, 2014, 04:08 AM
#15
Re: Can someone help me get started?
Originally Posted by NearFruitcake
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|