Andrue
Andrue

Reputation: 707

Enter and Break C++ for loop: minimum of 10 iterations AND no empty string

I've written a program that I manly created for automated formatting for definitions. It may be pointless, but it was a challenge because I'm getting back into C++.

I have a loop that adds a list item every iteration; however, I want to make it so you have to have a minimum of 10 iterations AND szWord has to be empty to terminate the loop. Also, if it's possible to have a multi-line string, different than how I did it, then please post.

Here's the loop (I've tried with both a > and a < and I understand why both fail, but I'm not sure how to fix this):

for(int i =1; i<10 && szWord != ""; i++){
    cout <<"Word: ";
    getline(cin, szWord); //Get intended word(s)
    cout << "\n Definition: ";
    getline(cin, szDefinition); //get intended defintion
    szList_Item = "<li><em>" + szWord + "-</em> " + szDefinition + "</li>";//Concates and makes HTML li tag
    ofstream Definition_HTML; //Ofstream Declaration
    Definition_HTML.open(szFile_Name, ios::app); //Adds to end of text to prevent rewriting; 

    Definition_HTML << szList_Item; //inserts szList_Item
    Definition_HTML.close(); //close
}

Entire Code:

#include <iostream> //cin, cout
#include <stdlib.h>
#include <stdio.h>
#include <cstdlib>
#include <cstdio>
#include <fstream>
#include <string> //getline(), string


using namespace std;
void HTML_Start(string); 
void HTML_End(string);

void HTML_Start(string Class, string Date, string File_Name, string Name, string Title){
    string szCSS = "<style>"
        "html {"
        "background-color: white;"
        "padding: 10px;"
        "font-family: sans-serif;"
        "font-size: 15px;"          "}"             "body {"
        "background-color: #f6f6f6;"
        "box-shadow: 0 2px 8px rgba(0,0,0,.5);"
        "display: table;"
        "padding: 0;"
        "margin: 0;"
        "position: relative;"
        "z-index: 0;"
        "width: 100%;"          "}"             "address {"
        "position: fixed;"
        "top: 5%;"
        "right: 1%;"
        "text-align: right;"
        "font-size: smaller;"
        "font-style: normal;"           "}"             "h1 {"
        "position: absolute;"
        "left: 120px;"
        "top: 40px;"
        "font-size: larger;"
        "font-weight: normal;"
        "text-decoration: underline;"           "}"             "ol {"

        "border-left: 2px solid rgba(255,0,30,.25);"
        "border-right: 2px solid rgba(255,0,30,.05);"
        "padding: 0;"
        "margin: 0;"
        "margin-left: 100px;"
        "margin-right: 80px;"
        "position: relative;"
        "z-index: 0;"
        "float: left;"
        "width: 80%;"           "}"             "ol li {"
        "padding: 0;"
        "margin-left: -104px;"
        "margin-right: -81px;"
        "padding-left: 110px;"
        "padding-right: 100%;"
        "border-bottom: 2px solid rgba(0,160,255,.1);"
        "line-height: 30px;"
        "height: 30px;"
        "width:100%;"           "}"             "ol li:first-child {"
        "border-top: 2px solid rgba(0,160,255,.1);"
        "margin-top: 120px;"            "}"             "ol li:last-child {"
        "margin-bottom: 50px;"          "}"             "ol::after {"
        "position: absolute;"
        "bottom: 50px;"
        "right: -65px;"
        "color: rgba(0,160,255,.16);"
        "line-height: 30px;"
        "font-weight: 400;"
        "font-family: \'Mrs Sheppards\', cursive;"
        "letter-spacing: 2px;"          "}"             "ol::before {"
        "content: \"\";"
        "background-color: white;"
        "box-shadow: 0 2px 4px rgba(0,0,0,.5) inset;"
        "height: 25px;"
        "width: 25px;"
        "border-radius: 25px;"
        "position: absolute;"
        "top: 105px;"
        "left: -75px;"          "}"             "body::before {"
        "content: "";"
        "background-color: white;"
        "box-shadow: 0 2px 4px rgba(0,0,0,.5) inset;"
        "height: 25px;"
        "width: 25px;"
        "border-radius: 25px;"
        "position: absolute;"
        "top: 50%;"
        "left: 27px;"           "}"             "body::after {"
        "content: "";"
        "background-color: white;"
        "box-shadow: 0 2px 4px rgba(0,0,0,.5) inset;"
        "height: 25px;"
        "width: 25px;"
        "border-radius: 25px;"
        "position: absolute;"
        "bottom: 105px;"
        "left: 27px;"           "}"

        ".container{"
        "width:auto;"
        "max-width: 100%;"
        "height:100%;"
        "overflow: hidden;"             "}"

        "em {"
        "font-weight: bold;"
        "font-style: oblique;"          "}"             "</style>";

    string szTitle = "<title>" + Title + "</title>";    ofstream Definition_HTML;
    Definition_HTML.open(File_Name, ios::app);
    Definition_HTML << "<!DOCTYPE html> <html> <head> <meta charset=\"UTF-8\">"
        << szTitle
        << szCSS
        << "</head> <body>"
        << "<div class=\"container\">"
        << "<address>" + Name + "<br>" + Class + "<br>" + Date + "</address>"
        << "<h1>" + Title + "</h1>"
        << "<ol>";
    Definition_HTML.close();

}

void HTML_End(string File_Name){
    ofstream Definition_HTML;
    Definition_HTML.open(File_Name, ios::app);
    Definition_HTML << "</ol></div></body></html>";
    Definition_HTML.close();

}

int main() {
    //Declare Variables
    string szClass = "";
    string szDate = "";
    string szDefinition = "";
    string szExtension = "Def.html";
    string szFile_Name = "Definitions.html";
    string szList_Item = "";
    string szName = "";
    string szTitle = "";
    string szWord = " ";

    //Start main function of program
    cout << "This is a program that automatically formats your definitions. \n It then proceeds to create a web page for easy viewing."
        "Hopefully, at some point, it will automatically get the definition for you and create a quiz."
        "\n\n";

    cout << "Name: ";
    getline(cin, szName); //Gets Name of User
    cout << "\n" << "Class and Period: ";
    getline(cin, szClass); //Gets Class
    cout << "\n"
        << "Press ENTER for today's date."
        << "\n"
        << "Date: ";
    getline(cin, szDate); //Gets date user inserts
    if (szDate == "")
    {
        szDate = "this_is_today's_date_place_holder"; //If user hits enter, then display current date
    }
    else { szDate = szDate; //Not sure if 'else' is required, but if so, it sets Date to itself
    }

    cout <<  "\n" << "Page Title: ";
    getline(cin, szTitle); // Gets the title and H4 of the page
    szFile_Name = szTitle + szExtension;
    HTML_Start(szClass, szDate, szFile_Name, szName, szTitle);  //set the name of file to the title and Def.html

    for(int i =1; i<10 && szWord != ""; i++){
        cout <<"Word: ";
        getline(cin, szWord); //Get intended word(s
        cout << "\n Definition: ";
        getline(cin, szDefinition); //get intended defintion
        szList_Item = "<li><em>" + szWord + "-</em> " + szDefinition + "</li>";//Concates and makes HTML li tag
        ofstream Definition_HTML; //Ofstream Declaration
        Definition_HTML.open(szFile_Name, ios::app); //Not sure what ios::app does; Sets the name of file to szFile_Name
        Definition_HTML << szList_Item; //inserts szList_Item
        Definition_HTML.close(); //close
    }
    HTML_End(szFile_Name);
    return 0; 
}

Upvotes: 0

Views: 299

Answers (1)

woolstar
woolstar

Reputation: 5083

You want to write the logic of when you want the loop to go on:

either i<=10

or

szWord is not empty.

( i<=10 ) || ! szWord.empty()

This is classic boolean logic inversion. The opposite of ( ! A && ! B ) is ( A || B )

Upvotes: 2

Related Questions