cin.get() and cin.ignore() to find next character.

i am working on a basic program to encrypt/decrypt a message based on numbers being equal to a letter.

i am wondering how i can use cin.get() and cin.ignore() to get the next number in the sequence. right now all the code does is read the first number and print it out as the corresponding character. but if i wanted to input say 8 5 9 9 15 (which would need to be printed out as hello) all at once and without using strings or arrays how would i do this?

Thanks for your help in advance!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

   int code, length=0;
   int count=0;

   cout<<"Enter the length of your message: ";
   cin>>length;

   cout<<"Enter the coded phrase: ";
   cin>>code;
   
   while(count<length){
      count++;
      
      if(code!=0 && code != 27){
         code += 96;
         cout<< (char) code;
      }
      cin.get(code); //the problem is here. im not sure if this is even the correct usage
   }
Last edited on
Still can't figure this out? Any help would be greatly appreciated
1
2
3
while( std::cin>>code ){
  //...
}
ne555 that would work but i need to enter the entire encoded message at one time without hitting enter between each number.

when the program is run it would look like this. ignore the options to encode or decode.


1 - Decode
2 - Encode
Select an option: 1
Enter the length of your message: 5
Enter the coded phrase: 8 5 9 9 15


and with that i need it to print out


1 - Decode
2 - Encode
Select an option: 1
Enter the length of your message: 5
Enter the coded phrase: 8 5 9 9 15
The Decoded message is: hello


instead it now prints out


1 - Decode
2 - Encode
Select an option: 1
Enter the length of your message: 5
Enter the coded phrase: 8 5 9 9 15
hhhhh

Last edited on
Topic archived. No new replies allowed.