-
Absolute newbie question
Hi. I know it's trivial, but when using cout to output stuff, how can you get it to output '\'? Because it seems not to output them because it expects them to be followed by either 'n' or '0' or something else telling it to do something outputtey :(.
I wanted to make a program that took a string and turned it into big ascii text, for example 'a' would become:
Code:
__
/ \
/ \
/__/\__\
Which would be incredibly annoying, but interesting nonetheless. I've only done this much so far:
Code:
int main () {
char str [256];
cout<<"Please input the text to be converted to BIG ASCII TEXT:\n\n";
cin.getline (str, 256, '\n');
switch (str[0]) {
case 'a':
cout<<" __ \n / \ \n / \ \n/__/\__\ \n";
break;
}
}
But this is obviously impared if you can't use any backslashes.
I don't really want help with it, because I think I'll learn more working it out for myself.. I just thought it might clear up why I want to use '\'.
-
-
printf() works, but for some jolly reason, it gives you wild errors if you have an ODD number of \'s that you wish to print out.
-
To output simply '\', try
cout<<"\\";
-
Thanks very much. Should've been able to work that out for myself really :rolleyes:.
-
Another question:
Is it possible to get a switch case statement to execute for each element of an array? For example, in my 256 character string, is it possible for my switch to apply to each of the characters, so that I can have one switch with 26 cases (one to draw an ascii version of each letter) , or do I need to have 256 switches, each of which contains what could be described as (str [x(i) = x(i-1) +1] ) up untill 256?
Having described it like that, I'm wondering if it's possible to do:
Code:
for (int x =0; x<256; x++) {
switch (str [x]){
case 'a':
cout<<"(blah blah blah)";
break;
case 'b':
cout<<"(blah blah blah)";
break;
etc
}
}
Also, am I right in thinking that switch (str) would just check if the case == the memory address? The whole pointers thing is a bit hazy to me :(...
-
Yes it is possible to nest a switch inside a for loop as you detailed above.
And you are correct, doing:
would not meet your purposes.
-
Bear with me because this is my second program ever...
I compiled and ran the following, but every time you input anything it outputs the ascii drawings (vertically) corresponding to the string you input, followed by "LXLLPP" for short sentences, and simply "LPP" for longer ones. I thought perhaps this was a problem with my memory, but I restarted many times and got the exact same output, as did a few friends I sent it to. Can anybody see the problem?
Code:
#include <iostream>
using namespace std;
int main () {
char str [256];
cout<<"Please input the text to be converted to BIG ASCII TEXT:\n\n";
cin.getline (str, 256, '\n');
for (int x = 0; x<254; x++) {
switch (str [x]) {
case 'a':
cout<<" __\n / \\ \n / \\ \n / \\ \n/___/\\___\\ \n";
break;
case 'b':
cout<<" __\n| \\ \n|__/ \n| \\ \n|___/ \n";
break;
case 'c':
cout<<" ___\n / __|\n / /\n \\ \\__\n \\___|\n";
break;
case 'd':
cout<<" ___\n| \\ \n| /\\ |\n| \\/ |\n|___/\n";
break;
case 'e':
cout<<" ___\n|\n|__\n|\n|___\n";
break;
case 'f':
cout<<" ___\n|\n|__\n|\n|\n";
break;
case 'g':
cout<<" ___\n / __|\n/ / ___\n\\ \\__||\n \\____|\n";
break;
case 'h':
cout<<" _ _\n| | | |\n| |_| |\n| _ |\n|_| |_|\n";
break;
case 'i':
cout<<" ____\n|_ _|\n ||\n _||_\n|____|\n";
break;
case 'j':
cout<<" ____\n|_ _|\n ||\n _||\n|__/\n";
break;
case 'k':
cout<<"_ __\n| | / /\n| |/ /\n| _ \\ \n|_| \\_\\ \n";
break;
case 'l':
cout<<" _\n| |\n| |\n| |__\n|____|\n";
break;
case 'm':
cout<<" __ __\n/ \\ / \\ \n| |\\\\ //| | \n| | \\v/ | | \n|_| v |_| \n";
break;
case 'n':
cout<<" __ _ \n| \\ | | \n| |\\\\| | \n| | \\ | \n|_| \\_| \n";
break;
case 'o':
cout<<" __\n / \\ \n/ /\\ \\ \n\\ \\/ / \n \\__/ \n";
break;
case 'p':
cout<<" ___ \n| \\ \n| _/ \n| | \n|_| \n";
break;
case 'q':
cout<<" __ \n / \\ \n/ \\ \n\\ \\ / \n \\__\\ \n";
break;
case 'r':
cout<<" ___ \n| \\ \n| / \n| \\ \n|_|\\__\\ \n";
break;
case 's':
cout<<" ___ \n / __| \n \\ \\ \n _\\ \\ \n|___/ \n";
break;
case 't':
cout<<" _____ \n|_ _| \n | |\n | |\n |_|\n";
break;
case 'u':
cout<<" _ _ \n| | | | \n| | | | \n| - | \n \___/ \n";
break;
case 'v':
cout<<"__ __ \n\\ \\ / / \n \\ v / \n \\ / \n v \n";
break;
case 'w':
cout<<"__ __ \n\\ \\ /\\ / / \n\\ v v / \n \\ /\\ / \n v v \n";
break;
case 'x':
cout<<"__ __ \n\\ \\ / / \n \\ \\/ / \n / /\\ \\ \n/_/ \\_\\ \n";
break;
case 'y':
cout<<"__ __ \n\\ \\ / / \n \\ v / \n | | \n |_| \n";
break;
case 'z':
cout<<" ____ \n|__ | \n / / \n / / \n|____| \n";
break;
}
}
cin.get();
}
-
First you might want to make your loop stop when it reaches the end of the input (rather than going through the entire buffer):
Code:
for (int x=0;str[x]!='\0';x++) //stop at null character
{
//...
}
And also check the faq on converting strings to lowercase (as it is, your program wont convert capital letters like 'A', 'B', etc)
-
Didn't think of using str [x] in the loop. Silly me. Thanks, that's fixed it!