I have 2 problems. One problem is the fact that in my for-loop I just used a constant, 3. This is fine for this specific program, though if I were to add more names in the future I would be required to correspondingly update the constant everytime. My first question then, is what should I use to represent the amount of items in the index at the 3 instead of just using a constant if possible.
My second problem is that I would like this program to only accept the names that I have placed in the name index. If the user inputs an incorrect name, I would just like it to print "Please input valid name" and then prompt the user for another string which will run through the original program of determining a names number.
This is my code feel free to leave your thoughts. Thank you!
long number[] = {8178895397, 8178895398, 8178895399};
string name[] = {"Jake", "Dylan", "Gabe"};
int main(void)
{
string in_name = get_string("name of person\n");
for(int i = 0; i < 3; i )
{
if(strcmp(in_name, name[i]) == 0)
{
printf("%ld", number[i]);
{
printf("\n");
return true;
}
}
}
}
CodePudding user response:
I don't have the CS50 package on my system, but following is version of your code with generic character array and string functionality that addresses the possible change in name count along with providing a "while" loop to allow repeated tries at entering a valid name or exiting if need be.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NELMS(A) (sizeof(A) / sizeof(A[0]))
long number[] = {8178895397, 8178895397, 8178895398, 8178895399};
char* name[] = {"Jake", "Dylan", "Gabe", "Alfonz"}; /* Using a character array in lieu of string declaration - do not have CS50 */
int main()
{
int loop = 1; /* Will be used to break out of the "while" loop */
while(loop)
{
//string in_name = get_string("name of person\n"); /* Using in lieu of string declaration - do not have CS50 */
char in_iname[32]; /* An arbitrary string length for a name */
printf("Name of person or type \"q\" to exit: ");
scanf("%s", in_iname); /* Using in lieu of get_string */
if (strcmp(in_iname, "q") == 0)
{
break; /* At this level we can just use break to exit the "while" loop */
}
for(int i = 0; i < NELMS(name); i )
{
if(strcmp(in_iname, name[i]) == 0)
{
printf("%ld\n", number[i]);
loop = 0;
break;
}
}
if (loop == 1)
{
printf("Sorry - this was an invalid name - please try again\n");
}
}
return 0;
}
As noted, a macro is defined to streamline checking how many elements are in your name array. That way if you add or subtract from the array (and the corresponding long integer array) you need not be concerned with updating a loop counter test. Second, the prompting and validation block is placed inside a "while" loop which will be exited once valid name is entered or the user decides to quit by entering the letter "q". That gives the user a proper method of exiting without having to force an exit with "Ctrl-C".
Following is some sample output from the terminal.
@Una:~/C_Programs/Console/GetName/bin/Release$ ./GetName
Name of person or type "q" to exit: JIm
Sorry - this was an invalid name - please try again
Name of person or type "q" to exit: Lily
Sorry - this was an invalid name - please try again
Name of person or type "q" to exit: Gabe
8178895398
Use all or part of this to see if it meets the spirit of your project.