This is a basic c++ project I am working on. It is supposed to be a message coder/decoder; I want to take a string and translate it to ansi values or take a block of values and translate it to a sentence. I am making progress but have some hangups.
My input string to turn into code stops at a whitespace. i have tried noskipws but am sure i ddin't use it correctly. i would love for the translation to include the whitespace ansi code '32'...i think
Then my output is only grabbing the first integer entered; also I would love to have the user enter the integers without having in a 'string', I mean so they wouldn't have to press enter every time.
Thanks for taking a look, any help or even pointing in a direction is appreciated!
P.S. I used the integer 0000 as an escape value for lack of a better idea
#include <iostream>
#include <string>
#include <fstream>
usingnamespace std;
int main ()
{
int source;
cout<<" If you have a message to encode enter '1'"<<endl;
cout<<" If you have one to decode enter '2'"<<endl;
cin>>source;
switch (source){
case 1:{
ofstream code;
char coder;
string message;
cout<<" What phrase do you want coded?"<<endl;
cout<<" ";cin>>message;
code.open("code.txt");
for (size_t i=0; i < message.length(); i++){
coder=message.at(i);
code<<(int)coder;
code<<" ";
}
code.close();
break; }
case 2:{
ofstream code;
int coder;
int message;
cout<<" What code do you want decoded?"<<endl;
cout<<" Press enter after each integer"<<endl;
cout<<" Enter 0000 when the message is done"<<endl;
do{
cout<<" ";cin>>message;
code.open("code.txt");
code<<(char)message;
code<<" ";
}while(message!=0000);
code.close();
break;
}
break;
}
ifstream myReadFile;
myReadFile.open("code.txt");
char output[100];
if (myReadFile.is_open()) {
while (!myReadFile.eof()) {
myReadFile.read (output, 100);
cout<<output;
}
}
myReadFile.close();
return 0;
}
> Then my output is only grabbing the first integer entered;
> also I would love to have the user enter the integers without having in a 'string',
> I mean so they wouldn't have to press enter every time.
1 2
int i, j, k ;
std::cin >> std::skipws >> i >> j >> k ;
So i have the whitespaces working great, now something strange.
Everything works like I want it to until I add the IF statement; then it breaks and returns 0;
If I comment out the IF it works again. I tried a switch instead and got the same result as an IF
line 8: cin>>menu; This will just read one char and the new line char will still be in the input buffer so when you later call getline it will read the rest of the line which is empty unless the user had entered more characters on the same line. To fix this you can use ignore to discard the rest of the line: cin.ignore(numeric_limits<streamsize>::max(), '\n' );
On line 26 one is uninitialized when you use it on line 30 so you can't be sure what value it has. Also note that 000 is the same as 0. A better way to write the loop is
1 2 3
while(cin>>one && one != 0){
code<<(char)one;
}
This also has the advantage that it will stop if the user enters invalid input or EOF is reached.