C++ Newbie / Output File Layout/ For Loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Prosdo
    New Member
    • Nov 2007
    • 32

    C++ Newbie / Output File Layout/ For Loop

    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.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    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?

    Comment

    • beacon
      Contributor
      • Aug 2007
      • 579

      #3
      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:
      Code:
      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:
      Code:
      for(int i=0; i<size; i++)
      or
      Code:
      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.

      Comment

      • Prosdo
        New Member
        • Nov 2007
        • 32

        #4
        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?

        Comment

        • oler1s
          Recognized Expert Contributor
          • Aug 2007
          • 671

          #5
          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?

          Comment

          • oler1s
            Recognized Expert Contributor
            • Aug 2007
            • 671

            #6
            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.

            Comment

            • beacon
              Contributor
              • Aug 2007
              • 579

              #7
              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....

              Comment

              • Prosdo
                New Member
                • Nov 2007
                • 32

                #8
                I know I can't post the whole code so can I send it to one you guys?

                Comment

                • beacon
                  Contributor
                  • Aug 2007
                  • 579

                  #9
                  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.

                  Comment

                  • Prosdo
                    New Member
                    • Nov 2007
                    • 32

                    #10
                    Code:
                    #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\ste pha~1\desktop\m ailin~1.cpp: In function `int main()':
                    c:\docume~1\ste pha~1\desktop\m ailin~1.cpp:20: implicit declaration of function `int setw(...)'
                    c:\docume~1\ste pha~1\desktop\m ailin~1.cpp:36: parse error before `||'

                    Comment

                    • Prosdo
                      New Member
                      • Nov 2007
                      • 32

                      #11
                      I fixed the one error. I forgot to insert #include <iomanip>.

                      Comment

                      • Ganon11
                        Recognized Expert Specialist
                        • Oct 2006
                        • 3651

                        #12
                        You might mean to say setf(ios::left) ;

                        The || is the logical OR operator.

                        Comment

                        • Prosdo
                          New Member
                          • Nov 2007
                          • 32

                          #13
                          Thanks Gannon.
                          I'm still getting errors though.
                          c:\docume~1\ste pha~1\desktop\m ailin~1.cpp: In function `int main()':
                          c:\docume~1\ste pha~1\desktop\m ailin~1.cpp:39: implicit declaration of function `int setf(...)'

                          Comment

                          • Ganon11
                            Recognized Expert Specialist
                            • Oct 2006
                            • 3651

                            #14
                            Isn't setf in iomanip.h? Try adding:

                            [CODE=cpp]#include <iomanip>[/CODE]

                            Comment

                            • Prosdo
                              New Member
                              • Nov 2007
                              • 32

                              #15
                              Originally posted by Ganon11
                              Isn't setf in iomanip.h? Try adding:

                              [CODE=cpp]#include <iomanip>[/CODE]
                              Still getting the same error.

                              Comment

                              Working...