looping help
im trying to write a program that encrypts the user input. i can get the first char inputed read and encrypted and displayed on screen but i cant seem to get any char past that.
#include <iostream>
using namespace std;
int main()
{
char input;
cout << "Enter text to be encrypted" << endl;
cin.get(input);
switch (input)
{
case 'a':
cout << char('a' + 13);
}
return 0;
}
this encrypts a but even if i input aaa it only reads and encrypts the first a. What ami not doing that it doesnt read more than the first char?
[654 byte] By [
MnkyScpe] at [2007-11-11 10:31:14]

# 1 Re: looping help
1. All you are saving from the "text to be encrypted" input is a single character. Nothing more. So you need to either have a char array or, better still, a string to hold the input, which you can test one character at a time.
2. You need to implement a loop which tests whether there are any more char's in the input string, then goes to work on the next char ...
nspils at 2007-11-11 20:58:35 >

# 2 Re: looping help
to get a string i can do one of two things, getline, but ive tried that and i cant seem to get it to read all the char saved from getline. Also im trying to refrain from using getline because of the extra line it includes. Another thing is that a string is not always going to read the whitespaces, which i want to do as well. Ive also tried setting a buffer on my char to 80 so it saves the first 80 char to char but i cnat read em out and convert...
# 3 Re: looping help
Strings will hold whitespace.
Looks like you need to do some research on input streams and their functions:
http://www.cplusplus.com/
and
http://cs.smu.ca/~porter/csc/ref/cpp_io.html
will help you get started
nspils at 2007-11-11 21:00:35 >

# 4 Re: looping help
well i had a flash of insight and ive come up with the following code that does exactly what i want it to but... it wont terminate once the user is done inputing the text.
#include <iostream>
using namespace std;
int main()
{
char ch;
cout << "Enter text to be encrypted." << endl;
while (cin)
{
cin.get(ch);
if(ch >= 'a' && ch <= 'm')
cout << char (ch + 13);
else if (ch >= 'n' && ch <= 'z')
cout << char(ch - 13);
else if (ch == ' ')
cout << char(32);
}
return 0;
} am i missing a break somewhere or is the while loop not finding a reason to end so it just keeps waiting?
# 5 Re: looping help
cin will always be "true" (it exists) so you get stuck inside that loop without an escape .. but you're on the right track ... what can signal to you that a stream, like cin, has content in it, or is not empty?
nspils at 2007-11-11 21:02:40 >

# 6 Re: looping help
well i figured how to end it, and i was able to change the code accordingly to write the output to a file and save it, and now i begin work on my decryotion prrogram and im encountering some issues... i figured all i had to do was open the file i save the output to from my encrypt program and have it read teh chars and decrypt them by reversing what i initially did,
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream inFile;
ofstream outFile;
char ch;
inFile.open("encrypt.sec");
cout << inFile;
do {
cin.get(ch);
if(ch >= 'a' && ch <= 'm')
cout << char (ch - 13);
else if (ch >= 'A' && ch <= 'M')
cout << char (ch - 13);
else if (ch >= 'n' && ch <= 'z')
cout << char (ch + 13);
else if (ch >= 'N' && ch <= 'Z')
cout << char (ch + 13);
else if (ch == ' ')
cout << char (32);
} while (ch != '\n');
return 0;
} for some reason or another it reads a strange input from the file instead of what is actually there. Oh and here is my finished encryption code,
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream inFile;
ofstream outFile;
char ch;
cout << "Enter text to be encrypted." << endl;
outFile.open("encrypt.sec");
do {
cin.get(ch);
if(ch >= 'a' && ch <= 'm')
outFile << char (ch + 13);
else if (ch >= 'A' && ch <= 'M')
outFile << char (ch + 13);
else if (ch >= 'n' && ch <= 'z')
outFile << char (ch - 13);
else if (ch >= 'N' && ch <= 'Z')
outFile << char (ch - 13);
else if (ch == ' ')
outFile << char (32);
} while (ch != '\n');
return 0;
}