Absolute newbie question

This is a discussion on Absolute newbie question within the C++ Programming forums, part of the General Programming Boards category; Hi. I know it's trivial, but when using cout to output stuff, how can you get it to output '\'? ...

  1. #1
    Perma-Newbie
    Join Date
    Feb 2005
    Posts
    7

    Smile 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 '\'.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    \\ outputs a single \
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    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.

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    38
    To output simply '\', try

    cout<<"\\";

  5. #5
    Perma-Newbie
    Join Date
    Feb 2005
    Posts
    7

    Thumbs up

    Thanks very much. Should've been able to work that out for myself really .

  6. #6
    Perma-Newbie
    Join Date
    Feb 2005
    Posts
    7

    Smile

    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 ...

  7. #7
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Yes it is possible to nest a switch inside a for loop as you detailed above.

    And you are correct, doing:
    Code:
    switch(str)
    would not meet your purposes.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  8. #8
    Perma-Newbie
    Join Date
    Feb 2005
    Posts
    7
    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();
    }

  9. #9
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    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)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  10. #10
    Perma-Newbie
    Join Date
    Feb 2005
    Posts
    7
    Didn't think of using str [x] in the loop. Silly me. Thanks, that's fixed it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  2. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 08:00 AM
  3. C prog newbie question
    By Draginzuzu in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 05:45 PM
  4. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 03:38 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21