Thread: Question about cprint

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    71

    Question about cprint

    Would somebody be able to tell me why the line "This program accepts.... starts all the way the right side of the screen when I use cprint?

    Code:
    textcolor (YELLOW);
       cprintf("\n                         Welcome to my program!\n\n  ");
       printf("This program accepts two given year. The ending year must be larger");\
       printf("than the\nstarting year. The program");
       printf(" will determine how may leap years are in the range.\n");
       printf("If there are no leap years in the range, the program will");
       printf(" determine the next \nclosest leap year after the ending year.\n\n");

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Probably because you're using cprintf right before your regular printf calls. See link. Pay attention to the Limits section.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    71
    But if I use just printf, the text isn't in the colour specified.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Did you read the Limits section like the man said to?
    Limits
    Like all other conio output functions, cprintf distinguishes between \r and \n.
    \n = newline aka line feed
    \r = carriage return

  5. #5
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192
    But there is no rule that says cprintf will print data right-aligned just because it sees spaces....
    My guess is that Guti14 is mistaking the inclusion of spaces to right alignment. cprintf basically differs from printf in place that it prints the text taking the foreground and background colours into consideration.

    -Harsha
    Help everyone you can

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    No its printing to the right because \n tells it to go down one line but keep the current position on that line. \r would tell it to go to the beginning of the line but stay on that line.
    \n\r would tell it to go down one line and then go to the beginning of the line

  7. #7
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192
    OOps...

    learnt that........(my original impression was that the linefeed char is only useful while printing the text to a printer......the good old dot-matrix printers etc., Never knew that \n and \r would effect the display of text also and since printf appends a \r to \n it really never occured to me..)

    Thanks,
    Harsha.
    Help everyone you can

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    well printf doesn't append \r. Nor does any normal output function. cprintf puts the console into a different mode in which it looks at \r and \n differently. In normal output \n is newline where as in the cprintf mode its line feed.

  9. #9
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192
    So, printf works in a default console mode where a \n is actually \n\r and cprintf changes the console mode where a \n is strictly \n and a \r is strictly \r.
    Help everyone you can

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM