Program that outputs a string as a "moving banner"

This is a discussion on Program that outputs a string as a "moving banner" within the C++ Programming forums, part of the General Programming Boards category; Hi guys, my latest project at school is to create this "rolling banner" type thing where a string is outputted ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    14

    Program that outputs a string as a "moving banner"

    Hi guys, my latest project at school is to create this "rolling banner" type thing where a string is outputted across the screen, going left.
    Something like this:
    Hello World!
    ello world ! h
    llo world ! he

    By using the "Wait" function I can make it look like a sort of animation. My teacher advised me to resize the string to 80 as that is the length of the console. I've finally devised a way to do it, but I've run into an issue. The string moves from one line to the next, and it eventually runs out of space and the program crashes. How do I make it so that the string only moves on a single line?9000 is just an arbitrary number also. Any help is greatly appreciated! Heres the code:
    Code:
    #include<iostream>
    #include<string>
    #include<ctime>
    #include<stdlib.h>
    using namespace std;
    void work(string s);
    void Wait(double ms);
    int main()
    {
        string s;
        cout<<"Enter dat string: ";
        getline(cin,s);
        system("CLS");
        work(s);
    
    
    }
    void work(string s)
    {
        int j=0,z=79,x=0;
        s.resize(80);
        for(int b=0;b<9000;b++){
        system("CLS");
        for(int i=j;i<s.length()-1;i++)
        {
            cout<<s.at(i);
            cout<<" ";
        }
        j++;
        Wait(.001);
        for(int k=z;k>0;k--)
        {
            cout<<" ";
        }
        z--;
        Wait(.001);
        for(int j=0;j<x;j++)
        {
            cout<<s.at(j);
            cout<<" ";
        }
        x++;
        Wait(.001);
        }
    }
    void Wait(double ms)
    {
        clock_t endwait;
        endwait = clock () + ms * CLOCKS_PER_SEC ;
        while (clock() < endwait) {}
    }

  2. #2
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    honestly, if i was doing this, i would use a char array, with a loop, and after the line is printed, use a "gotoxy" command to move curser back to the beginning of the "banner, and rewrite the "new" banner with the adjusted array indexes....

  3. #3
    a newbie :p
    Join Date
    Aug 2008
    Location
    Zurich, Switzerland, Switzerland
    Posts
    91
    Isn't it obvious, it's merely because you are trying to access memory beyond its real size. I would suggest you to learn how to tap breakpoint, this is the most essential skill in programming though :-)

  4. #4
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    Why not just use the carriage return escape sequence? Something like.
    Code:
    while(scrolling){
      std::cout<<string<<"\r";
      PauseForABit();
      MoveStringOverByOne();
      CheckForEndingCondition();
    }
    Last edited by Lesshardtofind; 01-26-2013 at 02:48 AM.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    2,590
    that's not guaranteed to work, because some systems (macos <= 9.x) might use \r as their newline character.

  6. #6
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    I had been taught escape sequences were standard. How about a char with the ASCII value of 13 then would that work on all systems?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,313
    Quote Originally Posted by Lesshardtofind
    I had been taught escape sequences were standard.
    Indeed '\r' is standard, denoting a carriage return. Where portability is concerned, I think the real problem is that there is nothing in the standard that states that a carriage return should result in the behaviour that you have in mind. (Then again, there is nothing in the standard that states that there is a screen or a terminal.)

    Quote Originally Posted by Lesshardtofind
    How about a char with the ASCII value of 13 then would that work on all systems?
    In ASCII, 13 corresponds to a carriage return, which in turn corresponds to '\r' in C++, so using 13 would just be using a magic number for no good reason. Furthermore, it is possible for 13 to map to say, the character 'a' on some system, so there is no benefit in using 13 when you really mean '\r'.

    EDIT:
    That said, I think that your '\r' suggestion is fine. It just isn't guaranteed to work everywhere, but the same goes for any other solution.
    Last edited by laserlight; 01-26-2013 at 12:49 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    1,220
    The escape sequence "\n" is guaranteed by the input output system to be translated to what is appropriate for the system to mean "newline". You may notice that if you output "\n\n\n" to a text file in Windows, the result will be 6 bytes.

    For the original question, if you want to make some kind of moving banner that would actually work on all systems, you would probably need to use a compatibility layer like ncurses or pdcurses, which supplies it's own cursor control functions. These functions will then do "the right thing" on whatever your target system is.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,313
    Quote Originally Posted by c99tutorial
    The escape sequence "\n" is guaranteed by the input output system to be translated to what is appropriate for the system to mean "newline". You may notice that if you output "\n\n\n" to a text file in Windows, the result will be 6 bytes.
    Indeed (other than for binary I/O), though that is not relevant here since Elkvis was concerned with whether '\r' works for the behaviour that Lesshardtofind had in mind rather than disputing that '\n' means "newline".

    For the original question, if you want to make some kind of moving banner that would actually work on all systems, you would probably need to use a compatibility layer like ncurses or pdcurses, which supplies it's own cursor control functions. These functions will then do "the right thing" on whatever your target system is.
    I agree. In other words, accept that this is not portable, so you deal with it by using a library that is as portable as needed. That said, given that this is a school assignment, if you are not allowed to use a third party library, then incorporating Lesshardtofind's suggestion by writing a function that can later be re-implemented with such a library would be a good idea.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Virtual Printer" or "Moving Printjobs"
    By extasic in forum Windows Programming
    Replies: 12
    Last Post: 06-30-2011, 09:33 AM
  2. Replies: 4
    Last Post: 06-16-2010, 02:12 PM
  3. Replies: 46
    Last Post: 08-24-2007, 05:52 PM
  4. sockets and a "banner" scan
    By Tuborgrules in forum Networking/Device Communication
    Replies: 10
    Last Post: 01-19-2006, 11:19 AM

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