Why do I keep getting compiler errors with strtok in C? - c

My assignment is to create a progrma that asks the users for a date then prints if its valid or not. I am only allowed to use strings and no int %d.
Why does my program get so many compiler errors dealing with the strtok and calling the function?
Severity Code Description Project File Line Suppression State
Warning C4024 'getInput': different types for formal and actual parameter 1 dating c:\users\chris\documents\visual studio 2015\projects\dating\dating.c 26
Warning C4047 'function': 'char *' differs in levels of indirection from 'char' dating c:\users\chris\documents\visual studio 2015\projects\dating\dating.c 26
Warning C4024 'getInput': different types for formal and actual parameter 2 dating c:\users\chris\documents\visual studio 2015\projects\dating\dating.c 26
Warning C4024 'getInput': different types for formal and actual parameter 3 dating c:\users\chris\documents\visual studio 2015\projects\dating\dating.c 26
Warning C4996 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. dating c:\users\chris\documents\visual studio 2015\projects\dating\dating.c 19
Warning C4996 'strtok': This function or variable may be unsafe. Consider using strtok_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. dating c:\users\chris\documents\visual studio 2015\projects\dating\dating.c 21
Warning C4996 'strtok': This function or variable may be unsafe. Consider using strtok_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. dating c:\users\chris\documents\visual studio 2015\projects\dating\dating.c 22
Warning C4996 'strtok': This function or variable may be unsafe. Consider using strtok_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. dating c:\users\chris\documents\visual studio 2015\projects\dating\dating.c 24
Warning C4098 'getInput': 'void' function returning a value dating c:\users\chris\documents\visual studio 2015\projects\dating\dating.c 90
Warning C4477 'printf' : format string '%s' requires an argument of type 'char *', but variadic argument 1 has type 'int' dating c:\users\chris\documents\visual studio 2015\projects\dating\dating.c 47
Warning C4477 'printf' : format string '%s' requires an argument of type 'char *', but variadic argument 2 has type 'int' dating c:\users\chris\documents\visual studio 2015\projects\dating\dating.c 47
Warning C4477 'printf' : format string '%s' requires an argument of type 'char *', but variadic argument 3 has type 'int' dating c:\users\chris\documents\visual studio 2015\projects\dating\dating.c 47
Error LNK2019 unresolved external symbol _WinMain#16 referenced in function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ) dating c:\Users\Chris\documents\visual studio 2015\Projects\dating\MSVCRTD.lib(exe_winmain.obj) 1
Error LNK1120 1 unresolved externals dating c:\users\chris\documents\visual
studio 2015\Projects\dating\Debug\dating.exe 1
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Declares function
void getInput(char *userInput1, char *userInput2, char *userInput3);
//Declares variables.
int main(void) {
char userInput[100];
printf("Enter a date in this format : MM/DD/YY:");
//Captures users input
scanf("%s", &userInput);
//Divides the 3 dates entered into 3 variables.
char *userInput1 = strtok(userInput, "/");
char *userInput2 = strtok(NULL, "/");
//Used X to capture the last end of integers entered.
char *userInput3 = strtok(NULL, "X");
//Called function.
getInput(*userInput1, *userInput2, *userInput3);
system("pause");
return 0;
}
void getInput(char *userInput1, char *userInput2, char *userInput3)
{
// If user inputs a valid date this will run.
if (0 > *userInput1<12, 0>*userInput2<31, 0>*userInput3 < 99) {
// If user month input is this...
switch (*userInput1) {
//if User month input is 02 this will run
case 02:
//If user year is a leap year this will run
if (*userInput3 % 4 == 0) {
//If user day is inbetween 0-29 it will say the date is valid.
if (0 > *userInput2 > 29) {
printf("You entered a valid date, %s/%s/%s", *userInput1, *userInput2, *userInput3);
}
// If the day of the February isn't 0-29 it will print it is invalid.
else
printf("Invalid date.");
}
case 04:
//Their are only 30 days in April, if user Inputs 31 it is invalid.
if (*userInput2 == 31) {
printf("Invalid date.");
}
//Every other date should be valid because the main if else statement filters it all out.
else {
("You entered a valid date, %s/%s/%s", *userInput1, *userInput2, *userInput3);
}
case 06:
if (*userInput2 == 31) {
printf("Invalid date.");
}
//Every other date should be valid because the main if else statement filters it all out.
else {
("You entered a valid date, %s/%s/%s", *userInput1, *userInput2, *userInput3);
}
case 11:
if (*userInput2 == 31) {
printf("Invalid date.");
}
//Every other date should be valid because the main if else statement filters it all out.
else {
("You entered a valid date, %s/%s/%s", *userInput1, *userInput2, *userInput3);
}
break;
}
}
//If the user enters a date that isn't valid , Invalid date prints.
else {
printf("Invalid date.");
}
return 0;
}

You pass characters instead of pointers to getinput():
getInput(*userInput1, *userInput2, *userInput3);
Try this instead:
getInput(userInput1, userInput2, userInput3);
The warning you get from the compiler regarding scanf() are indications of potential problems:
scanf is unsafe: indeed you do not limit the number of characters to store into the userInput array, and you should not pass the address of the array, but the address of its first entry, and you should check the return value:
if (scanf("%99s", userInput) != 1) {
/* handle input failure */
}
strtok is unsafe to use in nested contexts. You do not do this in the above code, but it is difficult for the compiler to verify and it would be safer to use strtok_s or strtok_r instead.
You have further problems in function getInput():
You should check if any of the arguments are NULL.
if (0 > *userInput1<12, 0>*userInput2<31, 0>*userInput3 < 99) { is a valid C expression, but not what you intend to test. Write this instead:
if (*userInput1 > 0 && *userInput1 < 12
&& *userInput2 > 0 && *userInput2 < 31
&& *userInput3 > 0 && *userInput3 < 99) {
...
}
But note however that you are testing characters, not the decimal value encoded into these characters. You should first convert these strings into numbers with atoi() or strtol(), or use a different scanf() conversion.
Note that leap years are not strictly multiples of 4, you might need to use the Gregorian rule if you want to handle years outside the 1901..2099 range. I assume YY implies 20YY, but this would be incorrect if you ask for birthdays.
You missed that September also has 30 days.
Here is a corrected and simplified version:
#include <stdio.h>
#include <string.h>
int check_date(int month, int day, int year);
int main(void) {
char userInput[100];
int day, month, year;
printf("Enter a date in this format : MM/DD/YY:");
//Captures users input
if (scanf("%d/%d/%d", &month, &day, &year) == 3) {
check_date(day, month, year);
} else {
printf("incorrect input\n");
}
system("pause");
return 0;
}
int check_date(int month, int day, int year) {
int valid = 1;
// If user inputs a valid date this will run.
if (month <= 0 || month > 12 || day <= 0 || day > 31 || year < 0 || year > 99) {
valid = 0;
} else {
switch (month) {
//if User month input is 02 this will run
case 02:
//If user year is a leap year this will run
if (year % 4 == 0) {
if (day > 29)
valid = 0;
} else {
if (day <= 28)
valid = 0;
}
break;
case 4:
case 6:
case 9:
case 11:
if (month > 30)
valid = 0;
break;
}
}
if (valid) {
printf("You entered a valid date, %d/%d/%d\n", month, day, year);
return 1;
} else {
printf("Invalid date\n");
return 0;
}
}

Related

When checking the validity of date of birth separated by dashes, an error rises when entering characters, and i can't solve that issue

from the requirements of my final project is to validate the scanned date of birth in the following format (DD-MM-YYYY).
the date of birth is a struct formed of 3 integers, day, month and year. So, I scan it from the user as follow:
scanf("%d-%d-%d",&dob.day,&dob.month,&dob.year);
However, when I enter a character, of course it enters an infinite loop.
I tried to use the flushinput concept (as I've used it with other scanned integers). It prevents the infinite loop. However, when re-scanning (through a certain loop), it keeps showing the "INVALID EMAIL" message, even though the entered email is correct! I guess this arises due to the presence of the dashes in the input.
So, can anybody tell me what to do?
Here's the part of the code where the issue arises (without the flushinput function):
void dobCheck(DATE dob) ///validates date of birth and rescans it if wrong
{
while(1)
{
if (dob.year<1806 || dob.year>2021 || dob.month<1 || dob.month>12 || dob.day<1 || dob.day >31)
{
dobCheckAct(dob); continue;
}
if ((dob.year%4)==0 && dob.month ==2 && dob.day >29)
{
dobCheckAct(dob); continue;
}
if (dob.year%4)!=0 && dob.month ==2 && dob.day >28)
{
dobCheckAct(dob); continue;
}
if (dob.month == 4 ||dob.month == 6 || dob.month == 9 || dob.month == 11) && dob.day> 30)
{
dobCheckAct(dob); continue;
}
break;
}
}
void dobCheckAct(DATE dob) ///action taken in case of invalid date of birth
{
printf("\a\n**INVALID DATE OF BIRTH!\n");
scanf("%d-%d-%d",&dob.day,dob.month,dob.year);
}
Thanks in advance!!

How to change a variable from a structure from integer to string?

The question assume
typedef struct {
int day;
int month;
int year;
} Date;
Date mfgDate = {17, 10, 2016}, expiryDate;
printf("%d-%d-%d", mfgDate.day, mfgDate.month, mfgDate.year);
By doing this i can display the date to the form of 17-10-2016, the question requires me to display the date in the form of DD-MMM-YYYY which is 17-OCT-2016, how can i do that? the question provides a tip which is to use switch statement but i can't seems to find out how, thanks a lot for help.
You could use strftime() for that:
char buffer[BUFFER_SIZE];
errno=0; //clear errno since we need it to detect errors in strftime()
//tm_year is years since 1900
//%d prints the day of the month, %B or %b the month name according to the locale
//and %Y prints the year, results in the format DD-MMMM-YYYY
int r = strftime
(buffer, sizeof buffer, "%d-%b-%Y", &(struct tm){.tm_year=2016-1900, .tm_mon=9, .tm_mday=17} );
if(!r && errno)
{
//do some error handling here
}
printf("%s\n",buffer);
If you are just starting with C and have not learned about arrays, a switch statement is a viable choice, just for the execise
const char* monthname(int month)
{
switch (month) {
case 1: return "JAN";
case 2: return "FEB";
// you can fill the remaining month yourself here
case 12: return "DEC";
// handle the error case
default: return "???";
}
}
to print your date then use:
printf("%d-%s-%d", mfgDate.day, monthname(mfgDate.month), mfgDate.year);
Simply have months names in the table.
char *months[] = {"JAN", "FEB","MAR", ......};
printf("%d-%s-%d", mfgDate.day, months[mfgDate.month - 1], mfgDate.year);

Accepting an input of multiple formats

I'm trying to create a code that accepts a date, however I want to give the user the ability to enter the date as dd/mm/yyyy or dd-mm-yyyy. My code is below, I tried to use OR but it does not work
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main ()
{
int dayA, monthA, yearA,
printf("First date in format DD/MM/YYYY or DD-MM-YYYY: ");
scanf("%d/%d/%d" || "%d-%d-%d", &dayA, monthA, yearA);
Here is a simple way to do it. It inputs into a string, and then checks if one of the format methods works. It's always essential to check the return value from the scanf function family, to know if the conversion succeeded.
#include<stdio.h>
int main(void)
{
int dayA, monthA, yearA;
puts("Enter a date dd/mm/yyyy or dd-mm-yyyy");
char datestr[32];
if(fgets(datestr, sizeof datestr, stdin) != NULL) {
if(sscanf(datestr, "%d/%d/%d", &dayA, &monthA, &yearA) == 3 || // added missing &
sscanf(datestr, "%d-%d-%d", &dayA, &monthA, &yearA) == 3) {
printf("Date is day %d, month %d, year %d\n", dayA, monthA, yearA);
}
}
}
Here are two runs of the program:
Enter a date dd/mm/yyyy or dd-mm-yyyy
23/4/2020
Date is day 23, month 4, year 2020
Enter a date dd/mm/yyyy or dd-mm-yyyy
23-4-2020
Date is day 23, month 4, year 2020

syntax error before ';' token

I wrote a quick program (Calculating wages) to help me stay up to speed. For some reason when I compile this I keep getting the error "53: syntax error before ';' token"
#include
int main()
{
#define week 7;
#define year 365;
int jan,feb,mar,apr,may,june,july,aug,sep,oct,nov,dec;
int wage; /* Upgrade to float in future */
char input,month;
int holder;
jan=mar=may=july=aug=oct=dec=31;
apr=june=sep=nov=30;
feb=28;
for(;;)
{
if(input='y')
{
#define YEAR 366;
break;
}
else if(input='n')
{
break;
}
else
{
printf("Unable to understand input");
}
}
printf("Enter wage/day in pounds.\n?\n");
scanf("%d",&wage); /* unsure if I need to get string and then use atoi */
printf("Wage per day:\t%d\n",wage);
printf("Wage per week:\t%d\n",wage*7);
printf("Wage per month:\t%d\n",wage*30);
holder=YEAR;
printf("Wage per year:\t%d\n",wage*holder);
printf("As months have varying day amounts, if you wish to view a specific month type:\n");
printf("A-Jan\nB-Feb\nC-Mar\nD-Apr\nE-May\nF-June\nG-July\nH-Aug\nI-Sep\nJ-Oct\nK-Nov\nL-Dec\nor type X-to EXIT");
month=getchar();
if((month=='A')||(month=='C')||(month=='E')||(month=='G')||(month=='H')||(month=='J')||(month=='L')){
printf("Wage for this month will be:\t%d",wage*31);
}
else if((month=='D')||(month=='F')||(month=='I')||(month=='K')){
printf("Wage for this month will be:\t%d",wage*30);
}
else if((month=='B')&&(year==365)){
printf("Wage for this month will be:\t%d",wage*28);
}
else if((month=='B')&&(year==366)){
printf("Wage for this month will be:\t%d",wage*29);
}
else if(month=='X'){
exit(1);
}
return 0;
}
No matter how many times I read through it I just can't manage to see what I'm doing incorrectly.
As a side note, if anybody feels like screaming/giving pointers about my style go ahead as I appreciate any tips that can lead to the improvement of my skills.
Thanks everyone, that was an overwhelmingly fast response, I've got the code compiled correctly so now I can start debugging the runtime errors (Before anyone mentions it. Yes I do understand the need to flush after input)
One error (though not the one that immediately got you, it seems) is this line:
exit 1;
In C, exit is a regular function, and so you have to call it like this:
exit(1);
As others have pointed out, your use of || is also incorrect, though that is probably giving you warnings (if anything) and not errors.
You cannot do
if (month=='A'||'C'||'E'||'G'||'H'||'J'||'L'){
...
}
This way month=='A'||'C'||'E'||'G'||'H'||'J'||'L' you are comparing the wrong values, i.e. <bool> || <char> || <char> ||..., which gives you an invalid syntax.
Try this instead,
if (month=='A' ||
month=='C' ||
month=='E' ||
month=='G' ||
month=='H' ||
month=='J' ||
month=='L') {
...
}
However, as mentioned by #danfuzz, the issue seems to lie elsewhere...
There are many catastrophic problems with your code, but most of them (besides the exit 1 bit) are formally compilable. They're just not doing what you think they're doing.
However, you state that you get a compilation error well before that exit 1 line. If that is the case, then it must be caused by something you aren't showing us, since there's no ; on line 53. My guess would be that year is defined as a macro that contains ;.
EDIT: In the comments you said that you have defined year as 365. I suspect that you did this
#define year 365;
This is what's causing the error. Get rid of that ; after 365. However, if that's the case I still don't get why you would compare your year to 365 or 366 if you already defined it to be 365 specifically.
EDIT: So, it is exactly as I guessed. You don't need those ; after #define statements for manifest constants. This is your error. Macros are replaced by textual substitution meaning that currently your
if((month=='B')&&(year==365)){
gets translated into
if((month=='B')&&(365;==365)){
which is what causes the original error. If you want to define year as a macro constant that stands for 365, it should be
#define year 365
The same applies to week, although you are not using it anywhere in your code.
Your sintax is wrong, the correct way to write that is:
if((month=='A')||(month=='C')||(month=='E')||(month=='G')||(month=='H')||(month=='J')||(month=='L')){
printf("Wage for this month will be:\t%d",wage*31);
}
and so on. But you may want to consider the switch which is probably more easy to read.
switch( month ) {
case 'A':
case 'C':
case 'E':
case 'G':
case 'H':
printf( "statement A\n");
break;
case 'K':
case 'D':
case 'F':
printf( "statement B\n");
break;
case 'B':
if( year == 365) {
printf( "something else\n" );
} else if( year == 366 ) {
printf( "something else\n" );
} else {
printf( "Unexpected year %d\n", year );
}
break;
case 'X':
return -1; /* Note here return... not exit! */
default:
printf( "Why am I here?\n" );
break;
}
Now that you edited another error is #define YEAR 365;
Get rid of that ';'. You may prefer defining that as int instead of using a precompiler define, expecially if you are going to change its value

c programming structure error

I have a few issues with my syntax, it mainly says i have an errror before "struct" on the line "Struct CustomerInfo s; but i dont seem to find the problem.
My program is supposed to ask what the person would like to do first of all, they can store a record for the first option which is then stored in a structure which can be viewed by selecting the 2nd option of the menu, if they select the 3rd then they obviously quit the program.
any help would be appreciated thank you.
#include <stdio.h>
void menu();
int id,first,last;
struct CustomerInfo
{
char FirstName[15]; /* These are the varibles for the customer infomation */
char LastName[20];
int ID;
};
int main()
{ /* program starts */
int selection = 0;
void menu()
{ /* Menu loop function */
do
{ /* menu start */
printf("\n\n - What would you like to do?");
printf("\n1 - Store a customer record");
printf("\n2 - View customer Records");
printf("\n3 - Quit program");
scanf("%i", &selection);
} while (selection > 3);
printf("You have entered an incorrect value"); /* If selection is greater than 3 then end program */
return 0;
}
switch(selection)
{
/* switch statement starts */
case 1:
struct CustomerInfo s;
printf("Please enter the customers details including First name, Lastname and ID.\n\n");
printf("Enter First name: ");
scanf("%s", s.FirstName); /* Option 1: Asks to enter the customers details to store then loops back to program */
printf("Enter Last name: ");
scanf("%s", s.LastName);
printf("Enter Customer ID: ");
scanf("%s", s.ID);
void menu();
break;
case 2:
printf("\nDisplaying Infomation\n");
printf("First name: %s\n",s.Firstname); /* Option 2: Prints the customer details as listed in option 1 */
printf("Last name: %s\n",s.Lastname);
printf("Customer ID: %s\n",s.ID);
void menu();
break;
case 3: /* Option 3: Program ends if option 3 is chosen. */
break;
}
return 0;
Let's start by looking at the structure you created; next we'll try to see if it can be fixed.
I am leaving details out so we can see the big outline:
main {
struct{}
void menu(){
do {
stuff
} while (selection > 3)
printf("you have entered an incorrect value"); // if selection is > 3
}
switch(selection) {
// do something if selection is 1 or 2, exit if 3
}
There is no final closing brace in your code. I am assuming that's a copy-paste error, so I added it. Compiling with -Wall (to get warnings as well as errors reported), I get a number of errors:
sel.c:18: error: nested functions are disabled, use -fnested-functions to re-enable
sel.c: In function ‘menu’:
sel.c:31: warning: ‘return’ with a value, in function returning void
sel.c: In function ‘main’:
sel.c:38: error: expected expression before ‘struct’
sel.c:41: error: ‘s’ undeclared (first use in this function)
sel.c:41: error: (Each undeclared identifier is reported only once
sel.c:41: error: for each function it appears in.)
sel.c:61: warning: control reaches end of non-void function
Let's take those in turn:
sel.c:18: error: nested functions are disabled, use -fnested-functions to re-enable
Putting one function inside another is "nesting". It is rare that you would want to do that - it means that the function is only "visible" when you are inside the other function (a bit like local variables, but for functions). It is not standard C - it is an extension of gcc. It's almost always a bad idea to use non-standard (and thus non-portable) extensions. See http://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html
sel.c: In function ‘menu’:
sel.c:31: warning: ‘return’ with a value, in function returning void
When we declare a function void, we say that it will not return a value. When you have a statement like return 0; you ARE returning a value. The compiler will ignore that - but it warns that you said one thing, and did another. Just use return; without a parameter, and the warning goes away.
sel.c:38: error: expected expression before ‘struct’
sel.c:41: error: ‘s’ undeclared (first use in this function)
sel.c:41: error: (Each undeclared identifier is reported only once
sel.c:41: error: for each function it appears in.)
This is the trickiest one. You would expect that you were properly declaring a variable s in line 38 - but the compiler complains. The reason for this is explained in the excellent Q&A at
Why can't variables be declared in a switch statement?
As an aside - if you COULD declare a variable like this - what are you doing with it? Your code currently reads in values, and returns. But as soon as you leave the "scope" of the variable (in your case, since you declared s inside the switch, that would be its scope) the variable disappears (the memory that was used for it is marked "free" and will be re-used.)
sel.c:61: warning: control reaches end of non-void function
This says that you have reached the end of a function that expects to return a value, but you don't have a return someValue; type of statement. Again - this only causes a warning, since the default behavior is to return 0 if no value is given, but it's a sign that you said one thing and did another.
So far I've just explained the errors that the compiler gave. Let's look more closely at the code structure. What I think you want to do is something like this:
define customerInfo structure
define menu function
main()
repeat:
call menu, get selection
switch(selection):
case 1: create new record
case 2: display records
case 3: quit program
In order to make this work, we need to make some changes to your program. First - let's move the menu function definition outside of the main function so we have portable code. Second - if we want to be able to create multiple customer records, we need to store them in an array. Really you would want a list so you can extend indefinitely, but let's keep it simple and allow a maximum of 10 records. Then we need to improve the logic of the menu function (if selection is not 1, 2 or 3 you give a message and try again; in your current code the line
printf("You have entered an incorrect value");
doesn't get executed until you have exited the loop that tested for incorrect values… so when you finally get there, the value is valid, not invalid.
Before we actually get to writing the "correct" code, there's another thing worth noting. When you read values using scanf, you do things like:
scanf("%s", s.FirstName);
which is correct, since s.FirstName is a pointer to the start of a string. However, you allocated a finite amount of space to your string (namely, 15 characters including the terminating '\0') so if someone enters a long name, your program will crash. "Good defensive coding" demands that you catch this - use for example
scanf("%14s", s.FirstName);
This says "read no more than 14 characters". There are better tricks but at least this is a start. However, you actually make a mistake when you do
scanf("%s", s.ID);
Since ID was defined as an int, and now you are reading a string into… not just its address, but into some location that is pointed to by the value of s.ID . This is very likely to give you a segmentation error (accessing memory that "isn't yours"). You should be doing:
scanf("%d", &s.ID);
"Read an integer into the location of s.ID"
Also - in some places you use FirstName, while in others you use Firstname. Ditto LastName. Capitalization matters - when you fix other compiler errors, these will start to show up.
Since you seem to want to be able to read in more than one customer record, we need an array of records; as I said above, we have to make sure the array is available in the scope of the switch statement, and "survives" that statement (so you can do something with it). Taking all these things together gets us to something like this:
#include <stdio.h>
// define function prototype:
int menu();
struct CustomerInfo
{
char FirstName[15]; /* These are the variables for the customer infomation */
char LastName[20];
int ID;
};
int menu()
{ /* Menu loop function */
int flag = 0;
int selection;
do
{ /* menu start */
if(flag > 0) printf("You have entered an incorrect value"); /* If selection is greater than 3 then end program */
printf("\n\n - What would you like to do?");
printf("\n1 - Store a customer record");
printf("\n2 - View customer Records");
printf("\n3 - Quit program\n>> ");
scanf("%i", &selection);
flag++;
} while (flag < 10 && (selection < 0 ||selection > 3));
return selection;
}
int main(void)
{ /* program starts */
struct CustomerInfo s[10];
int selection;
int customerCount = 0;
while(1) {
int ii; // loop counter we will need later
selection = menu();
switch(selection)
{
case 1:
printf("Please enter the customers details including First name, Lastname and ID.\n\n");
printf("Enter First name: ");
scanf("%s", s[customerCount].FirstName); /* Option 1: Asks to enter the customers details to store then loops back to program */
printf("Enter Last name: ");
scanf("%s", s[customerCount].LastName);
printf("Enter Customer ID: ");
scanf("%d", &s[customerCount].ID);
customerCount++;
break;
case 2:
printf("\nDisplaying Infomation\n");
for(ii = 0; ii < customerCount; ii++) {
printf("First name: %s\n",s[ii].FirstName); /* Option 2: Prints the customer details as listed in option 1 */
printf("Last name: %s\n",s[ii].LastName);
printf("Customer ID: %d\n---\n",s[ii].ID);
}
break;
case 3: /* Option 3: Program ends if option 3 is chosen. */
return 0; // program returns
break;
}
}
}
Test output:
- What would you like to do?
1 - Store a customer record
2 - View customer Records
3 - Quit program
>> 1
Please enter the customers details including First name, Lastname and ID.
Enter First name: John
Enter Last name: Smith
Enter Customer ID: 123
- What would you like to do?
1 - Store a customer record
2 - View customer Records
3 - Quit program
>> 5
You have entered an incorrect value
- What would you like to do?
1 - Store a customer record
2 - View customer Records
3 - Quit program
>> -1
You have entered an incorrect value
- What would you like to do?
1 - Store a customer record
2 - View customer Records
3 - Quit program
>> 1
Please enter the customers details including First name, Lastname and ID.
Enter First name: Harry
Enter Last name: Jones
Enter Customer ID: 654
- What would you like to do?
1 - Store a customer record
2 - View customer Records
3 - Quit program
>> 2
Displaying Infomation
First name: John
Last name: Smith
Customer ID: 123
---
First name: Harry
Last name: Jones
Customer ID: 654
---
- What would you like to do?
1 - Store a customer record
2 - View customer Records
3 - Quit program
>> 3

Resources