Home Technical Talk

C++ for n00bs

Trying to do this horrible horrible extra credit assignment for a class and i noticed how much i suck/hate programming. Not to get into to much detail about the extra credit assignment but it involves rewriting a code using loops.

So my 1st question for anyone who can answer it for me is ... what does \n mean and/or do for a line of code?

Replies

  • renderhjs
    Options
    Offline / Send Message
    renderhjs sublime tool
    This is not a place where people help out with housework or class assignments- it's kind of rude because it makes feel people beeing used for something non- important, something not worth sharing at all!

    \n = new line
    \r = return carriage
    \t = tab
  • animatr
    Options
    Offline / Send Message
    animatr polycounter lvl 18
    \n means start a new line.

    for instance(in python)
    print "hello\nworld"

    would look like:
    hello
    world
  • MoP
    Options
    Offline / Send Message
    MoP polycounter lvl 18
    \ is a general escape character for special characters inside strings.
    some common ones are \n for newline, \t for tab-space, and \\ which simply goes to \ (to avoid the code trying to escape the next character in the string after the initial \).
  • RawRanator
    Options
    Offline / Send Message
    thank you guys.
  • RawRanator
    Options
    Offline / Send Message
    i'm trying to write a code that will create a diamond
    *
    ***
    *****
    ***
    *
    EDIT: the * should all be aligned in the center but it didn't come out that way.

    like this diamond here, using loops.


    i am extremely terrible with loops and arrays. So forgive me for asking this dumb question but ...

    int _tmain(int argc, _TCHAR* argv[])
    {

    for(int i = 0; i < 7; i++){

    string Display;
    cout << i << "*";

    do{


    } while(true);


    }


    return 0;

    }




    does this look ok? am i on the right track? any tips on what to do afterwards?
  • fearian
    Options
    Offline / Send Message
    fearian greentooth
    Honestly, this isn't realy polycounts speciality, and while you might get one or two responses, you'll get more help faster on a coding forum or something!
    good luck though!
  • RawRanator
    Options
    Offline / Send Message
    i figured i would try here since you guys have been helpful before in the past but thanks anyways
  • FRANKGRIMES
    Options
    Offline / Send Message
    int _tmain(int argc, _TCHAR* argv[]) {  
     for(int i = 0; i < 7; i++){ 
      string Display; 
      cout << i << "*"; 
       do{  } while(true); 
      }  return 0; 
    }
    

    if you're meant to have a dynamic "diamond" you want the maximum number as your input, save that number as an integer. then solve the number of lines necessary to reach that number so your program will print '*' at line one in the position max_stars / 2, then expand towards the middle and regress afterwards. if this doesn't make sense i could write it for you :p
  • SyncViewS
    Options
    Offline / Send Message
    SyncViewS polycounter lvl 13
    Hi, here it is. It is quite simple and should get you started.
    #include <iostream>
    using std::cout;
    using std::endl;
    
    void diamond(int size)
    {
        int spaces = size / 2;
    
        // loop over the number of lines
        for (int i = 0; i != size; ++i)
        {
            // loop to write whitespaces for each line
            for (int j = abs(spaces); j != 0; --j)
                cout << " ";
    
            // loop to write * for each line
            for (int k = 0; k != (size - abs(spaces) * 2); ++k)
                cout << "*";
    
            --spaces;
    
            // go to next line and flush the stream
            cout << endl;
        }
    }
    
    int main()
    {
        // call the function diamond()
        // it works as it's meant if size is odd
        diamond(5);
    
        return 0;
    }
    

    If you need some references for C++, try the sites:

    http://www.learncpp.com/
    http://www.cplusplus.com/

    Good luck!
Sign In or Register to comment.