Post #120,432
10/8/03 3:53:42 AM
|
Well, interestingly enough
I get correct results when I do if (val1 < -127 || val1 > 127), but not when I do if ((val1 < -127) || (val1 > 127)). Which is strange; it's not what you'd think it should do.
val1 is an int.
The val2 in my earlier post was a typo when putting it into here; by that time I was getting squirrely.
--\n-------------------------------------------------------------------\n* Jack Troughton jake at consultron.ca *\n* [link|http://consultron.ca|http://consultron.ca] [link|irc://irc.ecomstation.ca|irc://irc.ecomstation.ca] *\n* Kingston Ontario Canada [link|news://news.consultron.ca|news://news.consultron.ca] *\n-------------------------------------------------------------------
|
Post #120,439
10/8/03 8:49:08 AM
|
Re: Well, interestingly enough
Are you sure you're not using '|' instead '||' ?
Copy/paste the code...
--
OK, George W. is deceptive to be sure. Dissembling, too. And let's not forget deceitful. He is lacking veracity and frankness, and void of sooth, though seemingly sincere in his proclivity for pretense. But he did not lie. [link|http://www.jointhebushwhackers.com/not_a_liar.cfm|Brian Wimer]
|
Post #120,539
10/8/03 8:48:57 PM
|
Yes, I was sure.
--\n-------------------------------------------------------------------\n* Jack Troughton jake at consultron.ca *\n* [link|http://consultron.ca|http://consultron.ca] [link|irc://irc.ecomstation.ca|irc://irc.ecomstation.ca] *\n* Kingston Ontario Canada [link|news://news.consultron.ca|news://news.consultron.ca] *\n-------------------------------------------------------------------
|
Post #120,591
10/9/03 7:07:56 AM
|
copy/paste the full code, then
--
OK, George W. is deceptive to be sure. Dissembling, too. And let's not forget deceitful. He is lacking veracity and frankness, and void of sooth, though seemingly sincere in his proclivity for pretense. But he did not lie. [link|http://www.jointhebushwhackers.com/not_a_liar.cfm|Brian Wimer]
|
Post #120,675
10/9/03 7:41:26 PM
10/10/03 12:59:17 PM
|
Sure, here's my final version
\n/* Source file for the C program in Assignment 1 of CISC221 - Computer\n * Architecture, for Dr. Clive Dove. */\n\n/* Header Declarations */\n\n#include <stdio.h>\n\n#define SIZE 80\n\nint value1, value2, sum; // variables to hold values entered by user after \n // conversion to numbers.\n\nchar instring[SIZE]; // String to hold number entered by user. Size is large \n // to ensure that there is no buffer overflow.\n\nchar error[20]; // string to hold error messages.\n\n/* Here there be function declarations. There are two functions; one to\n * convert an ascii string into an int value, and one to handle erroneous\n * input by the user. */\n\nint ascToInt(char []); // Converts ascii strings to ints.\n\nint inputOnError(char []); // Handles input outside specified range.\n\nint main()\n{\n\n while (1)\n // this loops until user puts sentinal value into value1 that causes program\n\n // to exit.\n {\n printf("Please enter a number between -128 and 127.\\nEnter zero to exit:");\n scanf("%s", instring);\n value1 = ascToInt(instring);\n if (value1 == 0)\n return 0; // Exit the program when user inputs 0 into first value.\n\n printf("Please enter another number between -128 and 127:");\n scanf("%s", instring);\n value2 = ascToInt(instring);\n\n sum = value1 + value2;\n\n printf("%x + %x = %x, sum = %d, v = %d\\n", value1 & 0xFF, value2 & 0xFF, sum & 0xFF, sum, (sum < -128 || sum > 127));\n }\n}\n\n\nint ascToInt(char *s)\n{\n int result = 0;\n int sign = 1;\n\n if (*s == '-') // Test for a sign in the first character of the string.\n\n {\n s++;\n if (!*s) // if next character is null, - is only character.\n result = inputOnError("not a number");\n else\n sign *= -1;\n }\n while (*s >= '0' && *s <= '9') // while there are digits in the string.\n\n {\n result *= 10;\n result += *s - '0';\n s++;\n }\n result *= sign; // apply the sign\n\n if (*s) // if there is anything other than a digit in the string.\n result = inputOnError("not a number");\n\n\n if (result < -128 || result > 127) // if result is outside of range.\n\n result = inputOnError("outside the range specified");\n return result;\n}\n\n/* inputOnError is the error handler for invalid input. It takes one argument\n * of a string indicating the nature of the error, prompts again for input,\n * and then uses the ascToInt function to value it, and returns the result to\n\n * the calling function. */\n\nint inputOnError(char *error)\n{\n int output;\n do\n {\n printf("The value you entered was %s.\\nPlease re-enter a number between -128 and 127:", error);\n scanf("%s", instring);\n output = ascToInt(instring);\n }\n while (output < -128 || output > 127);\n\n return output;\n} |
edit: I just wrote it... again:) This time I'm done 'cause after this I'm going to have a beer. I'd esp. like to thank Todd for giving me the knife and fork and inviting me to dine. edit2: took out my student number.
--\n-------------------------------------------------------------------\n* Jack Troughton jake at consultron.ca *\n* [link|http://consultron.ca|http://consultron.ca] [link|irc://irc.ecomstation.ca|irc://irc.ecomstation.ca] *\n* Kingston Ontario Canada [link|news://news.consultron.ca|news://news.consultron.ca] *\n-------------------------------------------------------------------
Edited by jake123
Oct. 9, 2003, 10:27:30 PM EDT
Edited by jake123
Oct. 10, 2003, 12:59:17 PM EDT
|
Post #120,746
10/10/03 8:45:17 AM
|
Works with or without parens for me
VC 6 compiler
BTW, is it OK to criticize the code? Or would you rather hear from your Professor first?
--
OK, George W. is deceptive to be sure. Dissembling, too. And let's not forget deceitful. He is lacking veracity and frankness, and void of sooth, though seemingly sincere in his proclivity for pretense. But he did not lie. [link|http://www.jointhebushwhackers.com/not_a_liar.cfm|Brian Wimer]
|
Post #120,768
10/10/03 10:49:06 AM
|
Feel free
Do realise that certain things were written in stone... like the output.
--\n-------------------------------------------------------------------\n* Jack Troughton jake at consultron.ca *\n* [link|http://consultron.ca|http://consultron.ca] [link|irc://irc.ecomstation.ca|irc://irc.ecomstation.ca] *\n* Kingston Ontario Canada [link|news://news.consultron.ca|news://news.consultron.ca] *\n-------------------------------------------------------------------
|
Post #120,824
10/10/03 4:09:28 PM
|
Re: Feel free
Well, you have a function \nint ascToInt(char []); // Converts ascii strings to ints.\n In reality, that function does nothing of the sort. It parses the string into integer, but then it checks that the integer is in range, handles the range errors, reads the string again (indirectly) an so on. How about clearly separating string to integer conversion from reading, validation and error handling? See where that takes you.
--
OK, George W. is deceptive to be sure. Dissembling, too. And let's not forget deceitful. He is lacking veracity and frankness, and void of sooth, though seemingly sincere in his proclivity for pretense. But he did not lie. [link|http://www.jointhebushwhackers.com/not_a_liar.cfm|Brian Wimer]
|
Post #121,056
10/13/03 11:59:46 AM
|
You're suggesting that
I should break the function up two or three others?
--\n-------------------------------------------------------------------\n* Jack Troughton jake at consultron.ca *\n* [link|http://consultron.ca|http://consultron.ca] [link|irc://irc.ecomstation.ca|irc://irc.ecomstation.ca] *\n* Kingston Ontario Canada [link|news://news.consultron.ca|news://news.consultron.ca] *\n-------------------------------------------------------------------
|
Post #121,062
10/13/03 12:48:50 PM
|
Basically, yes
Make the content really match the name.
--
OK, George W. is deceptive to be sure. Dissembling, too. And let's not forget deceitful. He is lacking veracity and frankness, and void of sooth, though seemingly sincere in his proclivity for pretense. But he did not lie. [link|http://www.jointhebushwhackers.com/not_a_liar.cfm|Brian Wimer]
|
Post #120,808
10/10/03 1:50:05 PM
|
Works here too, both ways. gcc 3.3.2
|
Post #120,818
10/10/03 3:25:25 PM
|
It is working here
which basically means I screwed up somewhere, but since that version of the program is long dead (I wrote it about five times), I can't really go back and see where.
--\n-------------------------------------------------------------------\n* Jack Troughton jake at consultron.ca *\n* [link|http://consultron.ca|http://consultron.ca] [link|irc://irc.ecomstation.ca|irc://irc.ecomstation.ca] *\n* Kingston Ontario Canada [link|news://news.consultron.ca|news://news.consultron.ca] *\n-------------------------------------------------------------------
|