Kavien W.

asked • 07/22/19

C++ program use of getchar and putchar function

What is difference between these two programs

1 )


#include<iostream.h>

#include<stdlib.h>

int main()

{

system("cls");

char ch ;

do

{

cout<<"enter a character :";

ch= getchar();

if ( ch== '\n')

ch = getchar();

cout<<endl;

if (ch>= 65 && ch<= 90)

ch = ch + 32;

else

if(ch>=97 && ch<= 122)

ch = ch -32;

putchar(ch);

cout<<endl;

} while ( ch!='0');

return 0 ;

}



2 )


#include<iostream.h>

#include<stdlib.h>

int main()

{

system("cls");

char ch ;

do

{

cout<<"enter a character :";

ch= getchar();

if (ch>= 65 && ch<= 90)

ch = ch + 32;

else

if(ch>=97 && ch<= 122)

ch = ch -32;

putchar(ch);

cout<<endl;

} while ( ch!='0');

return 0 ;

}


And why we are using this line in middle of program .

if ( ch== '\n')

ch = getchar();

cout<endl;



1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.