473,473 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

C++ Newbie / Output File Layout/ For Loop

32 New Member
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.
Nov 10 '07 #1
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?
Nov 10 '07 #2
beacon
579 Contributor
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:
Expand|Select|Wrap|Line Numbers
  1. for(initialization; test; update){
  2.           statement; 
  3.           statement;
  4.           }
  5.  
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:
Expand|Select|Wrap|Line Numbers
  1. for(int i=0; i<size; i++)
or
Expand|Select|Wrap|Line Numbers
  1. 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.
Nov 11 '07 #3
Prosdo
32 New Member
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?
Nov 11 '07 #4
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?
Nov 11 '07 #5
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.
Nov 11 '07 #6
beacon
579 Contributor
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....
Nov 11 '07 #7
Prosdo
32 New Member
I know I can't post the whole code so can I send it to one you guys?
Nov 11 '07 #8
beacon
579 Contributor
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.
Nov 11 '07 #9
Prosdo
32 New Member
Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <stdlib.h>
  3. #include <cstring>
  4. #include <string>
  5. #include <fstream>
  6.  
  7. using namespace std;
  8. string name;  // Is the name for the mailing labels
  9. string street_address; // Is the address for the mailing labels
  10. string zip;  // Is the zipcode for the mailing labels
  11. string state; // Is the state for the mailing labels
  12. string city; // Is the city for the mailing labels
  13. int number_of_boxes; // Number of labels neeeded
  14. int box_number; //Current Label
  15. ofstream mailing_labels;
  16.  
  17. int main()
  18. {
  19. mailing_labels.open("D:\\mailing_labels.dat"); //Opens the file
  20. mailing_labels<<setw(40)<<" "<<setw(40)<<" "<<'\n'<<'\n'; //Breaks the page into two columns                                                                       //Accepts number of boxes
  21.  
  22. for(box_number=1;box_number ==number_of_boxes;box_number ++)
  23. {
  24. cout <<"Please enter the name of the person you are shipping these items to: " <<'\n';  //Asks for the name
  25. cin >> name;                                                                          //Accepts the name
  26. cout <<"Please enter the street address you are shipping to: "<<'\n';                  //Asks for the street address
  27. cin >> street_address;                                                                       //Accepts the street address
  28. cout <<"Please enter the city you are shipping to: "<<'\n';                            //Asks for the city
  29. cin >> city;                                                                            //Accepts the city
  30. cout <<"Please enter the two letter abbreviation of the state you are shipping to: "<<'\n';         //Asks for the state
  31. cin >> state;                                                                                        //Accepts the state
  32. cout <<"Please enter the five digit zip code you are shipping to: "<<'\n';                        //Asks for the zip code
  33. cin >> zip;                                                                                          //Accepts the zip code
  34. cout <<"How many boxes are you shipping? "<<'\n';                                                   //Asks for number of boxes
  35. cin >> number_of_boxes;   
  36. setf(ios||left);
  37. mailing_labels<<setw(40)<<name<<'\n';
  38. mailing_labels<<setw(40)<<street_address<<'\n';
  39. mailing_labels<<setw(33)<<city<<setw(2)<<state<<setw(5)<<zip<<'\n';
  40. mailing_labels<<setw(4)<<"Box "<<setw(2)<<box_number<<setw(4)<<" of "<<setw(2)<<number_of_boxes<<'\n'<<'\n';
  41. }
  42. mailing_labels.close();
  43.  
  44.       system("PAUSE");
  45.       return 0;
  46. }
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 `||'
Nov 11 '07 #10
Prosdo
32 New Member
I fixed the one error. I forgot to insert #include <iomanip>.
Nov 11 '07 #11
Ganon11
3,652 Recognized Expert Specialist
You might mean to say setf(ios::left);

The || is the logical OR operator.
Nov 11 '07 #12
Prosdo
32 New Member
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(...)'
Nov 12 '07 #13
Ganon11
3,652 Recognized Expert Specialist
Isn't setf in iomanip.h? Try adding:

Expand|Select|Wrap|Line Numbers
  1. #include <iomanip>
Nov 12 '07 #14
Prosdo
32 New Member
Isn't setf in iomanip.h? Try adding:

Expand|Select|Wrap|Line Numbers
  1. #include <iomanip>
Still getting the same error.
Nov 12 '07 #15
Prosdo
32 New Member
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

Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <stdlib.h>
  3. #include <string>
  4. #include <fstream>
  5. #include <iomanip>
  6.  
  7. using namespace std;
  8. char name[40];                     // Is the name for the mailing labels
  9. char street_address[40];          // Is the address for the mailing labels
  10. string zip;                      // Is the zipcode for the mailing labels
  11. string state;                   // Is the state for the mailing labels
  12. char city[20];                 // Is the city for the mailing labels
  13. int number_of_boxes;          // Number of labels neeeded
  14. int box_number;              //Current Label
  15. ofstream mailing_labels;
  16.  
  17. int main()
  18. {
  19. mailing_labels.open("C:\\Documents and Settings\\All Users\\Desktop\\mailing_labels.dat"); //Opens the file
  20. mailing_labels<<setw(40)<<" "<<setw(40)<<" "<<'\n'<<'\n';                                //Breaks the page into two columns                                                                       //Accepts number of boxes
  21. cout <<"Please enter the name of the person you are shipping these items to: " <<'\n';  //Asks for the name
  22. cin.getline(name, 40);                                                                  //Accepts the name
  23. cout <<"Please enter the street address you are shipping to: "<<'\n';                  //Asks for the street address
  24. cin.getline(street_address, 40);                                                        //Accepts the street address
  25. cout <<"Please enter the city you are shipping to: "<<'\n';                            //Asks for the city
  26. cin.getline(city, 20);                                                                            //Accepts the city
  27. cout <<"Please enter the two letter abbreviation of the state you are shipping to: "<<'\n';         //Asks for the state
  28. cin >> state;                                                                                        //Accepts the state
  29. cout <<"Please enter the five digit zip code you are shipping to: "<<'\n';                        //Asks for the zip code
  30. cin >> zip;                                                                                          //Accepts the zip code
  31. cout <<"How many boxes are you shipping? "<<'\n';                                                   //Asks for number of boxes
  32. cin >> number_of_boxes;                                                                             //Accepts number of boxes
  33. for(box_number=1;box_number <=number_of_boxes;box_number ++)
  34. {
  35. mailing_labels.setf(ios::left);
  36. mailing_labels<<setw(40)<<name<<'\n';        //Prints the Name
  37. mailing_labels<<setw(40)<<street_address<<'\n';     //Prints the street address
  38. mailing_labels<<setw(25)<<city<<setw(2)<<", "<<setw(2)<<state<<setw(2)<<", "<<setw(5)<<zip<<'\n';  //Prints the city,state, and zip
  39. mailing_labels<<setw(4)<<"Box "<<setw(2)<<box_number<<setw(4)<<" of "<<setw(2)<<number_of_boxes<<'\n'<<'\n';   //Box # of #
  40. }
  41. mailing_labels.close();   //Closes the file
  42.  
  43.       system("PAUSE");
  44.       return 0;
  45. }
Nov 12 '07 #16
Prosdo
32 New Member
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.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <stdlib.h>
  3. #include <string>
  4. #include <fstream>
  5. #include <iomanip>
  6.  
  7. using namespace std;
  8. char name[40];                  // Is the name for the mailing labels
  9. char street_address[40];        // Is the address for the mailing labels
  10. string zip;                     // Is the zipcode for the mailing labels
  11. string state;                   // Is the state for the mailing labels
  12. char city[20];                  // Is the city for the mailing labels
  13. int number_of_boxes;            // Number of labels neeeded
  14. int box_number;                 //Current Label
  15. ofstream mailing_labels;        //Defines outfile
  16.  
  17. int main()
  18. {
  19. mailing_labels.open("C:\\Documents and Settings\\All Users\\Desktop\\mailing_labels.dat");            //Opens the file
  20. mailing_labels<<setw(40)<<" "<<setw(40)<<" "<<'\n'<<'\n';                                             //Breaks the page into two columns                                                                       //Accepts number of boxes
  21. cout <<"Please enter the name of the person you are shipping these items to: " <<'\n';                //Asks for the name
  22. cin.getline(name, 40);                                                                                //Accepts the name
  23. cout <<"Please enter the street address you are shipping to: "<<'\n';                                 //Asks for the street address
  24. cin.getline(street_address, 40);                                                                      //Accepts the street address
  25. cout <<"Please enter the city you are shipping to: "<<'\n';                                           //Asks for the city
  26. cin.getline(city, 20);                                                                                //Accepts the city
  27. cout <<"Please enter the two letter abbreviation of the state you are shipping to: "<<'\n';           //Asks for the state
  28. cin >> state;                                                                                         //Accepts the state
  29. cout <<"Please enter the five digit zip code you are shipping to: "<<'\n';                            //Asks for the zip code
  30. cin >> zip;                                                                                           //Accepts the zip code
  31. cout <<"How many boxes are you shipping? "<<'\n';                                                     //Asks for number of boxes
  32. cin >> number_of_boxes;                                                                               //Accepts number of boxes
  33. for(box_number=1;box_number <=number_of_boxes;box_number ++)                                          //Loop
  34. {
  35.  
  36. if(box_number % 2 > 0)
  37. mailing_labels.setf(ios::left);
  38. if(box_number % 2 < 1)
  39. mailing_labels.setf(ios::right);
  40. mailing_labels<<setw(40)<<name<<'\n';                                                                 //Prints the Name
  41. mailing_labels<<setw(40)<<street_address<<'\n';                                                       //Prints the street address
  42. mailing_labels<<setw(25)<<city<<setw(2)<<", "<<setw(2)<<state<<setw(2)<<", "<<setw(5)<<zip<<'\n';     //Prints the city,state, and zip
  43. mailing_labels<<setw(4)<<"Box "<<setw(2)<<box_number<<setw(4)<<" of "<<setw(2)<<number_of_boxes<<'\n'<<'\n';   //Box # of #
  44. mailing_labels.unsetf(ios::left);
  45. mailing_labels.unsetf(ios::right);
  46. }
  47. mailing_labels.close();                                                                               //Closes the file
  48.  
  49.       system("PAUSE");
  50.       return 0;
  51. }
  52. }
Nov 12 '07 #17
Prosdo
32 New Member
Figured it out. Done with the assignment. Thanks for your help everyone. :)
Nov 12 '07 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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...
0
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...
10
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. ...
4
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:...
1
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...
3
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...
4
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: ...
0
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;...
1
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
0
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...
0
Oralloy
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,...
0
jinu1996
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...
0
tracyyun
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...
0
agi2029
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,...
1
isladogs
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...
0
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...
0
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 ...
0
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.