c++ - 输入和中断 C++ for 循环 : minimum of 10 iterations AND no empty string

标签 c++ loops for-loop

我自己编写了一个程序,用于自动格式化定义。这可能毫无意义,但它是一个挑战,因为我正在回到 C++ 中。

我有一个循环,每次迭代都会添加一个列表项;但是,我想做到这一点,因此您必须至少进行 10 次迭代,并且 szWord 必须为空才能终止循环。另外,如果可能有一个多行字符串,与我的做法不同,请发布。

这是循环(我已经尝试使用 > 和 < 并且我明白为什么都失败了,但我不确定如何解决这个问题):

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
}

完整代码:

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

最佳答案

你想写下循环何时继续的逻辑:

要么我<=10

szWord不为空

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

这是经典的 bool 逻辑反转。 ( !A && ! B ) 的反义词是 ( A || B )

关于c++ - 输入和中断 C++ for 循环 : minimum of 10 iterations AND no empty string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20462592/

相关文章:

c++ - 复制两个字符数组的模板函数

PHP - 创建自定义模板系统?

algorithm - 运行固定次数的循环是否被认为是 Big - Oh(1)?

java:为什么两个线程不并行执行

php - 每个结果的百分比,其中 id = id

c++ - std::regex_match 在传递右值参数时给出错误的匹配结果

c++ - 仅 boost dynamic_bitset header

c++ - 为特定时间快速搜索音符建模音乐(音符)的最佳方式

c++ - 数独递归,接受零为合法

c++ - 自动换行功能