dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: For Loop Issue and Output Layout Problem

Hybrid View

  1. #1
    Join Date
    Nov 2007
    Posts
    5

    Unhappy For Loop Issue and Output Layout Problem

    Hi everyone. I am very new to C++ and taking a class. (don't laugh too hard at me lol) I have a program due on Monday and I ran into a wall. The programs calls for us to make mailing labels. They must be in this format:

    Michael Smith
    555 Lincoln Way
    Philadelphia, PA 24211
    Box 1 of 5

    The odd labels must be on the left and the even on the right they should be sent to the output file in the layout

    LABEL1 LABEL 2
    LABEL 3 LABEL 4
    LABEL 5

    So I need them to enter the number of boxes and it should count up to the number they need. I figured for that I would need something like number = number + 1. We can use loops, but I am not to sure with those. Here is the code I have so far:

    Quote:
    #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 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");

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



    system("PAUSE");
    return 0;
    }
    My teacher said I would need the modulus operator to split the sides of the mailing list? Can anyone help? Also I wanted to use a for loop, but I am kind of confused on how to format it? Thanks guys. Any help would be SO greatly appreciated.

  2. #2
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    cout<<"ENTER NUMBER OF BOXES: ";
    cin>>bCount;
    for(...;i<bCount;i++){
    //and put the body here ...
    //write resived data to the file also at the end of each iteration .
    }
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  3. #3
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    it's very hard to return back and print in a colum next to the first one,
    So print twice boxes in each loop iteration ...
    like:
    out<<setw(40)<<address1<<setw(40)<<address2;
    out<< //left box then right box and so on
    and check if they r odd number to not print anything in the right at the last iteration or a null strings for example, i.e. if they r odd add another null values to be even. and of course the loop iteration numbers will be the half.

    hope this can helps...

    BTW: the loop shouldn't include the data entry from the user ?
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  4. #4
    Join Date
    Nov 2007
    Posts
    5
    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

    Code:
    #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;
    }

    It somewhat works but the labels don't move much. Something still is wrong.

Similar Threads

  1. Replies: 1
    Last Post: 04-24-2008, 06:03 AM
  2. Replies: 0
    Last Post: 11-28-2005, 01:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center