Well, just taking a quick glance at this - looks like both versions will echo characters you input, but version 1 will trap new lines. It will put one out when found so that your output continues on the next line until another one is found. Version 2 looks like it will simply ignore new lines.
Kavien W.
asked 07/22/19C++ 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
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.