Printing to a console

Login to reply to this topic.
Fri, 2005-07-22 10:42
Joined: 2005-07-22
Forum posts: 52
Hi !

I am new to Symbian C++ and need help with printing a descriptor(string) to a console. Consider the code below where I print a descriptor on the console.

Code:

TBufC <20> Kbuf = "hello" ;

TInt ctr = 0 , len ;

TText ch ;
len = KBuf.Length();

   do
   {

            ch = KBuf[ctr];
         
           _LIT(Kch,"%c");
         
           console->Printf(Kch,ch);
         
           ctr++ ;


   }while(ctr < len); 
   


In the above case I am printing a string (Descriptor) to the console character by character. Isnt there a way or some function using which I can print the descriptor directly without using the character by character approach ?. In ANSI C++ e.g. there is a puts() function but there doesnt seem to be a console->Puts() counterpart in Symbian C++.

Plz help.

Fri, 2005-07-22 10:51
Joined: 2005-05-24
Forum posts: 12
Re: Printing to a console
console->Printf() can be used to print a String. An example:

TInt myNumber = 0;
console->Printf(_L("My number: %d"), myNumber);

Fri, 2005-07-22 13:44
Joined: 2005-07-22
Forum posts: 52
Re: Printing to a console
Hi !

Thanks for replying  Smiley . Using printf the way you pointed how does work for numeric data but doesnt for strings. I mean the tried the following :

Code:

TBuf <20> test ;

_LIT(KTest,"hello");

test = KTest ;

console->Printf(_L("%s"),test);


The above doesnt work !!.
Fri, 2005-07-22 13:55
Joined: 2005-01-04
Forum posts: 226
Re: Printing to a console
Try this........


_LIT(KTest,"hello");

TBuf <20> test(KTest) ;

console->Printf(test);

Or.......

console->Printf(_L("hello"));

Or.........

_LIT(KTest,"hello");

console->Printf(KTest);



BR,




Fri, 2005-07-22 15:47
Joined: 2005-07-22
Forum posts: 52
Re: Printing to a console
Thanks,that works Smiley. Now if I have to receive user input I again have to resort to character by charecter logic. e.g.) Consider the code below :

Code:

void CDesc::Greet()
{
  //Ask for users name

   console->Printf(_L("\nEnter your name :"));

  //Declare a buffer on the stack to hold the name

  TBuf <100> name ;

  TText ch ; //User entered character

  TInt ctr = 0 , asc ;

  //Receive the name

  do
  {

      ch = console->Getch() ;

      asc = ch ;

      console->Printf(_L("%c"),ch);

      if(asc != 32 && asc != 13)
            name.Append(ch);

      ctr++ ;


  }while(asc != 32 && asc != 13)  ; //Space = 32 , Enter = 13

   //Print a Hello on a newline

   console->Printf(_L("\nHello,")) ;

    //Print the name

  console->Printf(name);

  //Print an exclamation

   console->Printf(_L("!"));


}



Isd there a counterpart of scanf() in Symbian C++ that will allow me to receive a string from the user at one stretch rather than resorting to character by character logic ?.

Thanks Smiley
  • Login to reply to this topic.