It looks like you're using an Ad Blocker.

Please white-list or disable AboveTopSecret.com in your ad-blocking tool.

Thank you.

 

Some features of ATS will be disabled while you continue to use an ad-blocker.

 

Noob to c programming help!

page: 1
0
<<   2 >>

log in

join
share:

posted on Dec, 9 2005 @ 09:05 PM
link   
This is a simple casear cipher encode only portion that i wrote. it:

1. Promtps for user input
2. Asks for the shift value
3. Uses the shift value such as 2 on the first letter then increments it by 1 for each letter after that.
EXAMPLE: Hello would be Jhpqu


My problem is at the end of each encrypted word it prints a funny character that i dont need. How can i eliminate it??thanks

For some reason its is cuttin parts of the beginning of my code out so this is it.

Its messing up the first 2 lines of code after the inlcude and it wont let me fix it,


Code:



#include
#include






/*encode function, to encode user input */
int main()
[
char buff[BUFSIZ];
int i = 0;
int shift_value;
int p = 0;

printf( "**initializing.....**\n\n"
"**linking..........**\n\n");
printf( "Welcome agent Brown\n\n");
printf( "You have accessed the Caesar Cipher program\n\n");
printf("\nPlease enter the text you wish to encrypt: ");/*accept user input */
fgets(buff, sizeof(buff), stdin);


printf("\nEnter your encryption shift value (anything from +-1 to 25): ");
scanf ("%i", &shift_value);

[
while ( buff != '\0' )
[

buff = buff + shift_value + i++;

]

]

printf("\n Your encrypted text is:%s\n",buff);/*return encrypted text */


return 0; /* indicate successful completeion */
]



[edit on 9-12-2005 by Uber Fr0g]

[edit on 9-12-2005 by Uber Fr0g]

[edit on 9-12-2005 by Uber Fr0g]

[edit on 9-12-2005 by Uber Fr0g]



posted on Dec, 9 2005 @ 09:07 PM
link   
double post

[edit on 12/9/2005 by djohnsto77]



posted on Dec, 9 2005 @ 09:15 PM
link   
If you're using windows I think you should use "\r\n" instead of "\n"



[edit on 12/9/2005 by djohnsto77]



posted on Dec, 10 2005 @ 12:21 AM
link   

Originally posted by djohnsto77
If you're using windows I think you should use "\r\n" instead of "\n"
[edit on 12/9/2005 by djohnsto77]


Technically correct, but this is going to a console--printf, fgets, etc. A "\n" should be fine; maybe don't quote me on that, but at worse you'd just get no line breaks.

Just curious, but was the last part supposed to look like this (without spaces next to the brackets):


while ( buff[ i ] != '\0' )
[

buff[ i ] = buff[ i ] + shift_value + i++;

]


I'm not an expert at C, but it looks like the reason you're getting the strange character is because you're converting some potentially strange integer values into chars. Are you checking anywhere as to whether

buff[ i ] + shift_value + i++

comes up after Z (or lowercase, can't remember...) if you convert it? Try throwing in a couple of printf's in there to track the value of shift_value, i, and buff[ i ] (the original value, not the changed version), might help you see what's going wrong.

Then again, I could be completely out in left field
Just hope it helps somehow.



posted on Dec, 10 2005 @ 12:59 PM
link   
First of all congratulations on learning the best programming language out there. (IMHO)

Every program you want to make, no matter how big or small can be broken down into smaller little tasks. You must follow this rule or you'll drive yourself crazy trying to figure the whole thing out.

In that train of thought we first need to tell the user hello. Pretty simple stuff. You can use printf if you wish but since we don't have any variables to output I'm just using the more simple puts function.



// Say hello
puts("**initializing.....**\n\n**linking..........**\n\n");
puts("Welcome agent Brown\n\n");
puts("You have accessed the Caesar Cipher program\n\n");
puts("Please enter the text you wish to encrypt: ");


Now that we have the user's attention we need to get their encryption text. Again my preference here is scanf. You'll find that there are lots of different ways of doing the same thing. If you get the same result then don't worry about it. scanf is like the opposite of printf.


// Get input from the user
// It is possible that the user may exceed our allocated space.
// We could address that but it would cloud the issue. TODO: for later
scanf("%s",&buff);

puts("\nEnter your encryption shift value (anything from +-1 to 25): ");
scanf("%d",&shift_value);

// We now have a string and a shift value
// TODO: Check for empty string and out of range value


// Determine how long the string is
encrypt_text_length = strlen(buff);


Now all that's left to do is encrypt the text.


// Encrypted the text
for(i = 0; i < encrypt_text_length; i++)
[
buff = ShiftCharacter(buff,shift_value,i);
]

// Display the encrypted text for the user
printf("\nYour encrypted text is: %s",buff);

return 0; /* indicate successful completeion */


But wait! How did that work? Is there a magic function that shifts the characters for us and wraps when we go past z? Well there is if you make one. This is what I was saying about seperating the different portions of the problem. Also, since we solve this issue in a function we can take this code and reuse it in another program if we wish.



// This function will shift the character by the determined amount
// For our purpose we are only concerned with the letters a - z
char ShiftCharacter(char unencrypt,int shift_value,int offset)
[
char encrypted_char = unencrypt;
int i;

// Capital A - Z
if(unencrypt >= 'A' && unencrypt 'Z')
[
encrypted_char = 'A';
]
]

]

// Lowercase a - z
else if(unencrypt >= 'a' && unencrypt 'z')
[
encrypted_char = 'a';
]
]
]

return encrypted_char;

]



That's all there is too it. The only thing that bothered me when I looked at the result is that the function to shift the characters was sort of clumsy and awkward. It's not a big deal if you're only shifting 2 or 25 spaces but what if you wanted to shift 100,000 places. It could really slow this program down. I came up with a solution. that reduces the calculation to a fixed number of steps no matter how many spaces you shift. I'll include that below when I show everything at once. To understand how this works you need to know what the '%' (Called modulus) does. It basically gives you the remainder in a division problem.

Well here's everything together. If this is your homework I'm just going to say that you really need to understand how this works on your own. I can't take your test for you. Seeing examples of working code is one of the best ways to learn but you have to try this out on your own. Practice, practice, practice



// casear_cipher.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include
#include

// Declare the function
char ShiftCharacter(char unencrypt,int shift_value,int offset);


int main(int argc, char* argv[])
[

char buff[1024];
int shift_value;
int encrypt_text_length;
int i;

// Say hello
puts("**initializing.....**\n\n**linking..........**\n\n");
puts("Welcome agent Brown\n\n");
puts("You have accessed the Caesar Cipher program\n\n");
puts("Please enter the text you wish to encrypt: ");

// Get input from the user
// It is possible that the user may exceed our allocated space.
// We could address that but it would cloud the issue. TODO: for later
scanf("%s",&buff);

puts("\nEnter your encryption shift value (anything from +-1 to 25): ");
scanf("%d",&shift_value);

// We now have a string and a shift value
// TODO: Check for empty string and out of range value


// Determine how long the string is
encrypt_text_length = strlen(buff);

// Encrypted the text
for(i = 0; i < encrypt_text_length; i++)
[
buff = ShiftCharacter(buff,shift_value,i);
]

// Display the encrypted text for the user
printf("\nYour encrypted text is: %s",buff);


return 0; /* indicate successful completeion */
]


// This function will shift the character by the determined amount
// For our purpose we are only concerned with the letters a - z
char ShiftCharacter(char unencrypt,int shift_value,int offset)
[
char encrypted_char;
int alphanumber; // The character as a number ( A = 1, B = 2, Z = 26 etc.)

// Capital A - Z
if(unencrypt >= 'A' && unencrypt = 'a' && unencrypt



posted on Dec, 10 2005 @ 04:17 PM
link   
Wow thanks for all the great responses. I understand almost everything. The one part i am unfamiliar with is the "puts" portion. What is the difference between puts and printf? Is it just, Using printf is better if you are going to recieve input, and puts is better when you do not recieve input from the user? thanks



Oh yeah, and what is this portion of code for??

#include "stdafx.h"


It's causing a fatal error. Also, i would like to add that was not homework, Just a program my friend had worked on a few weeks ago and i wanted to attempt to get a little more practice before finals lol. There are a few more things i want to try ad add to this program but i am going to attempt them on my own before bothering all fo you with it.

Getting funny errors when i take that out to:
error C2664: 'ShiftCharacter' : cannot convert parameter 1 from 'char [1024]' to 'char'

warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data



[edit on 10-12-2005 by Uber Fr0g]


[edit on 10-12-2005 by Uber Fr0g]

[edit on 10-12-2005 by Uber Fr0g]



posted on Dec, 10 2005 @ 04:58 PM
link   
O yeah, should be




buff = ShiftCharacter(buff,shift_value,i);


And you don't need the #include stdafx.h



posted on Dec, 10 2005 @ 05:31 PM
link   


buff = ShiftCharacter(buff,shift_value,i);



^^ thats the same thing???



posted on Dec, 10 2005 @ 05:38 PM
link   
I think it's supposed to be



buff[ i] = ShiftCharacter(buff[ i],shift_value,i);


The BBCode is taking out the subscripts (it thinks you want italic)



posted on Dec, 10 2005 @ 06:47 PM
link   
ooh, that was it, im not used to the bbcode.


Quick question, when i type more than one word, example: hello world

with spaces the program crashes. Same for punctuation. Is there a way to let it accept spaces and punctuation??



posted on Dec, 10 2005 @ 06:52 PM
link   

Originally posted by Uber Fr0g
with spaces the program crashes. Same for punctuation. Is there a way to let it accept spaces and punctuation??


I'm imagining this is some kind of school project...did you get any guidance/specification on how to handle them or any handling of when a character goes out of the alphabet due to shifting?

[edit on 12/10/2005 by djohnsto77]



posted on Dec, 10 2005 @ 07:09 PM
link   
No not a school project, my friend did a caesar cipher complete program similar to this a while back though. Im just attempting some of the programs he did in his class to help me learn a bit more before the final in my class. As far as your question, we havent really done alot with the ascii chart and all. Thats why im asking.



posted on Dec, 10 2005 @ 07:30 PM
link   
Well then it's up to you. You could give an error if a nonalphabetical character is entered or rotate around the entire ascii table instead of just the alphabet.



posted on Dec, 10 2005 @ 08:29 PM
link   
ahhh, ill give it a shot.



posted on Dec, 10 2005 @ 10:47 PM
link   

Originally posted by Uber Fr0g
ooh, that was it, im not used to the bbcode.


Quick question, when i type more than one word, example: hello world

with spaces the program crashes. Same for punctuation. Is there a way to let it accept spaces and punctuation??


Well, I'll give you a hint. The function that shifts the character returns the value of "encrypted_char". What is that value if it's not between A and Z? The answer is ????? who knows. It's uninitialized which means it could be anything. Shame shame on me for not actually testing what I wrote. If you initialize it to the value of the unencrypted character first then if it's not between a and z it will just return what what was passed in to the function and the value will not change.

I'll try to be more careful in the future with the bbcode. I forget about that sometimes.



posted on Dec, 11 2005 @ 05:31 PM
link   
hmm, ive been attempting to get the program to ignore anything other than a-z but still print them, such as commas. Is this possible or am i trying to empty a boat full of water with a hole in it?



posted on Dec, 11 2005 @ 09:00 PM
link   
Just initialize encrypted_char to the original character and if it doesn't fall between a and z it will just return the original character.

Let's say that the unencrypt value is dollar sign '$'



char ShiftCharacter(char unencrypt,int shift_value,int offset)
[

// Initialize the return value to the original value
char encrypted_char = unencrypt;

if(unencrypt >= 'A' && unencrypt



posted on Dec, 13 2005 @ 07:09 PM
link   
hey, ive been playin with what you said a bit. But, it still skips straight past the part of the program where i ask a shift value from the user, whenever i enter moe than one word to be encrypted.



posted on Dec, 13 2005 @ 08:10 PM
link   
If you want to get more than one word at a time you'll have to read the text in as a stream;

fgets(buff, sizeof buff, stdin);

will replace

scanf("%s",&buff);

I believe you were doing this correctly at first. Sorry if we sidetracked you.



posted on Dec, 14 2005 @ 07:53 PM
link   
yeah, i fixed it. Ive been trying to reset the shift value for every word entered, i tried to test for whitespaces in bewtween words and when the program encountered them to restart the shift value for each word, but it was so jumbled it messed everything up. Is there a simple way to loop this?

[edit on 14-12-2005 by Uber Fr0g]

[edit on 14-12-2005 by Uber Fr0g]



new topics

top topics



 
0
<<   2 >>

log in

join