|
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
|
|
|
|