Thank you guys so much for all the replies with constructive criticism! It is very much appreciated.
I have made a few changes to the code, following your guidelines.
1. Characters used in functions or comments are now not taken into account anymore.
2. Characters used while assigning values to variables are now not taken into account anymore either.
3. Instead of using 2 variables for each type of brace, the program now uses only 1 and adds up when it's an opening form and subtracts when it's a closing form. If the end result is not 0 a warning is printed to the user.
4. I have changed main() to int main(void)
5. I have changed b_getinput to a_getinput to avoid confusion with booleans.
6. I have changed the long line setting all integers to zero to one line per integer.
One thing I haven't changed though is the getline function suggested by MK27. The reason for this is that it uses a few things I have yet to come across in the book. My knowledge of C is still very limited and I don't want to use functions that I do not understand in an attempt to avoid learning bad habits.
The new code
Code:
#include <stdio.h>
#define MAXINPUT 32766
/* This program checks the input on rudimentary syntax errors.
-Thoughts before coding-
1. Collect the input and store it in array of character type.
2. Check the input character by character.
3. Count number of opening and closing curly brackets ('{}') and see whether they match up or not.
4. If they are not equal, return to the user that he has forgotten a curly bracket.
5. Check the number of opening and closing open brackets ('()') and see whether they match up or not.
6. If they are not equal, return to the user that he has probably forgotten an open backet.
7. Check the number of opening and closing square brackets ('[]') and see whether they match up or not.
8. If they are not equal, return to the user that he has probably forgotten a square backet.
9. Check for single and double quotes and see whether the result of adding those up is an even number.
10. If this is not the case, return to the user that he has probably forgotten a single or double quote.
11. Check whether the amount of times '*' and '/' are used directly after each other is even.
12. If this is not the case, return to the user that he has probably forgotten to close a comment block
*/
int a_getinput(char a_input[], int maxinput); // Declaring fuction
int main(void)
{
int len; // Holds length of input
int j; // Used for the loop to check array
int curl; // Holds amount of times curled brackets are used
int open; // Holds amount of times open brackets are used
int square; // Holds amount of times square brackets are used
int dquote; // Holds amount of times double quotes are used
int squote; // Holds amount of times single quotes are used
int bcom; // Holds amount of times '*' and '/' are used subsequently
int in; // Holds a value to see whether the input should be counted or not.
char input[MAXINPUT] = {0}; // Holds current input
// Setting all integers to 0
j = 0;
curl = 0;
open = 0;
square = 0;
dquote = 0;
squote = 0;
bcom = 0;
in = 0;
len = a_getinput(input, MAXINPUT); // Get value for len
if (len > 0) // If there is input
printf("\n The code you put in was:\n\n%s\nPOSSIBLE ERRORS: \n\n", input); // Print the code that was put in
for (j = 0; j <= len; j++) {
if (in == 0) { // If the value of in = 0 the code is not in (between): Quotation marks, a comment block, a single comment line nor does it come after "="
if (input[j] == '*') { // If /* is put in, add 1 to bcom and set in = 1. As the code put in hereafter until */ should not be checked.
if (input[j-1] == '/') {
++bcom;
in = 1;
}
}
else if (input[j] == '/') { // If a single line comment is specified set in = 2.
if (input[j+1] == '/'){
in = 2;
}
}
else if (input[j] == '\"') { // If the coder has put in \" set in = 3.
++dquote;
in = 3;
}
else if (input[j] == '=') { // If the coder has put in a single "=" set in = 4. if == has been put in, in = 0.
if (input[j+1] != '=')
in = 4;
}
else if (input[j] == '\'') { // If in isn't 1, 2, 3 or 4 and thus the single quote is written in a place that it has a function it adds 1 to squote
++squote;
}
else if (input[j] == '{') {
++curl;
}
else if (input[j] == '}') {
--curl;
}
else if (input[j] == '(') {
++open;
}
else if (input[j] == ')') {
--open;
}
else if (input[j] == '[') {
++square;
}
else if (input[j] == ']') {
--square;
}
}
if (in == 1) { // If a comment /* has been put in the program does not do anything with quotes, braces, brackets, parentheses etc. that are specified until */ is discovered.
if (input[j] == '*') {
if (input[j+1] == '/') {
--bcom;
in = 0; // Set in = 0 as the comment block has been closed and anything written after that has a function in the program.
}
}
}
else if (in == 2) {
if (input[j] == '\n') { // The program does not do anything with quotes, braces, brackets, parentheses etc. that are specified until a new line is discovered.
in = 0;
}
}
else if (in == 3) { // The program does not check code between quotation marks until a quotation mark followed by a closing open bracket is discovered. If we take printf for example, there is no way we can place ") without getting out of printf.
if (input [j] == '"') {
if (input [j+1] == ')') {
in = 0;
++dquote;
}
}
}
else if (in == 4) { // The program does not check code after a single '=' sign until a semicolon or newline is discovered.
if (input[j] == '=' || input[j] == ';' || input[j] == '\n') {
in = 0;
}
}
}
if (curl != 0) {
printf ("The amount of opening curled brackets minus the amount of closing curled brackets is not 0, check to see if you have forgotten to put '{' or '}' somewehere. \n\n");
}
if (open != 0) {
printf ("The amount of opening open brackets minus to the amount of closing open brackets is not 0, check to see if you have forgotten to put '(' or ')' somewehere. \n\n");
}
if (square != 0) {
printf ("The amount of opening square brackets minus the amount of closing square brackets is not 0, check to see if you have forgotten to put '[' or ']' somewehere. \n\n");
}
if ((dquote % 2)!= 0) {
printf ("The amount of double quotes you used is not an even number, check to see if you have forgotten to place a double quote somewhere.\n\n");
}
if ((squote % 2)!= 0) {
printf ("The amount of single quotes you used is not an even number, check to see if you have forgotten to place a single quote somewhere.\n\n");
}
if (bcom != 0) {
printf ("The amount of times you used /* minus the amount of times you used */ is not 0, check if you forgot to close or open a block of comments.\n\n");
}
return 0;
}
a_getinput(char s[], int lim)
{
int c, i;
for (i = 0; i < lim-1 && (c=getchar())!=EOF; ++i) // Create loop to run for as long as the amount of characters put in does not exceed the limit and EOF is not put in.
s[i] = c; // Assign the value of c to place i in the array.
++i;
s[i] = '\0';
return i;
}
Again, any constructive criticism is very much appreciated!