Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

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.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-16-2008, 05:14 AM
trevordunstan trevordunstan is offline
Newbie
 
Join Date: May 2008
Posts: 3
Rep Power: 0
trevordunstan is on a distinguished road
Default Ceasar shift program troubles

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);
          
         
         }
         
          
          


}
my problem is that when I enter a space it prints a space followed by the previous shifted character then the correct shifted output. eg

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 09-16-2008, 11:46 AM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,418
Last Blog:
wxWidgets is NOT code ...
Rep Power: 37
WingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to behold
Default Re: Ceasar shift program troubles

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 09-17-2008, 05:40 AM
trevordunstan trevordunstan is offline
Newbie
 
Join Date: May 2008
Posts: 3
Rep Power: 0
trevordunstan is on a distinguished road
Default Re: Ceasar shift program troubles

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

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


All times are GMT -5. The time now is 02:14 AM.

Contest Stats

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

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads