issues

i got an assignment to make a program that makes webpages. i need help with getline and ordering of steps. here is my code, i cant find out how it get stuck in some loops and the getline function is not working properly.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include <iostream>
#include <fstream>
#include <string> 
#include <exception> 

class htmlfile{
private:
    std::string filename;
    std::ofstream savefile; 
    bool ishead; 
public:
    
    bool runprogram(){
        ishead = false;
        char eord; 
        savefile.exceptions(std::ofstream::failbit | std::ofstream::badbit);
        std::cout << "Enter a title for your webpage: ";
        getline(std::cin, filename); 
        std::string hold; 
        hold = filename;
        filename = "/Users/home/Desktop/dreamweaver/dreamweaver/"; 
        filename += hold; 
        filename += ".html"; 
        try{
            savefile.open(filename.c_str());
        }
        catch (std::ofstream::failure &e){
            std::cout << "An error has occured";
            return false; 
        }
        savefile << "<html><head><title>" << hold << "</title>"; 
        std::cout << '\n' << "What color would you the backgroud (G)rey, (C)arolina blue, Wolfpack (R)ed, or Duke (B)lue: ";
        std::cin >> eord; 
        savefile << backcolor(eord); 
        while ( eord != 'Q'){
        std::cout << "Add an element to your web page. Select an action: (T)ext, (H)orizonal Rule, (Q)uit: ";
        std::cin >> eord;
            switch (eord){
            case 'T':
                    savefile << paragraph();
                    break; 
            case 't':
                    savefile << paragraph();
                    break; 
            case 'H':
                    savefile << horozonal(); 
                    break; 
            case 'h':
                    savefile << horozonal();
                    break;
            case 'Q':
                    savefile << ender();
                    return true; 
            case 'q':
                    savefile << ender(); 
                    return true;
                default:
                    std::cout << "entered in wrong text";
                    break; 
            }
        }
        return true; 
    }
     
    std::string horozonal(){
        std::string returnR = "<hr />";
        return returnR; 
    }
    
    std::string ender(){
        std::string returnR = "</body></html>";
        return returnR; 
    }
    
    std::string paragraph(){
        std::string returnR, a;
        char b;
        std::cout << "is this text a (H)eading, or a (P)aragraph text? ";
        std::cin >> b;
        switch (b) {
           case 'H':
                if(ishead == false){
                std::cout << "What styles would you like to apply to this text? (select all that apply) (B)old, (C)enter, change (F)ont, (N)one: ";
                std::cin >> a; 
                returnR += fontstyle(a); 
                    return returnR;
                    ishead =true; 
                }
                break; 
            case 'h':
                if(ishead == false){
                    std::cout << "What styles would you like to apply to this text? (select all that apply) (B)old, (C)enter, change (F)ont, (N)one: ";
                    std::cin >> a; 
                    returnR += fontstyle(a); 
                    ishead = true; 
                    return returnR;
                }
                break;
           case 'P':
                std::cout << ""; 
                returnR += fontstyle(a); 
                returnR += paradd(); 
                return returnR;
            case 'p':
                std::cout << ""; 
                returnR += fontstyle(a); 
                returnR += paradd(); 
                return returnR;
                
        }
        return returnR; 
    }
    
    std::string paradd(){
        char a; 
        std::string returnR; 
        std::cout << "what would you like to do: add a (L)ink, add a line (B)reak, add more (T)ext, (E)nd the paragraph: "; 
        std::cin >> a; 
        while ( a != 'e' || 'E'){
        if(a == 'l' || 'L') returnR += link();
        if(a == 'b' || 'B') returnR += "<br />";
        if(a == 't' || 'T') returnR += text(); 
        if(a == 'e' || 'E') returnR += "</p>"; 
        } 
        return returnR; 
    }
    
    std::string link(){
        std::string returnR = "<a href=\"", a; 
        std::cout << "enter the url: ";
        getline(std::cin, a); 
        returnR += a;
        returnR += "\">";
        std::cout << "Enter the link text to be displayed: ";
        getline(std::cin, a);
        returnR += a;
        returnR += "<\a>";
        return returnR; 
    }
     
    std::string fontstyle(std::string a){
        std::string returnR = "<body><h1 style=\"font-weight:";
        for(int i = 0; i < a.length(); i++){
            if(a[i] == 'b' || 'B') returnR+=" bold;";
            if(a[i] == 'C' || 'c') returnR+=" text-align: center;";
            if(a[i] == 'f' || 'F') returnR+= setfont(); 
        }
        text(); 
        returnR += "</h1>"; 
            return returnR; 
    }
    
    std::string setfont(){
        int font; 
        std::string returnR = "font-family: "; 
        std::cout << "what font size: ";
        std::cin >> font; 
        returnR += font;
        returnR += ";";
        return returnR; 
    }
    
    std::string text(){
        std::string returnR = "font-family: ", words; 
        char a; 
        std::cout << "Which font would you like to choose? (A)rial, (T)imes New Roman, (I)mpact, (C)omic Sans MS : ";
        std::cin >> a; 
        switch (a){
                case 'A':
                returnR += "arial;"; 
                break;  
                case 'T':
                returnR += "times new roman;";
                break; 
                case 'I':
                returnR += "impact;";
                break;
                case 'C':
                returnR += "comic sans ms;"; 
                break;
            case 'a':
                returnR += "arial;"; 
                break;  
            case 't':
                returnR += "times new roman;";
                break; 
            case 'i':
                returnR += "impact;";
                break;
            case 'c':
                returnR += "comic sans ms;"; 
                break;
        }
        std::cout << "Enter your text: ";
        getline(std::cin, words);  
        returnR += "\">"; 
        returnR += words; 
        return returnR; 
    }
    
    std::string backcolor(char a){
        switch(a){
            case 'G':
                return "<style type = \"text/css\">body {background-color: #B22222; color: #999999;</style></head>"; 
            case 'C':
               return "<style type = \"text/css\">body {background-color: #B22222; color: #99CCFF;</style></head>"; 
            case 'R':
                return "<style type = \"text/css\">body {background-color: #B22222; color: #FF0000;</style></head>"; 
            case 'B':
                return "<style type = \"text/css\">body {background-color: #B22222; color: #0000CC;</style></head>"; 
            case 'g':
                return "<style type = \"text/css\">body {background-color: #B22222; color: #999999;</style></head>"; 
            case 'c':
                return "<style type = \"text/css\">body {background-color: #B22222; color: #99CCFF;</style></head>"; 
            case 'r':
                return "<style type = \"text/css\">body {background-color: #B22222; color: #FF0000;</style></head>"; 
            case 'b':
                return "<style type = \"text/css\">body {background-color: #B22222; color: #0000CC;</style></head>"; 
            default:
                std::cout << "you entered an invalid color\n please enter a color: ";
                std::cin >> a; 
                backcolor(a);
        }
        return "t(0.0t)"; 
    }
};

int main(){
    htmlfile active_file;
    active_file.runprogram(); 
    return 0;
}
the getline function is not working properly.

what exactly is happning with it .. what are you getting when you input the string .
the first getline work fine, all subsequent ones skip the getline function and continue with the program as if it was not there.
Enter a title for your webpage: hello

What color would you the backgroud (G)rey, (C)arolina blue, Wolfpack (R)ed, or Duke (B)lue: g
Add an element to your web page. Select an action: (T)ext, (H)orizonal Rule, (Q)uit: t
is this text a (H)eading, or a (P)aragraph text? 

p
Which font would you like to choose? (A)rial, (T)imes New Roman, (I)mpact, (C)omic Sans MS : a
Enter your text: what would you like to do: add a (L)ink, add a line (B)reak, add more (T)ext, (E)nd the paragraph: 

that is the output the error is after
Enter your text: 
it is suppose to wait until a line is entered but continues for some reason.
Last edited on
Just to add:
if(character == 'a' || 'A') // or similar
does not work the way you think it does, you can't skip out of some of the comparisons.
so if i do
if(variable == thingy) do stuff;
if(variable ==tolower(thingy)) do same stuff;
could i just do a tolower or toupper function in ctype.h and it would work?
@OP you should come up with a variable naming convention for yourself. It will help you edit your code significantly. For example you could associate a prefix of the variable name to it's type.

like int = i, pointer = p, double = d, string = str, c-string = cstr, char = c, char array = pc etc....

1
2
3
int iInput = 0;
string strFileName;
char* pcBuffer;
ok i used tolower for the if and switch statements, it did not fix the problem, my issue is in this function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
std::string text(){
        std::string returnR = "font-family: ", words; 
        char a; 
        std::cout << "Which font would you like to choose? (A)rial, (T)imes New Roman, (I)mpact, (C)omic Sans MS : ";
        std::cin >> a; 
        switch (a){
                case 'A':
                returnR += "arial;"; 
                break;  
                case 'T':
                returnR += "times new roman;";
                break; 
                case 'I':
                returnR += "impact;";
                break;
                case 'C':
                returnR += "comic sans ms;"; 
                break;
            case 'a':
                returnR += "arial;"; 
                break;  
            case 't':
                returnR += "times new roman;";
                break; 
            case 'i':
                returnR += "impact;";
                break;
            case 'c':
                returnR += "comic sans ms;"; 
                break;
        }
        std::cout << "Enter your text: ";
        getline(std::cin, words);  
        returnR += "\">"; 
        returnR += words; 
        return returnR; 
    }

the line getline(std::cin, words); is never run, it skips this step and just returns returnR. i am not sure why this happens, could someone explain why and/or tell me how to get it to work.
Which compiler are you using? I have seen that issue on a borland compiler but not on gcc, if that is what you are using.

The work around is put another cout before the getline. If it is the symptom I think it is that cout shouldn't execute but the getline will.
Last edited on
In your post above Azagaros',
add std::cin.sync() just before the getline
Have fun figuring out how that solves the problem.
The problem is that std::cin >> a; will read the first non-whitespace character and then stop. This leaves a newline character at the end of the line. When you later call getline it will read until it finds a new line character. In your case it finds it right away so the the string will be empty.

To solve this you can call std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); before all calls to getline that are preceded by std::cin >> .... What this does is that it skips all the characters that is left on the line so that the next input can start from a new fresh line.

Don't use sync(). It's not guaranteed to work.
Last edited on
@Peter87
Why isn't sync guranteed to work?
It's not guaranteed to do anything. On GCC it doesn't discard any input or anything.
Last edited on
Are you sure Peter87? I use MinGW, and I'm sure that it does what it is meant to do (at least in my system). Isn't MinGW very much like GCC ?
I'm sure. I don't know why MinGW differs from GCC.
Interesting, according to this page ( http://www.cplusplus.com/reference/iostream/istream/sync/ ) that is what sync is designed to do. Unless I'm misunderstanding it. Sounds like MinG is doing it right and GCC isn't according to just that page. Either that, or cpluplus.com is wrong, one of the two.
Last edited on by closed account z6A9GNh0
cplusplus.com is misleading.

Someone reported this "bug". Can be interesting to read. http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46624
cplusplus.com has always been a poor reference

Ouch!
Well, there maybe that bug in GCC, but MinGW just works fine.
cplusplus.com is misleading

Then the creator of the language must be misleading too. His book says it "clears buffer (flush)"
i got it working, i used cin.ignore(), i am using xcode if you are wondering, the problem is that the html file is not getting any of the code from paragraph() function, can u guys help me with that.
Topic archived. No new replies allowed.