我编写了一个我自己创建的程序,用于定义的自动格式化。这可能毫无意义,但这是一个挑战,因为我回到了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;
}
答案 0 :(得分:2)
你想写下你想要循环的逻辑:
i&lt; = 10
或
szWord不是空的。
( i<=10 ) || ! szWord.empty()
这是经典的布尔逻辑反演。与(!A&amp;&amp;!B)相反的是(A || B)