Click here to Skip to main content
15,885,915 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Help with a program that can respond to user inputs.. I'm a beginner in C and would want to know where I am going wrong in regards to syntax and if my conditionals and loops should be changed.
My Skeleton code is as follows;
C++
char *input_strings[] =
{ "Hello", "What is your name?", "How are you?" };
int main(int argc, char **argv)
//infinite loop
	while (1) {
 // get the input string
	printf("> ");
    fgets(input,100,stdin);
     ............ //This is where I created my own code. 
    if (quit_condition) {
 break;

I cannot use any built-in function except StringCMPI which I've done. I would just want clarification on what's wrong.
Thanks.

What I have tried:

C++
#include <stdio.h>
#include <stdlib.h>
char *input_strings[] =
{ "Hello", "What is your name?", "How are you?" };
int main(int argc, char **argv)
char toupp (char ch)
{
	if (ch<=97&&ch<122)
	{
		ch = ch-32;
	}
  return ch;
	}
	int stringCmpi(char*s1,char*s2)
	{
	int i=0, diff =0;
	for(i=0; s1[i]!='\0';i++)
	{
		if (toupp(s1[i]!=toupp(s2[i]))
		{
		return 1;}
		}
		return 0;
		}
{
	char input[100];
 // infinite loop
	while (1) {
 // get the input string
	printf("> ");
	int a = 0, b = 0;
	while (input[a]!='\0'){
	if input (input[a]=='\0''){
		int temp = b +1;
		if (input[temp]!='\0'){
	while(input[temp]=='\0'&&input[temp]!='\0'){
		if input[temp]=='\0'){
			a++;
		}
		temp++;
	    }
		}
		}
		blank[c] = input [b];
		b++
		c++
	}
	if((stringCmpi(input_strings[0],blank))==0)
{
		printf("Hello\n");
}
	else if((stringCmpi(input_strings[1],blank))==0)
{
		printf("My name is NeoNath\n");
}
	else if((stringCmpi(input_strings[2],blank))==0)
  
{		printf("I am fine\n");
  
}
	else if((stringCmpi("BYE",blank))==0)
{
		printf("See you later\n");
break;
}
	else
{
		printf("I do not understand\n");
}
 if (quit_condition) {
 break;
 }
 }
}
Posted
Updated 23-Mar-21 1:46am
v2
Comments
Richard MacCutchan 21-Mar-21 5:46am    
	if (ch<=97&&ch<122) // error you mean greater than the first
	{ 
		ch = ch-32;
	}

Don't use numbers to represent characters as they are difficult to understand. Use proper characgter constants:
	if (ch >= 'a' && ch <= 'z') // test for lower case
	{
		ch = ch - 0x040; // show the proper hex value
	}

The C language does not support "nested" functions, so this will not compile:
C++
int main(int argc, char **argv)
char toupp (char ch)
{
	if (ch<=97&&ch<122)
	{
You need to declare the toupp function outside the body of the main function:
C++
char toupp (char ch)
{
	if (ch<=97&&ch<122)
	{
    ...
    }
...
}

int main(int argc, char **argv)
{
...
}
As for the rest of it ... sort your indentation out so it's readable - that's not clear at all!
When you get bits like this:
C++
for(i=0; s1[i]!='\0';i++)
	{
		if (toupp(s1[i]!=toupp(s2[i]))
		{
		return 1;}
		}
		return 0;
		}
{
	char input[100];
It is not at all clear what you meant to have happen!
 
Share this answer
 
Comments
Member 15084336 20-Mar-21 16:41pm    
Thanks, I'll try that.
I prefer this function because the logic is a little more clear.
inline int ToUpper( int ch )
{
    const int diff = 'a' - 'A';
    if( ( ch >= 'a' ) && ( ch <= 'z' ) )
        ch -= diff;
    return ch;
}
 
Share this answer
 
Comments
Member 15084336 22-Mar-21 19:33pm    
Great, I've used this and have a more updated solution but with a slight isssue.
A nifty way to change case is to use boolean operations. The difference (in ASCII) between UC and LC is a single bit that it toggled. Interestingly that bit is the value of the space character ASCII= 0x100000 (i.e., 32 decimal).

So - you use code like this:

C++
if(isalpha(theChar))
   theChar |= 32;         // sets the bit (to LC)
or
   theChar &= 0x11011111; // clears bit: shown as hex to clarify (to UC)
or
   theChar ^= 32;  // swaps all UC <=> LC

now you have to use home-spun conditionals instead of isalpha() and this have to check the range of theChar to put it between 'a' and 'z', inclusive. I used the isalpha() to give you a bit more real-life with a straight-forward and readable solution that's more flexible.
 
Share this answer
 
Comments
Richard MacCutchan 23-Mar-21 8:07am    
theChar &= 0x11011111;

I don't think that is what you meant.
#include <stdio.h>
#include <stdlib.h>

char *input_strings[] = { "Hello", "What is your name?", "How are you?" };
inline int toupper(int ch)
//Upper case converter function.
{
	const int diff = 'a' - 'A';
	if ((ch >= 'a') && (ch <= 'z'))
		ch -= diff;
	return ch;
}

int stringCmpi(char *s1, char *s2)
	{
		int i = 0, diff = 0;
		for (i = 0; s1[i] != '\0'; i++)
		{
			if (toupper(s1[i]) != toupper(s2[i]))
				{
					return 1;
				}
				
		}
				return 0;
	}

			int main(int argc, char **argv)
			{
				char input[100],blank[100];
				// infinite loop
				while (1)
				{
					// get the input string
					printf("> ");
					fgets(input, 100, stdin);
					int a = 0, b = 0;
					while (input[a] != '\0')
					{
						if (input[a] == '\0')
						{
							int temp = a + 1;
							if (input[temp] != '\0')
							{
								while (input[temp] =='\0' && input[temp] != '\0')
								{
									if (input[temp] =='\0')
								{
									a++;
								}

								temp++;
							}
						}
					}

					blank[b] = input[a];
					a++;
					b++;
				}

				blank[b] = '\0';
				//----------------
				//--------------
				if ((stringCmpi(input_strings[0], blank)) == 0)
				{
					printf("Hello\n");
				}
				else if ((stringCmpi(input_strings[1], blank)) == 0)
				{
					printf("My name is JustinNgome\n");
				}
				else if ((stringCmpi(input_strings[2], blank)) == 0)

				{
					printf("I am fine\n");

				}
				else if ((stringCmpi("BYE", blank)) == 0)
				{
					printf("See you later\n");
					break;
				}
				else
				{
					printf("I do not understand\n");
				}
			}

			return 0;

}

Updated Solution; Code runs, yet it's not prompting the user "> " and I will enter a user input such as Hello. 
gcc -o HW3 HW3.c
Instead of receiving > 
I just receive the normal command line of: $ which doesn't take in user inputs such as strings.</stdlib.h></stdio.h>
 
Share this answer
 
Comments
Richard MacCutchan 23-Mar-21 8:01am    
I compiled the above but whatever I enter it just say "I do not understand", which is not helpful.

Your main method is overcomplicated, but since I am not sure what it is supposed to do I cannot correct it. You have the following line:
                    while (input[temp] =='\0' && input[temp] != '\0')


which also makes no sense. A charachter cannot be equal to '\0' and at the same time not equal to it.

You need to write out on paper the steps that the program is supposed to take. At a rough guess it is likely to be something like:
WHILE TRUE
Display the prompt message
Read the reply
Do something with the reply : no idea what it is supposed to be
Display an appropriate message
End WHILE
Member 15084336 23-Mar-21 22:21pm    
The code works, I just had an issue with the command line.Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900