Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 输入并断开C++;for循环:最少10次迭代,并且没有空字符串_C++_Loops_For Loop - Fatal编程技术网

C++ 输入并断开C++;for循环:最少10次迭代,并且没有空字符串

C++ 输入并断开C++;for循环:最少10次迭代,并且没有空字符串,c++,loops,for-loop,C++,Loops,For Loop,我已经编写了一个程序,主要是为定义的自动格式化而创建的。这可能毫无意义,但这是一个挑战,因为我回到了C++。 我有一个循环,每次迭代都会添加一个列表项;但是,我想这样做,您必须至少有10次迭代,并且szWord必须为空才能终止循环。此外,如果有可能有一个多行字符串,不同于我是如何做到的,那么请张贴 以下是循环(我尝试了a>和a *SZWord!=‘0’< /CODE。自从上次我用C++解决了几年。你的解决方案是什么?我是如何使用指针?= j.jyyJymiah。当我尝试运行你的时候,我得到了一个

我已经编写了一个程序,主要是为定义的自动格式化而创建的。这可能毫无意义,但这是一个挑战,因为我回到了C++。

我有一个循环,每次迭代都会添加一个列表项;但是,我想这样做,您必须至少有10次迭代,并且szWord必须为空才能终止循环。此外,如果有可能有一个多行字符串,不同于我是如何做到的,那么请张贴

以下是循环(我尝试了a>和a<,我理解为什么两者都失败了,但我不确定如何解决这个问题):


for(int i=1;i您希望在希望循环继续时写入的逻辑:


要么我只有9次迭代。对。我需要将我设置为0。但是,我仍然可以给出一个空字符串,因为我仍然小于10,所以我退出循环。如果我的符号颠倒了,并且I=1,那么我太小,无法进入循环。你不想要
szWord!=“”比较指针和SZWord永远不会指向硬编码字符串。你可能想要“代码> *SZWord!=‘0’< /CODE。自从上次我用C++解决了几年。你的解决方案是什么?我是如何使用指针?= j.jyyJymiah。当我尝试运行你的时候,我得到了一个与操作符错误不匹配的匹配。代码。@Crysis:我认为您想要的终止条件是(最多10次迭代)或(输入字符串为空)。抱歉,说它不起作用。lol。当我这么说的时候,我甚至还没有试过。后来我的大脑让我失望了。我的傲慢。我不明白的是:如果迭代不到10次,szWord仍然是空的,为什么它不终止?如果我理解你最初的定义,你想在超过10个循环后停止,只有
szWord
是空的。所以上面的逻辑只有一半必须为真才能继续。而
i10是否需要查看
szWord
哦,好吧。我认为,因为它是OR运算符,一旦一个参数为假,它就会中断。(我需要检查我的逻辑规则)
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; 
}
( i<=10 ) || ! szWord.empty()