reading a single character at a time

Aug 19, 2011 at 6:06am
i am trying to make a program to encrypt a character string.for this i require the compiler to read a single character at a time,process it and go on for the next character and so on untill the user press the enter key...but i am not able to read a single character at a time and process it...
FOR eg:
if i declare something like what is given below

1
2
char h;
cin>>h;

thought the compiler will store only one character but the user is allowed to enter as many characters as he wants...this is what i dont want..following is my strategy
1
2
3
4
5
6
7
8
9
int i=0;
    char h=0,arr[100];
    while((int)h!=13)
    {
        cin>>h;
        cout<<"\b"<<"*";
        h=arr[i];
        i++;
    }

but this is definitely not working for the reason mentioned above....plz help me out with this....
Last edited on Aug 19, 2011 at 6:06am
Aug 19, 2011 at 9:14am
Why don't you want to read the whole string first and process the encryption?

It goes as follows:
1
2
3
4
5
6
7
8
    char array[100];
    memset(array, 0, 100);

    cin >> array;

    cout << "original text: " << array << endl;
    cout << "encrypted text: "; // It is shifted by one character
    for (int i = 0; array[i] != 0 && i < 100; i++) cout << (char)(array[i] + 1) << " ";


But I suggest that you should use the c++ style string.
Aug 19, 2011 at 7:42pm
i dont want to make the user see what he is entering....sumthing like when you type your password.....plz help me out
Aug 19, 2011 at 8:24pm
closed account (zb0S216C)
Try this:
http://www.cplusplus.com/forum/general/3570/page1.html#msg15410

Wazzak
Aug 19, 2011 at 9:07pm
Or this: http://www.cplusplus.com/forum/general/47820/#msg259525
or _getch() (#include <conio.h> ).

Alas, Framework's post does what you want best... (I should read more carefully!).
Last edited on Aug 19, 2011 at 9:08pm
Topic archived. No new replies allowed.