|
I've been doing quite well in my class, but I've finally hit something that I can't figure out.
Basically, we have to decode a text file back to english (Caesar Cipher), and we have to run the text file through the our progame via the Command Prompt.
I've finished writing my code, but I can't get it to feed me back any output -- wrong or right.
I don't get an error or anything just this:
(checking to see if files I need are there, they are) + Show Spoiler +
(running the txt file through my program, maybe?) + Show Spoiler +
As you can see, nothing happens, and no errors appear either.
thats how I was instructed to do it and it's worked once before the other day, but now I finished writing my code and I can't test it =/
If you feel like taking the time, you can look at that as well:
+ Show Spoiler +#include <iostream> #include <iomanip> #include <math.h>
using namespace std;
char reverseCaesar( char ch ); char convPunct( char ch ); char convDigit( char ch ); char convRest(char ch); //*************************************************************************************************************************** int main() { char ch; int change;
cin >> ch;
while ( cin ) { char change; if( isalpha(ch) ) { change = reverseCaesar(ch); } else if (ispunct(ch) ) { change = convPunct(ch); } else if (isdigit(ch) ) { change = convDigit(ch); } else change = convRest(ch);
cin >> ch; }
system ("pause"); return 0; }
//***************************************************************************************************************************
char reverseCaesar( char ch ) { int i; if (ch <= 47 && ch > 44) { i = (int)ch + 23; } else if (ch <=99 && ch > 96) { i = (int)ch +23; } else i = (int)ch - 3; return char(i); }
char convPunct( char ch ) { switch(ch) { case ',': return '0'; break; case '.': return '1'; break; case '$': return '2'; break; case '&': return '3'; break; case '%': return '4'; break; case '*': return '5'; break; case '(': return '6'; break; case ')': return '7'; break; case '?': return '8'; break; case '!': return '9'; default: return ch; } }
char convDigit( char ch ) { switch(ch) { case '0': return '!'; break; case '1': return ','; break; case '2': return ')'; break; case '3': return '('; break; case '4': return '?'; break; case '5': return '-'; break; case '6': return '&'; break; case '7': return '$'; break; case '8': return '.'; break; case '9': return '+'; break; default: return ch; } }
char convRest(char ch) { if(ch==21) ch='\n'; else if(ch==22) ch=' '; else ch=ch;
return ch; }
I haven't added documentation, but it's fairly straight forward -- taking the characters from the txt file and decoding them (we had to write and use our own functions as well).
I'm willing to bet my program wouldn't work even if I could see it, but i'd at least like to try.
any help would appreciated, it's due in a couple of hours =)
if I have to i'll just submit and see if I can edit and resubmit tomorrow .
thanks.
   
|
been pretty long since I've done anything like coding or w/e, but if I understand correctly you enter your text file path as a parameter for your program? shouldn't there be parameters in your main then?
|
I have no help for your question, but I do believe that your reign at the top of LB will end after you bet on MBC over KT Rolster.
|
voting for MBC is your problem
edit: YOU GUYS ARE TOO FAST
|
Wait a minute something isn't right here.
I'm pretty sure you can't read a txt file with cin, while expecting a char.
I don't know the details of your assignment, but do you take the file you are reading from as a parameter? If you do, then you definitely need to take some command line arguments. If not, then you can just open the file in your main or accept it by input string, and read char by char until the end of file.
|
I'm a little confused about what you're asking, but I think you are asking why your program doesn't "do" anything when you run it. Okay, so given that...
Your second screenshot shows you running the program with the file as input correctly.
Now, your program doesn't produce any output on the command prompt because you aren't telling it to print anything. Use cout if you want to print messages to the command prompt. If you are trying to create a new file that contains the translation of the original file, you'll need to use an ofstream and output to that, or use "> some_file" when running the program and print to some_file using cout.
|
On October 17 2009 11:58 DrTJEckleburg wrote: I have no help for your question, but I do believe that your reign at the top of LB will end after you bet on MBC over KT Rolster.
i'm on top for a reason 
|
On October 17 2009 12:00 keV. wrote: Wait a minute something isn't right here.
I'm pretty sure you can't read a txt file with cin, while expecting a char.
Yes you can. Since he's using the "<" operator when running the program, it changes the stdin stream (which is what cin uses) to refer to the file instead of user input.
|
On October 17 2009 12:00 keV. wrote: Wait a minute something isn't right here.
I'm pretty sure you can't read a txt file with cin, while expecting a char.
I think the cin are for reading the characters in the text files, once they are read he changes them according to their values. could be wrong tho.
Anyhow there is no output like the other guy said
|
On October 17 2009 12:01 AcrossFiveJulys wrote: I'm a little confused about what you're asking, but I think you are asking why your program doesn't "do" anything when you run it. Okay, so given that...
Your second screenshot shows you running the program with the file as input correctly.
Now, your program doesn't produce any output on the command prompt because you aren't telling it to print anything. Use cout if you want to print messages to the command prompt. If you are trying to create a new file that contains the translation of the original file, you'll need to use an ofstream and output to that.
to put it into a new file, they taught us this format
program < input.txt > new.txt
i get a new file with nothing in it =/
|
So hmm, shoudn't you cout that ''change'' variable?
Edit: Try to cout the change variable at the end of the while loop, might cout the value into the new text file.
|
On October 17 2009 12:03 AcrossFiveJulys wrote:Show nested quote +On October 17 2009 12:00 keV. wrote: Wait a minute something isn't right here.
I'm pretty sure you can't read a txt file with cin, while expecting a char.
Yes you can. Since he's using the "<" operator when running the program, it changes the stdin stream (which is what cin uses) to refer to the file instead of user input.
I don't think reading from a file without a file handle is a good idea.
its sad how long its been since I've worked with simple I/O input...
|
Commenting is your best friend. Especially when you're asking others to help with your program. I didn't think it'd make that much difference, but I've had time where I look back on code and think "wtf was I doing here again?"
Do you need to separate it into functions like that? Sometimes it gets confusing debugging so many functions, especially when the code isn't your own. 
EDIT: Wait a second... You're reading this from a file, right? You have nothing set up for file IO.
|
The program is well formed, but it doesn't output anything! You need to write the output to "cout" and stream it into a file. Your command would become something like: pgm6 < input.txt > output.txt
|
Like whats been said in this thread, you need to either rewrite the original file as you read char by char or you need to print the output to a a new file or screen.
|
On October 17 2009 12:06 Ironson wrote: The program is well formed, but it doesn't output anything! You need to write the output to "cout" and stream it into a file. Your command would become something like: pgm6 < input.txt > output.txt
ok right, but how exactly do I make my program cout all the decoded characters at once after the loop is over?
and sorry for the code with out comments -- we did have to write functions to use in the main, i guess it's a way of them making us learn how they work.
|
Before your second "cin >> ch;", place "cout << change;".
|
On October 17 2009 12:09 Ironson wrote: Before your second "cin >> ch;", place "cout << change;".
woah, it worked.
got all the decoded next in a new file.
thanks a lot everyone -- you may have saved my A in this class.
now I can watch MBC just roll over KT tonight
|
this should give you simple error checking output.
+ Show Spoiler + int main() { char ch; int change;
cin >> ch;
while ( cin ) { char change; if( isalpha(ch) ) { change = reverseCaesar(ch);
} else if (ispunct(ch) ) { change = convPunct(ch);
} else if (isdigit(ch) ) { change = convDigit(ch);
}
else change = convRest(ch); //output goes here
cin >> ch; }
system ("pause"); return 0; }
|
On October 17 2009 12:13 keV. wrote:this should give you simple error checking output. + Show Spoiler + int main() { char ch; int change;
cin >> ch;
while ( cin ) { char change; if( isalpha(ch) ) { change = reverseCaesar(ch);
} else if (ispunct(ch) ) { change = convPunct(ch);
} else if (isdigit(ch) ) { change = convDigit(ch);
}
else change = convRest(ch); //output goes here
cin >> ch; }
system ("pause"); return 0; }
thanks a bunch kev
|
cin >> ch;
You're reading in from a file, not the command prompt. You need to use file IO commands instead of cin.
while ( cin ) {...}
That's not going to work. You need some kind of boolean logic statement there. Typically what I've done in my C++ class is we have a sentinel character and then loop until the sentinel character. I'm surprised that even compiled... Hmmm...
Yeah, didn't compile for me. Did it compile for you?
|
On October 17 2009 12:19 vAltyR wrote:You're reading in from a file, not the command prompt. You need to use file IO commands instead of cin. That's not going to work. You need some kind of boolean logic statement there. Typically what I've done in my C++ class is we have a sentinel character and then loop until the sentinel character. I'm surprised that even compiled... Hmmm... Yeah, didn't compile for me. Did it compile for you?
my code compiled fine and by adding the cout statement that people recommended fixed my issue -- all the decoding showed up in a new file (using -- pgm 6 < input.txt > new.txt) and it's all correctly decoded.
|
On October 17 2009 12:19 vAltyR wrote:You're reading in from a file, not the command prompt. You need to use file IO commands instead of cin. That's not going to work. You need some kind of boolean logic statement there. Typically what I've done in my C++ class is we have a sentinel character and then loop until the sentinel character. I'm surprised that even compiled... Hmmm... Yeah, didn't compile for me. Did it compile for you?
The '<' in the shell is the pipes the file's contents into the program through standard input - perfectly acceptable and extremely common when programming in Unix environments. The end of the file is delimited by the EOF character, and cin's implicit conversion to bool will return false when it is reached.
|
On October 17 2009 12:22 eMbrace wrote:Show nested quote +On October 17 2009 12:19 vAltyR wrote:cin >> ch; You're reading in from a file, not the command prompt. You need to use file IO commands instead of cin. while ( cin ) {...} That's not going to work. You need some kind of boolean logic statement there. Typically what I've done in my C++ class is we have a sentinel character and then loop until the sentinel character. I'm surprised that even compiled... Hmmm... Yeah, didn't compile for me. Did it compile for you? my code compiled fine and by adding the cout statement that people recommended fixed my issue -- all the decoding showed up in a new file (using -- pgm 6 < input.txt > new.txt) and it's all correctly decoded. Odd. *shrugs* Different compilers are different, I guess. If it works, no sense worrying about it.
|
On October 17 2009 12:05 keV. wrote:Show nested quote +On October 17 2009 12:03 AcrossFiveJulys wrote:On October 17 2009 12:00 keV. wrote: Wait a minute something isn't right here.
I'm pretty sure you can't read a txt file with cin, while expecting a char.
Yes you can. Since he's using the "<" operator when running the program, it changes the stdin stream (which is what cin uses) to refer to the file instead of user input. I don't think reading from a file without a file handle is a good idea. its sad how long its been since I've worked with simple I/O input...
I don't know what you mean by "isn't a good idea", but it's obvious that you don't know how I/O redirection works.
Here's a basic run down: a file handle is just a file descriptor that allows you read from the file. Similarly, stdin is a file descriptor that allows you to read from user input entered into the terminal. When you use the "<" and ">" operators when running a program, the shell is just changing the file descriptors that the program has when it starts.
So when you use "< file.txt", it changes stdin for the program to a file descriptor for file.txt, so when the program attempts to read from stdin, instead of reading from user input entered into the terminal, it will read from the file. So basically, you ARE using a file handle when you read from a file in that manner.
|
Also, to the OP, it seems suspicious that you managed to write 100% working code to accomplish this task without even understanding that you need to print something out if you want to see the result.
|
On October 17 2009 12:19 vAltyR wrote:You're reading in from a file, not the command prompt. You need to use file IO commands instead of cin. That's not going to work. You need some kind of boolean logic statement there. Typically what I've done in my C++ class is we have a sentinel character and then loop until the sentinel character. I'm surprised that even compiled... Hmmm... Yeah, didn't compile for me. Did it compile for you?
Yes it will work. The while will loop until there is no characters to be read anymore. Well I think.
|
On October 17 2009 12:45 AcrossFiveJulys wrote: Also, to the OP, it seems suspicious that you managed to write 100% working code to accomplish this task without even understanding that you need to print something out if you want to see the result. lol yeah.. and posting screenshots of running the thing first, only posting the code as an afterthought.
|
On October 17 2009 12:45 AcrossFiveJulys wrote: Also, to the OP, it seems suspicious that you managed to write 100% working code to accomplish this task without even understanding that you need to print something out if you want to see the result.
lol well, it didn't decode the capital 'Y's properly (turned them into '?'s) -- but w/e I submitted it.
i asked my TA for help earlier and he got me on the right path, but he also confused me as to if I needed a cout statement (he erased my previous ones) -- so I forgot about it until now =p
a silly mistake, I usually code alright, although this was a harder thing to do and i forgot something really simple.
|
You should realize that it is going to get a lot harder very quickly. It is important that you actually understand what you did and how this program actually works. If you don't you'll be beating your head against the wall later when you get to the chapters on streams and pointer arithmetic.
Also I hope your instructor is not teaching you this lousy coding style. Some of that is the problem of this forum, but it is no wonder you lost track of a statement with such verbose coding style.
+ Show Spoiler + char ch, change;
while (cin >> ch) {
if (isalpha(ch)) change = reverseCaesar(ch); else if (ispunct(ch)) change = convPunct(ch); else if (isdigit(ch)) change = convDigit(ch); else change = convRest(ch);
cout << change; }
Only with decent indentation. You get the idea. I know my former instructors deducted as much as 30% if your code looked extremely bad. This was a good thing because once you get a job, if your code is hard for others to maintain, you will be marginalized into unpleasant projects.
|
On October 17 2009 13:58 onmach wrote:You should realize that it is going to get a lot harder very quickly. It is important that you actually understand what you did and how this program actually works. If you don't you'll be beating your head against the wall later when you get to the chapters on streams and pointer arithmetic. Also I hope your instructor is not teaching you this lousy coding style. Some of that is the problem of this forum, but it is no wonder you lost track of a statement with such verbose coding style. + Show Spoiler + char ch, change;
while (cin >> ch) {
if (isalpha(ch)) change = reverseCaesar(ch); else if (ispunct(ch)) change = convPunct(ch); else if (isdigit(ch)) change = convDigit(ch); else change = convRest(ch);
cout << change; }
Only with decent indentation. You get the idea. I know my former instructors deducted as much as 30% if your code looked extremely bad. This was a good thing because once you get a job, if your code is hard for others to maintain, you will be marginalized into unpleasant projects.
thanks for the advice
|
|
|
|