First the prog asks for "enter character 1:", but when I press enter it should ask for "enter character 2:" and then wait for my input.Instead of that it only prints "enter character 2:" then prints "enter character 3:" and waits for my input.And same keeps happening for the rest.how to solve this??? help pls!!
#include <stdio.h>
int main ()
{
printf("\n\nName: ENTER YOUR NAME HERE");
printf("\nID: ENTER YOUR ID HERE\n\n");
char c;
int num = 0;
puts("Input a character and press ENTER. Type '.' to exit:");
do
{
printf("\nEnter character %d: ", ++num);
c = getchar();
} while (c != '.');
return 0;
}
getchar() retrieves the next character from the input. That said, the return key counts as a character. When you type in a letter then hit enter, the first letter is read, the loop executes again, and getchar() then reads the enter key from the input.
use cin (which will handle input the way you want to) instead of getchar. Actually, if you're intending on using C++ features, you should use <iostream>, the C++ version of stdio.h. (printf, puts, and getchar are C functions).
so it'll look something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
using std::cout; using std::cin;
int main()
{
cout << "\n\nName: ENTER YOUR NAME HERE\nID: ENTER YOUR ID HERE\n\n";
char c;
int num = 0;
cout << "Input a character and press ENTER. Type '.' to exit:";
do
{
cout << "\nEnter character " << ++num << ": ";
cin >> c;
} while (c != '.');
return 0;
}