|
||||||
| C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
Hey there guys,
I am writing a ceasar shift program for uni and I am having a little trouble. I have essentially written the program and it works except for a couple of small details which I am really finding hard to get around. Here is the code for the function that accepts the input then applies the ceasar shift Code:
void decryptEncryptLine(int shift) {
char input;
char output;
const char SENT = '\n';
printf("Please enter characters to be encrypted/decrypted\n\n");
scanf("%c", &input);
while (input != SENT){ // while input is not '\n'
if (isalpha(input)) {
input = (toupper(input)); // convert lowercase to uppercase
output = input + (shift); // apply ceasar shift
if (output > 90) { // encode wrap around
output -= 26;
}
else if (output < 65) { // decode wrap around
output += 26;
}
}
else if (input == ' ') {
printf("%c", input);
}
printf("%c",output);
scanf("%c", &input);
}
}
input = I like to eat pizza output = L LOLNH HWR RHDW WSLCCD The characters in red are not supposed to be there. The thing is that I understand why it is happening it is because I have 2 printfstatements the problem is that i just dont know the best way to fix it. I have been sitting here for days trying different ways to fix it but i just cant seem to crack it, even though i think it is just something simple that i am over looking. Could someone run the code and suggest a way that I can get the function to accept characters but only apply the ceasar shift to alpha characters but ignore spaces and numbers ie the numbers and spaces should be output but not shifted. I have only been studying C for a couple of months now so any advice would be hugely appreciated. thanks trev |
| Sponsored Links |
|
|
|
|||||
|
What's happening is that when the code encounters a space, it processes TWO printf statements, one for the space, and one for the previous output. I would move the printf("%c",output); inside your if block.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum Programming is a branch of mathematics. |
|
|||
|
Thanks heaps man,
I knew it was something simple that I was overlooking, didnt quite realise it was that simple though. Something that I should have picked up. The program now works as it should do and I think im going to cry ![]() Cheers man ! Trev |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Tutorial: Starting C# with C# 2008 Express Edition | Jordan | CSharp Tutorials | 19 | 08-08-2008 01:48 PM |
| [HELP] C++ program that will ask the user to enter not greater than to 15 numbers | Kyram143 | C and C++ | 7 | 08-06-2008 10:53 PM |
| Help with Square root and calculator program!!! | 123456789asdf | C and C++ | 10 | 12-02-2007 05:35 PM |
| WingedPanther | ........ | 2753.6 |
| Xav | ........ | 2704 |
| Brandon W | ........ | 1702.32 |
| John | ........ | 1207.73 |
| marwex89 | ........ | 1175.24 |
| morefood2001 | ........ | 966.05 |
| dcs | ........ | 655.75 |
| Steve.L | ........ | 475.59 |
| orjan | ........ | 418.58 |
| Aereshaa | ........ | 383.54 |