Hi eveyone. I am very new to C++ and have an assignment coming up. I understand most of it except for two parts. The For Loop and the layout in the output file. The assignment is to do mailing labels. They will be in the format:
Michael Smith
555 Smith Street
Philadelphia, PA 15728
Box 1 of 5
The teacher wants them to be printed
BOX 1 BOX2
BOX 3 BOX 4
BOX5
How would I go about splitting the page like that and forcing the program to print them that way. Also the for loop I am a bit uncertain of how to put it together. Can anyone maybe explain the for loop a little? Thanks guys. Any help you can provide would be so appreciated.
17 2410 Ganon11 3,652
Recognized Expert Specialist
In order to print in that way, you'd have to print the first lone of BOX 1, the first line of BOX 2, then a newline character, then the second line of BOX 1, then the second line of BOX 2, then a newline character, etc. etc, repeating the same way with BOX 3 and BOX 4.
As for the for...loop, it really depends on how you want to use it. For loops in and of themselves are very simple - what exactly are you using the loop to do, and what don't you understand?
If you are doing the same mailing label over and over again each time, you can create a variable (most likely a string) and then use the for loop to print it out.
The for loop is setup as follows: - for(initialization; test; update){
-
statement;
-
statement;
-
}
-
What does this mean? Start out by declaring all of the variables you need to print out. I would go with 4 different string variables...let's call them string name, address, state, and box. You could also create a variable called size to indicate how many times you want the loop to execute.
Then in the for loop you have to create another variable to scan through the data and print it out. You may have seen something like this in your textbook: - for(int i=0; i<size; i++)
or - for(int i=1; i<size; i++)
Think of 'i' as the first instance of whatever it is that you want repeated. In the case of your labels, the first loop through will print out each of the strings once. The second loop will print out each of the strings a second time, and so on and so forth.
What you need to remember about the for loop you are trying to write is that you will actually print out the string name, skip a good amount of space, and then print out the string name again. Then you will endl and print out the address, skip space, and print out address again. You will continue to do this for each of the strings.
Once the loop has completed once through, you should have two labels printed out, side by side. The counter, i++, will then increment and carry out the loop again. The loop will continue to do this until the counter reaches 'size'.
In case you haven't learned this yet, check out the functions 'setw' and 'setprecision', which are a part of the <iomanip> library. They will help you with spacing and digits for numbers.
The cool thing about this assignment, to me at least, is that you could include an increment counter to change the number of the box that is printing out. If you do that, you might not want to make a string variable box. I would create another variable (if you used 'i' again it will throw off your for loop) and increment it every time that line of code is printed out. You don't have to pay any attention to this though for your assignment unless your professor explicitly requires it.
Let me know if this helps or if I've only confused you further.
Thanks that was helpful. For the increment counter would I put something like.
box_number = 1;
cin >> name;
cin >> street_address;
cin >> city, state, zip_code;
cin >> "Box" >> box_number++ >> "of" >> number_of_boxes>> '\n';
But how do I make sure it stops at say the person enters 5 boxes. How do I make it stop there?
oler1s 671
Recognized Expert Contributor
Not even close. For one thing, I don’t see the word “for” in your post. Notice how that might be a problem when you’re trying to write a for loop? I’m not convinced you know what you’re doing. More like you’re shooting in the dark.
Step back a moment. Forget the for loop. Forget the algorithm. You have these five boxes with appropriate mailing data. How is that information being stored. Don’t tell me a bunch of strings. The compiler doesn’t understand “a bunch of strings”. Show me how the box data is being stored, in what is valid C++. If we can’t even get the datatype down, there’s no point in writing loops or anything else.
Aside: Have you covered structs or classes yet?
oler1s 671
Recognized Expert Contributor
From what I can tell, you don’t seem to intuitively understand how to break down this question. And you’re sloppily putting together code, so I see something like “cin >> city, state, zip_code;”. That isn’t valid at all.
I hate to be so pointedly rude, but I have to ask, are you familiar with the material? “How do I make it stop there?” By writing the for loop to terminate after going through all the boxes. Unless you never read for loop material properly, you should understand how to do so.
If you don’t know the material, we can’t help you short of writing your code. Which we won’t do. So you need to tell us if you don’t understand how some syntactical construct, like a for loop, or arrays, or whatever, work. And you need to tell us your thought process. We can’t correct your logic if you don’t reveal to us what you are thinking.
Are you going to be getting the data for the mailing label from the user? If so, you can put that step on hold and just cout your own personal info to test everything.
The cin stuff that you listed needs to go in the statement section of the for loop. Try this out, copy your code, and paste it here with the code brackets around your code block (see the reply guidelines) and we'll try to help show you what else you need to do. It's all about trying this out though....
I know I can't post the whole code so can I send it to one you guys?
You can post the whole code. Just make sure you type "[code]" (without the quotation marks), copy your code, and then type "[/code" at the end of it.
- #include <iostream.h>
-
#include <stdlib.h>
-
#include <cstring>
-
#include <string>
-
#include <fstream>
-
-
using namespace std;
-
string name; // Is the name for the mailing labels
-
string street_address; // Is the address for the mailing labels
-
string zip; // Is the zipcode for the mailing labels
-
string state; // Is the state for the mailing labels
-
string city; // Is the city for the mailing labels
-
int number_of_boxes; // Number of labels neeeded
-
int box_number; //Current Label
-
ofstream mailing_labels;
-
-
int main()
-
{
-
mailing_labels.open("D:\\mailing_labels.dat"); //Opens the file
-
mailing_labels<<setw(40)<<" "<<setw(40)<<" "<<'\n'<<'\n'; //Breaks the page into two columns //Accepts number of boxes
-
-
for(box_number=1;box_number ==number_of_boxes;box_number ++)
-
{
-
cout <<"Please enter the name of the person you are shipping these items to: " <<'\n'; //Asks for the name
-
cin >> name; //Accepts the name
-
cout <<"Please enter the street address you are shipping to: "<<'\n'; //Asks for the street address
-
cin >> street_address; //Accepts the street address
-
cout <<"Please enter the city you are shipping to: "<<'\n'; //Asks for the city
-
cin >> city; //Accepts the city
-
cout <<"Please enter the two letter abbreviation of the state you are shipping to: "<<'\n'; //Asks for the state
-
cin >> state; //Accepts the state
-
cout <<"Please enter the five digit zip code you are shipping to: "<<'\n'; //Asks for the zip code
-
cin >> zip; //Accepts the zip code
-
cout <<"How many boxes are you shipping? "<<'\n'; //Asks for number of boxes
-
cin >> number_of_boxes;
-
setf(ios||left);
-
mailing_labels<<setw(40)<<name<<'\n';
-
mailing_labels<<setw(40)<<street_address<<'\n';
-
mailing_labels<<setw(33)<<city<<setw(2)<<state<<setw(5)<<zip<<'\n';
-
mailing_labels<<setw(4)<<"Box "<<setw(2)<<box_number<<setw(4)<<" of "<<setw(2)<<number_of_boxes<<'\n'<<'\n';
-
}
-
mailing_labels.close();
-
-
system("PAUSE");
-
return 0;
-
}
I'm getting these errors as well.
c:\docume~1\stepha~1\desktop\mailin~1.cpp: In function `int main()':
c:\docume~1\stepha~1\desktop\mailin~1.cpp:20: implicit declaration of function `int setw(...)'
c:\docume~1\stepha~1\desktop\mailin~1.cpp:36: parse error before `||'
I fixed the one error. I forgot to insert #include <iomanip>.
Ganon11 3,652
Recognized Expert Specialist
You might mean to say setf(ios::left);
The || is the logical OR operator.
Thanks Gannon.
I'm still getting errors though.
c:\docume~1\stepha~1\desktop\mailin~1.cpp: In function `int main()':
c:\docume~1\stepha~1\desktop\mailin~1.cpp:39: implicit declaration of function `int setf(...)'
Ganon11 3,652
Recognized Expert Specialist
Isn't setf in iomanip.h? Try adding:
Isn't setf in iomanip.h? Try adding:
Still getting the same error.
I figured it out. It was suppose to be mailing_labels.setf(ios::left);. I have the bulk of the program working. The only thing I am having trouble with is getting it to print in two columns. Like
BOX 1 BOX 2
BOX 3 BOX 4
BOX 5 - #include <iostream.h>
-
#include <stdlib.h>
-
#include <string>
-
#include <fstream>
-
#include <iomanip>
-
-
using namespace std;
-
char name[40]; // Is the name for the mailing labels
-
char street_address[40]; // Is the address for the mailing labels
-
string zip; // Is the zipcode for the mailing labels
-
string state; // Is the state for the mailing labels
-
char city[20]; // Is the city for the mailing labels
-
int number_of_boxes; // Number of labels neeeded
-
int box_number; //Current Label
-
ofstream mailing_labels;
-
-
int main()
-
{
-
mailing_labels.open("C:\\Documents and Settings\\All Users\\Desktop\\mailing_labels.dat"); //Opens the file
-
mailing_labels<<setw(40)<<" "<<setw(40)<<" "<<'\n'<<'\n'; //Breaks the page into two columns //Accepts number of boxes
-
cout <<"Please enter the name of the person you are shipping these items to: " <<'\n'; //Asks for the name
-
cin.getline(name, 40); //Accepts the name
-
cout <<"Please enter the street address you are shipping to: "<<'\n'; //Asks for the street address
-
cin.getline(street_address, 40); //Accepts the street address
-
cout <<"Please enter the city you are shipping to: "<<'\n'; //Asks for the city
-
cin.getline(city, 20); //Accepts the city
-
cout <<"Please enter the two letter abbreviation of the state you are shipping to: "<<'\n'; //Asks for the state
-
cin >> state; //Accepts the state
-
cout <<"Please enter the five digit zip code you are shipping to: "<<'\n'; //Asks for the zip code
-
cin >> zip; //Accepts the zip code
-
cout <<"How many boxes are you shipping? "<<'\n'; //Asks for number of boxes
-
cin >> number_of_boxes; //Accepts number of boxes
-
for(box_number=1;box_number <=number_of_boxes;box_number ++)
-
{
-
mailing_labels.setf(ios::left);
-
mailing_labels<<setw(40)<<name<<'\n'; //Prints the Name
-
mailing_labels<<setw(40)<<street_address<<'\n'; //Prints the street address
-
mailing_labels<<setw(25)<<city<<setw(2)<<", "<<setw(2)<<state<<setw(2)<<", "<<setw(5)<<zip<<'\n'; //Prints the city,state, and zip
-
mailing_labels<<setw(4)<<"Box "<<setw(2)<<box_number<<setw(4)<<" of "<<setw(2)<<number_of_boxes<<'\n'<<'\n'; //Box # of #
-
}
-
mailing_labels.close(); //Closes the file
-
-
system("PAUSE");
-
return 0;
-
}
I figured it out. It was suppose to be mailing_labels.setf(ios::left);. I have the bulk of the program working. The only thing I am having trouble with is getting it to print in two columns. Like
BOX 1 BOX 2
BOX 3 BOX 4
BOX 5
Something is still wrong. I tried adding an IF statement and it kind of works but it places the Labels on seperate lines and barely moves the label. The layout is suppose to be like the page is broken in half. I can't figure out what is wrong. - #include <iostream.h>
-
#include <stdlib.h>
-
#include <string>
-
#include <fstream>
-
#include <iomanip>
-
-
using namespace std;
-
char name[40]; // Is the name for the mailing labels
-
char street_address[40]; // Is the address for the mailing labels
-
string zip; // Is the zipcode for the mailing labels
-
string state; // Is the state for the mailing labels
-
char city[20]; // Is the city for the mailing labels
-
int number_of_boxes; // Number of labels neeeded
-
int box_number; //Current Label
-
ofstream mailing_labels; //Defines outfile
-
-
int main()
-
{
-
mailing_labels.open("C:\\Documents and Settings\\All Users\\Desktop\\mailing_labels.dat"); //Opens the file
-
mailing_labels<<setw(40)<<" "<<setw(40)<<" "<<'\n'<<'\n'; //Breaks the page into two columns //Accepts number of boxes
-
cout <<"Please enter the name of the person you are shipping these items to: " <<'\n'; //Asks for the name
-
cin.getline(name, 40); //Accepts the name
-
cout <<"Please enter the street address you are shipping to: "<<'\n'; //Asks for the street address
-
cin.getline(street_address, 40); //Accepts the street address
-
cout <<"Please enter the city you are shipping to: "<<'\n'; //Asks for the city
-
cin.getline(city, 20); //Accepts the city
-
cout <<"Please enter the two letter abbreviation of the state you are shipping to: "<<'\n'; //Asks for the state
-
cin >> state; //Accepts the state
-
cout <<"Please enter the five digit zip code you are shipping to: "<<'\n'; //Asks for the zip code
-
cin >> zip; //Accepts the zip code
-
cout <<"How many boxes are you shipping? "<<'\n'; //Asks for number of boxes
-
cin >> number_of_boxes; //Accepts number of boxes
-
for(box_number=1;box_number <=number_of_boxes;box_number ++) //Loop
-
{
-
-
if(box_number % 2 > 0)
-
mailing_labels.setf(ios::left);
-
if(box_number % 2 < 1)
-
mailing_labels.setf(ios::right);
-
mailing_labels<<setw(40)<<name<<'\n'; //Prints the Name
-
mailing_labels<<setw(40)<<street_address<<'\n'; //Prints the street address
-
mailing_labels<<setw(25)<<city<<setw(2)<<", "<<setw(2)<<state<<setw(2)<<", "<<setw(5)<<zip<<'\n'; //Prints the city,state, and zip
-
mailing_labels<<setw(4)<<"Box "<<setw(2)<<box_number<<setw(4)<<" of "<<setw(2)<<number_of_boxes<<'\n'<<'\n'; //Box # of #
-
mailing_labels.unsetf(ios::left);
-
mailing_labels.unsetf(ios::right);
-
}
-
mailing_labels.close(); //Closes the file
-
-
system("PAUSE");
-
return 0;
-
}
-
}
Figured it out. Done with the assignment. Thanks for your help everyone. :)
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Bob |
last post by:
Everybody,
I've been doing a lot of on-line research and cannot find
any reference to the exact problem I'm having.
Let me preface this question with the fact that I'm coming
from an Oracle...
|
by: Danny Anderson |
last post by:
Hola, C++ folk!
I want to have a do...while loop that works with a different file each
iteration, prompting the user for the file name to open and write.
The problem I am having is that the...
|
by: tram |
last post by:
How do we pass on the step1 output file to step2 of the same job? I
would like to know the current job's output file programmetically,
instead of hard coding it. Any idaes would be appreciated.
...
|
by: Mountain Bikn' Guy |
last post by:
I am having serious problems with the following IDE bug:
Could not write to output file 'x.dll' -- 'The process cannot access the
file because it is being used by another process. '
and
BUG:...
|
by: TJ |
last post by:
I am very new to C# (this is my first real project), therefore please be
patient if my question is considered being to newbie.
I am modifying a program which takes a text file, does some...
| |
by: undshan |
last post by:
I am writing a code that needs to open a file, create an output file, run through my function, prints the results to the output file, and closes them within a loop. Here is my code:
#include...
|
by: smashzone |
last post by:
Hi all,
Can anyone help me with a script to produce an output file of a "df -m" command with ";" seperators. I assume its very easy but I know nothing about Linux/Unix (I'm a newbie)
That is: ...
|
by: jebbyleezer |
last post by:
Hello, I have source code that builds correctly, however, after the program terminates the output file produced is empty.
Here is my source code:
import java.io.*;
import java.util.Scanner;...
|
by: dwaterpolo |
last post by:
Hi Everyone,
I am trying to read two text files swY40p10t3ctw45.col.txt and solution.txt
and compare them, the first text file has a bunch of values listed like:
y
y
y
y
y
y
y
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |