Thread: assignment hail mary

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    6

    assignment hail mary

    I have really been working on this assignment (introductory course) for 2 weeks and it is due tonight. My probem is everytime something didn't work I changed it many times until now I look at my own code and have no idea whats going on. i realise this code is an abomination but I'm so confused I might as well be reading another language. Any help would really be appreciated. I don't want someone to do the work for me I just want to be able to do it myself.

    PHP Code:
    [ATTACH]7303[/ATTACH

  2. #2
    Registered User
    Join Date
    May 2007
    Posts
    6

    code below

    Code:
    //-----------------------------------------------------------------------------
    // Included Libraries
    #include <stdio.h>
    #include <string.h>
    //-----------------------------------------------------------------------------
    // Constants
    
    
    
    
    
    //-----------------------------------------------------------------------------
    // Function Prototypes
    //-----------------------------------------------------------------------------
    void get_password(char *);
    //-----------------------------------------------------------------------------
    // Purpose:		Get password from user
    // Arguments:	Pointer of a 6 char array to hold the password
    // Ret Value:	None
    // Pre:			User inputs five upper case alphabets (A-Z)
    // Post:		Password is written to the character array
    //-----------------------------------------------------------------------------
    void get_key(int *);
    //-----------------------------------------------------------------------------
    // Purpose:		Get integer key from user for encryption/decryption
    // Arguments:	Pointer to an integer
    // Ret Value:	None
    // Pre:			Positive integer less than 2147483637 (INT_MAX) as key
    // post:		Key is written to memory the integer points at
    //-----------------------------------------------------------------------------
    void encrypt(char *, int *);
    //-----------------------------------------------------------------------------
    // Purpose:		Encrypts a five upper case alphabetic (A-Z) password
    // Arguments:	Pointer of char array holding the plain password
    //				Pointer of integer holding the key for encryption
    // Ret Value:	None
    // Pre:			None
    // Post:		Password in char array is encrypted with the key
    //-----------------------------------------------------------------------------
    void decrypt(char*, int *);
    //-----------------------------------------------------------------------------
    // Purpose:		Decrypts a five upper case alphabetic (A-Z) password
    // Arguments:	Pointer of char array holding the encrypted password
    //				Pointer of integer holding the key for decryption
    // Ret Value:	None
    // Pre:			None
    // Post:		Encrypted password in char array is decrypted with the key
    //-----------------------------------------------------------------------------
    
    
    int main() {
    	char pass_input [6];
    	char *passptr; 
    	int key_input;
    	int *keyptr; 
    	get_password(passptr);
    	get_key(keyptr);
    	encrypt(passptr, keyptr);
    	printf("passptr: %s\n", passptr);
    
    
    
    
    
    
    
    
    
    }
    
    
    
    
    
    
    //-----------------------------------------------------------------------------
    // Functions
    //-----------------------------------------------------------------------------
    
    void get_password(char * passptr) {
    	char pass_input [6];
    	char passptr = pass_input; 
    	int key_input;
    	int *keyptr = &key_input;
    	
    	printf("Please enter five upper case letters:\n");
    	scanf("%5c", passptr);
    	
    	// Flush standard input
    	scanf("%*[^\n]");
    	scanf("%*c");
    	
    	
    }
    
    
    void get_key(int * keyptr) {
    	char pass_input [6]; 
    	int key_input;
    	int *keyptr = &key_input;
    	printf("Please enter five numbers:\n");
    	scanf("%i", &*keyptr);
    
    	// Flush standard input
    	scanf("%*[^\n]");
    	scanf("%*c");
    	
    }
    
    void encrypt(char *, int * passptr, keyptr) { 
    	char pass_input [6];
    	char *passptr = &pass_input; 
    	int key_input;
    	int *keyptr = &key_input;
    	passptr[0] += (*keyptr%26);
    	passptr[1] += (*keyptr%26);
    	passptr[2] += (*keyptr%26);
    	passptr[3] += (*keyptr%26);
    	passptr[4] += (*keyptr%26);
    	
    
    
    
    }
    
    void decrypt(char*, int * passptr, keyptr) {
    	char pass_input [6];
    	char *passptr = pass_input; 
    	int key_input;
    	int *keyptr = &key_input;
    	passptr[0] -= (*keyptr%26);
    	passptr[1] -= (*keyptr%26);
    	passptr[2] -= (*keyptr%26);
    	passptr[3] -= (*keyptr%26);
    	passptr[4] -= (*keyptr%26);
    
    }

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Odd how you have posted it as a C++ source file and the code is C.

    There are lots of errors in that code, 15 according to my compiler. I would look through it but I dont have time right now. Other people may be able to help you though.

    One point though - You dont need to use so much whitespace in your layout. But I did like the use of your comments and well done for explaining the workings of each function.
    Double Helix STL

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    As was said, one problem you're probably having is that you saved it as a .cpp file, which means it'll be interpretted as a C++ file by some compilers (like GCC).

    Looking through your code, though, it's in C, so I changed the name of it to assignment4.c. Here's the error list:

    assignment4.c:2:1: warning: C++ style comments are not allowed in ISO C90
    assignment4.c:2:1: warning: (this will be reported only once per input file)
    assignment4.c: In function `main':
    assignment4.c:55: error: `passptr' undeclared (first use in this function)
    assignment4.c:55: error: (Each undeclared identifier is reported only once
    assignment4.c:55: error: for each function it appears in.)
    assignment4.c:56: error: `keyptr' undeclared (first use in this function)
    assignment4.c:53: warning: unused variable `pass_input'
    assignment4.c:54: warning: unused variable `key_input'
    assignment4.c: In function `get_password':
    assignment4.c:81: error: 'passptr' redeclared as different kind of symbol
    assignment4.c:79: error: previous definition of 'passptr' was here
    assignment4.c:81: warning: initialization makes integer from pointer without a cast
    assignment4.c:86: warning: format argument is not a pointer (arg 2)
    assignment4.c:83: warning: unused variable `keyptr'
    assignment4.c: In function `get_key':
    assignment4.c:99: error: 'keyptr' redeclared as different kind of symbol
    assignment4.c:96: error: previous definition of 'keyptr' was here
    assignment4.c:97: warning: unused variable `pass_input'
    assignment4.c: At top level:
    assignment4.c:109: error: syntax error before "keyptr"
    assignment4.c: In function `encrypt':
    assignment4.c:109: error: number of arguments doesn't match prototype
    assignment4.c:32: error: prototype declaration
    assignment4.c:111: warning: initialization from incompatible pointer type
    assignment4.c: At top level:
    assignment4.c:125: error: syntax error before "keyptr"
    assignment4.c: In function `decrypt':
    assignment4.c:125: error: number of arguments doesn't match prototype
    assignment4.c:41: error: prototype declaration
    Looking through your code, the problem is that you seem to lack the understanding of how to use function parameters. You're making up variables instead of using what you're given. Don't do that. Read a good tutorial on functions and parameters.

    I rewrote your main() method to this:

    Code:
    int main(void)
    {
    	char pass_input[6];
    	int key_input;
    
    	get_password(pass_input);
    	get_key(&key_input);
    	encrypt(pass_input, &key_input);
    	printf("pass_input: &#37;s\n", pass_input);
    
    	return 0;
    }
    Try to understand why I made the changes that I did.

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    6

    thank you for replying

    Yes the function prototypes are commented well but this was provided by my instructor (thats why their the only thing in there that look good). The course in c I am doing is supposed to be totaly compatible with C++ so maybe thats why it looks like C++?

  6. #6
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Where you have the void functions at the end, I see "char *, int *..." in the list of parameters. One potential problem is that you didn't specify the name of the pointer to the char.

    And if this is C++ code instead of C, this thread should be moved to the C++ forum.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by ulillillia View Post
    And if this is C++ code instead of C, this thread should be moved to the C++ forum.
    It isn't. It's C code, but apparently saved in a .cpp file. The filename should be changed to have a .c extension.

  8. #8
    Registered User
    Join Date
    May 2007
    Posts
    6
    All our work has to be saved as .cpp It is part of the rquirements of the assignment.

  9. #9
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Thats sounds odd. If you are learning C, you should save it as a C file. But if that is what you were told to do then thats fine I guess. I know C++ has backward compatability with C but like MacGyver said some compilers interperet the code slightly differently depending on the extension
    Double Helix STL

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I remember the "Hall Mary":

    Hall Mary, full of opening doors, heaven knows I've been drinking.

    Blessed am I by the one's that don't knock me off my unstable feet.

    And blessed are the opener's who swing their doors open, carefully.

    I pray I'll find my dorm room, before I die, or have to throw up one more time.

    Thanks for reminding me why I gave up drinking!


  11. #11
    Registered User
    Join Date
    May 2007
    Posts
    6
    Could someone maybe give me an explanation of function parameters (in regards to how mine are wrong). I took the advice and went back to my textbook to read it again but my skull must be a bit thick and it wont seem to sink in.

  12. #12
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Here's a simple example of function parameters being used:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    float degrees(float input) // this function accepts a float variable type called "input" as a parameter
    {
    	return(input/57.2957795f);
    }
    
    int main(void)
    {
    	float BaseResult;
    	float MainResult;
    	
    	BaseResult = sin(60.0f); // this uses radians by default and should show 0.866 if in degrees
    	MainResult = sin(degrees(60.0f)); // calls the degrees function above with the parameter of 60
    	
    	printf("BaseResult is &#37;.3f and MainResult is %.3f.", BaseResult, MainResult)
    	
    	return 0;
    }
    When calling the degrees function, it uses the parameter you give it. Let's walk through:

    1. You call the function as "degrees(60.0f);". You are passing the number 60 as a floating point constant.
    2. When the function is processed, the input that it receives, 60, is put into the formula. It returns a float that equals input/57.2957795, or 60.0/57.2957795 which is slightly over 1 for a bit more than one radian. Try putting another number into the parentheses of the degrees function call and see what happens. 30 should give exactly 0.5 (or very close to it anyway).

    Try looking at my "LoadFile" function I use with my "Interactive Animation" program I made. It takes 8 parameters (as I currently have it) - some normal variables, some pointers, even a string.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  13. #13
    Registered User
    Join Date
    May 2007
    Posts
    6
    I think the most difficulty i am having is where you actually write the function (not prototype) for instance:

    PHP Code:
    void encrypt(char *, int * ) 
    I dont fully understand what should be put after int* (char*, int* xxxxxxxx)

    The way I originally did it My instructor wrote

    //you must name your arguments with a name.
    //you should use the argument not a local variable

  14. #14
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I think you're having trouble with the relationship between arrays and pointers. They both seem to act the same at times.

    encrypt() takes a char * and an int *. Remember a pointer is just simply a variable that contains an address of another variable of its base type. That means an int * is just a variable that contains an address of an int.

    Now arrays, when passed to functions, are not pass as entire blocks of memory. That would be inefficient to recopy an entire array. When you pass an array to a function, it really passes a pointer to the first element of the array. This is why encrypt() accepts a char *, when in reality you're passing it an array. You're supposed to pass it an array of 6 chars. The int * for this function is a pointer to an int that contains the key.

    This is why I wrote the following in main():

    Code:
    encrypt(pass_input, &key_input);
    Switching gears a little bit, function prototypes are used simply to let the compiler know that a function is going to occur later in the code, and what it accepts and what it returns. This helps you, the programmer, from making errors later on, as well as helping the compiler keep track of things better.

    The important part of function prototypes are the data types that it accepts and returns. Since the variable names are unimportant, they can be excluded. When you define the function, however, you must name the parameters.

    Code:
    void encrypt(char *, int *); /* function prototype */
    
    ...
    
    void encrypt(char *szMsg, int *key) /* Note that names are given.*/
    {
    	.... /* function definition */
    }
    When a variable is given to a function, only a copy of that variable is given. This is why pointers are important. You can only give the contents of a variable normally, but with pointers, you give the address of another variable, which means you can alter variables from calling functions.

  15. #15
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Look at the start of my LoadFile function. It takes two pointers as parameters. Note the formatting as well.

    Code:
    char LoadFile(const char FileName[64], char FileType, BITMAPINFOHEADER *BMPInfo, unsigned char *BMPData, char FogUsed, double ObjectScaling, char LoadType, unsigned int ArrayLength)
    
    ...
    
    LoadFile("MountainsBG.tga", 2, &MountainsInfo, MountainsData, 1, Mountains.Scaling, 1, sizeof(MountainsData));
    The top part is how the function is for the declaration of it and it's parameters. The bottom part is how it is called. What you're missing is the name. The name can be anything. Look at my LoadFile function and how it's used to help understand, and what the various parameters are for.

    The first parameter is a string - a file name for an image I want to load.

    The second parameter is to let me know what type of file is being loaded so I set the fields in the bitmapinfoheader structure correctly. 1 is for BMP which is used for 24-bit BMP images. 2 is for TGA images which are 32-bit (in my case anyway) and are read differently.

    The third parameter is a pointer to a structure, the bitmapinfoheader structure Windows uses.

    Parameter number 4 is a pointer to the array containing the image pixel color data. Because the array is defined as an unsigned char, the pointer must reference an unsigned char which is why I have it like that.

    The last 4 parameters are just general variables. FogUsed, generally a multi-option flag, is to let me know if the image should be altered to account for adding the fog. ObjectScaling is the distance of the object, of which is used to determine the fog's intensity in the fog formula. LoadType is a basic flag to tell me if the image's related structure is to be filled out or just the image data is to be reread. The last parameter, ArrayLength, is used to prevent accessing a an array index outside the allowed bounds, which is basically the size of the image data array.

    Look at my simple example I've given and look at how it's used. I have "float input" as the parameter. This means that the parameter is a float and it is named as input. You're just missing the name. Names in function parameters can be reused.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Help with a pretty big C++ assignment
    By wakestudent988 in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2006, 09:46 PM
  4. Replies: 1
    Last Post: 10-27-2006, 01:21 PM