I really dont know what name this needs..

Hey, im currently programming a program which will create a html document with whatever text you desire, the only problem is, each time i want to give it bold and italic (for example) it creates the word i inputted and bolds it and then creates another one but this time with italic, for example :
What text would you like to enter ?
Hey
Enter a command : Bold
Enter a command : Italic
and it created it like this :
Hey Hey
sorry if its not so understanable...
Last edited on
Could you share the piece of code which is not working as expected?
1
2
3
4
5
6
7
8
if(commands == "hightext")
	{
		page << "<html>\n" << "<body>\n"  << "<p>" << "<sup>" << text << "</sup>"  <<  "</p>" << "</body>\n" << "</html>";
	}
else if(commands == "bold")
	{
		page << "<html>\n" << "<body>\n"  << "<p>" << "<b>" << text << "</b>"  <<  "</p>" << "</body>\n" << "</html>";
	}

i used if's and else if's because i dont think it is possible to use switch with a string, thanks in advance :P
Why not have another case where it is bold and italic?

You know how to nest those tags properly in HTML right?


<lies>
If so then it should be really straightforward.

Also you could certainly use switch statements with strings. A window procedure in the WINAPI is an example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
switch(msg)   // This does nothing... but you get the idea
    {
        case WM_CREATE:
            break;
        case WM_PAINT:
            break;
        case WM_RBUTTONDOWN:
            break;
        case WM_CLOSE:
            DestroyWindow(hwnd);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);

Just put whatever constant you are comparing against inside of the parentheses of the switch.

ex. switch(your_variable_name_here)

in your case: switch(commands)

I hope that gives you some ideas to play with.

</lies>
Sean
Last edited on
@ slambert: Psst! Those are actually numbers. Open 'WinUser.h' and use Ctrl+F to see what I mean.
Good call there. in which case disregard everything after line 3 of my previous post.

switch can only be used with int.


Sean
Last edited on
closed account (Dy7SLyTq)
you can switch strings actually

1
2
3
4
5
6
7
8
9
10
11
12
switch (str2int(s))
{
  case str2int("Value1"):
    break;
  case str2int("Value2"):
    break;
}

constexpr unsigned int str2int(const char* str, int h = 0)
{
    return !str[h] ? 5381 : (str2int(str, h+1)*33) ^ str[h];
}
Well no, you were correct in that a switch statement can be used with strings. Your example just happened to be a bad choice.
closed account (Dy7SLyTq)
why is mine a bad choice?
@ DTSCode: That was directed at slambert. Yours is a little odd in that I would avoid function specifiers if possible but it looks fine.
closed account (Dy7SLyTq)
oh... my bad
Alright let me get this straight once and for all, you can't put non-int non-const variables into a switch expression but there are workarounds that let you do the same basic thing.

I now see the issue with the WINAPI example, after looking back.

If I remember right, Java initially didn't allow String in their switch so you had to do enum workarounds like the WINAPI case. I mistakenly thought that like java, c++ changed to support string.

I assume like always this hasn't happened because of C backwards compatibility issues.

I think DTSCode's workaround is also pretty nice. I must say this is the first time I have ever even thought about the type issues with switch statements, I guess I just glossed over it. This thread was very instructive.

Sean
Last edited on
You are all getting off topic, in html i do know how to make <b><i>bold and italic</b></i> but i dont know how to implement it in my program....
closed account (Dy7SLyTq)
welcome to the internet. thread derailment. post the whole code. we cant go off your snapshot
i do know how to make <b><i>bold and italic</b></i>


Sorry to bother you, I just registered on the forum to tell you, that the proper tag alignment would be:

1
2
3
4
5
<b> 
  <i> 
    text here
  </i>
</b>


Your way might be misinterpreted by browsers.

Also, on Security side. If you program to encode into HTML "whatever text you desire" are you aware of "HTML escaping" (Example article at Stack Overflow: http://stackoverflow.com/questions/5665231/most-efficient-way-to-escape-xml-html-in-c-string )?
Because if someone would like to encode in bold and italic string of
</i></b> <iframe src='hackwebsite.com'></iframe> <b><i>
your program would actually create dangerous html files.

Good luck,
Vill
Last edited on
closed account (Dy7SLyTq)
@vill Thats not true. I write all of my scripts the way op does. Its all whitespace so it doesnt matter
Thats not true. I write all of my scripts the way op does. Its all whitespace so it doesnt matter

Vill wasn't talking about the whitespace, but the fact that the <I> </I> block needs to be entirely inside the <b> </b> block. If you look at the post Vill's responding to, you'll see that the OP has the tags in a messed-up order.
closed account (Dy7SLyTq)
sorry my bad. it looked like he was talking about the style not syntax
Well, if the OP had used the style suggested by Vill, then his/her error would have been easy to spot instantly.

Programmers don't bang on about code layout and indentation because we're obsessed with visual aesthetics. We do it because it helps you avoid mistakes.
closed account (Dy7SLyTq)
i understand that, but just because it works for one person doesnt mean it works for all. there are pitfalls and advantages to each style
Topic archived. No new replies allowed.