#include<stdio.h>
void clearKeyboard(void){
while(getchar()!='\n');
}
void pause(void){
printf("Press <ENTER> to continue...");
clearKeyboard();
}
void printWelcome(void){
printf ("---=== Grocery Inventory System ===---\n\n");
}
int getInt(void){
int iVal;
char charCheck='x';
while (charCheck != '\n'){
scanf ("%d%c",&iVal,&charCheck);
if ( charCheck != '\n'){
clearKeyboard();
printf ("Invalid integer, please try again: ");
}
}
return iVal;
}
int getYesOrNo(void){
//list of variables declared and initialized
char ch;
int ret;
ch = 0;
ret = 0;
//it will keep asking the user as long as they don't reply with y or n
while(ch != 'Y' || ch != 'y' || ch != 'N' || ch != 'n')
{
scanf(" %c", &ch);
clearKeyboard();
if (ch == 'Y' || ch == 'y'){
ret = 1;
return ret;
}
else if (ch == 'N' || ch == 'n'){
ret = 0;
return ret;
}
//if they type other than y or n, it will print out this message
else{
printf("Only (Y)es or (N)o are acceptable: ");
}
}
return ret;
}
int getIntLimited(int lowerLimit, int upperLimit){
int iVal;
do{
iVal = getInt();
if (!(iVal >= lowerLimit && iVal <= upperLimit))
printf ("Invalid value, 0 <= value <= 7: ");
}while (!(iVal >= lowerLimit && iVal <=upperLimit));
return iVal;
}
int getMenuChoice(void){
int SEL;
int temp;
printf("1- List all items\n");
printf("2- Search by SKU\n");
printf("3- Checkout an item\n");
printf("4- Stock an item\n");
printf("5- Add new item or update item\n");
printf("6- delete item\n");
printf("7- Search by name\n");
printf("0- Exit program\n> ");
scanf("%d", &SEL);
if (SEL > 7){
temp = getIntLimited(0,7);
}
return SEL;
}
void GrocInvSys(void){
int SEL;
int DONE = 0;
SEL = 0;
printWelcome();
while (DONE == 0){
SEL = getMenuChoice();
clearKeyboard();
if (SEL == 1){
printf("List Items!\n");
pause();
}
if (SEL == 2){
printf("Search Items!\n");
pause();
}
if (SEL == 3){
printf("Checkout Item!\n");
pause();
}
if (SEL == 4){
printf("Stock Item!\n");
pause();
}
if (SEL == 5){
printf("Add/Update Item!\n");
pause();
}
if (SEL == 6){
printf("Delete Item!\n");
pause();
}
if (SEL == 7){
printf("Search by name!\n");
pause();
}
if(SEL == 0){
printf("Exit the program? (Y)es/(N)o): ");
DONE = getYesOrNo();
}
}
}
int main(void){
GrocInvSys();
return 0;
}
For this code, it would display all the items in the getMenuChoice and whenever use types the number for each item, it would print a special message.
Everything is working fine, however it should say "Invalid value, 0 < value < 7: " whenever I type something other than 0 or 1 or 2 or 3 or 4 or 5 or 6 or 7.
So I am guessing my getIntlimited function is not working in the getMenuChoice function. Any guesses to why?
p.s. I have typed pause(); for every if SEL == statement,is there any way I can make it better?
The main problem is some excess code in getMenuChoice(). It just needs to print the menu and return the value of getIntLimited(0,7):
int getMenuChoice(void){
printf("1- List all items\n");
printf("2- Search by SKU\n");
printf("3- Checkout an item\n");
printf("4- Stock an item\n");
printf("5- Add new item or update item\n");
printf("6- delete item\n");
printf("7- Search by name\n");
printf("0- Exit program\n> ");
return getIntLimited(0,7);
}
Another problem is the call to clearkeyboard() after the call to getMenuChoice() in GrocInvSys(). This reads more input up to the next newline, but getInt() has already read the newline that terminated the integer. There is no need to read another line, since its contents are just ignored. Remove that call to clearkeyboard() to fix that problem.
Related
After running the next code, in the function get_data(), everything runs normally until I get to the end of the For, when program should call the function continue_function() but in somehow, ignores it, and the program just ends.
Am I calling it in a wrong way?
I'm just start learning programming in C and this is one of the last exam's exercises.
#include <stdio.h>
#define MAX 100
#define YES 1
#define NO 0
struct record
{
char fname[15+1];
char lname[20+1];
char phone[9+1];
long income;
int month;
int day;
int year;
};
struct record list[MAX];
int last_entry = 0;
void get_data(void);
void display_report(void);
int continue_function(void);
void clear_kb(void);
main()
{
int cont = YES;
int ch;
while(cont == YES)
{
printf("\n");
printf("\n MENU");
printf("\n =========\n");
printf("\n1. Enter Names");
printf("\n2. Print Report");
printf("\n3. Quit");
printf("\n\nEnter Selection ==> ");
scanf("%d", &ch);
clear_kb();
system("cls");
switch(ch)
{
case 1: get_data();
break;
case 2: display_report();
break;
case 3: printf("\n\nThank You for using this Program!");
cont = NO;
break;
default: printf("\n\nInvalid Choice, Please Select 1 to 3!");
break;
}
}
}
void get_data(void)
{
int cont;
int ctr;
for(cont = YES; last_entry < MAX && cont == YES; last_entry++)
{
printf("\n\nEnter information for Person %d.", last_entry+1);
printf("\n\nEnter first name: ");
gets(list[last_entry].fname);
printf("Enter last name: ");
gets(list[last_entry].lname);
printf("Enter phone in 123-4567 format: ");
gets(list[last_entry].phone);
printf("Enter Yearly Income: ");
scanf("%ld", &list[last_entry].income);
printf("Enter Birthday: ");
do
{
printf("\n\tMonth (0 - 12): ");
scanf("%d", &list[last_entry].month);
}
while (list[last_entry].month < 0 || list[last_entry].month > 12);
do
{
printf("\tDay (0 - 31): ");
scanf("%d", &list[last_entry].day);
}
while (list[last_entry].day < 0 || list[last_entry].day > 31);
do
{
printf("\tYear (1800 - 2025): ");
scanf("%d", list[last_entry].year);
}
while (list[last_entry].year < 1800 || list[last_entry].year > 2025);
cont = continue_function();
}
if(last_entry == MAX)
{
printf("\n\nMaximum Number of Names has been entered!\n");
}
}
void display_report(void)
{
long month_total = 0, grand_total = 0;
int x, y;
fprintf(stdout, "\n\n");
fprintf(stdout, "\n REPORT");
fprintf(stdout, "\n ========");
for(x = 0; x <= 12; x++)
{
month_total = 0;
for(y = 0; y < last_entry; y++)
{
if(list[y].month == x)
{
fprintf(stdout, "\n\t%s %s %s %ld", list[y].fname, list[y].lname, list[y].phone, list[y].income);
month_total += list[y].income;
}
}
fprintf(stdout, "\nTotal for month %d is %ld:", x, month_total);
grand_total += month_total;
}
fprintf(stdout, "\n\nReport Totals:");
fprintf(stdout, "\nTotal Income is %ld", grand_total);
fprintf(stdout, "\nAverage Income is %ld", grand_total/last_entry);
fprintf(stdout, "\n\n* * * End of Report * * *");
}
int continue_function(void)
{
char ch;
printf("\n\nDo you wish to continue? (Y)es/(N)o: ");
getc(ch);
while(ch != 'n' && ch != 'N' && ch != 'y' && ch != 'Y')
{
printf("\n%c is invalid!", ch);
printf("\n\nPlease enter \'N\' to Quit or \'Y\' to Continue: ");
getc(ch);
}
clear_kb();
if(ch == 'n' || ch == 'N')
{
system("cls");
return(NO);
}
else
{
system("cls");
return(YES);
}
}
void clear_kb(void)
{
char junk[80];
gets(junk);
}
You're not using your get functions properly:
https://www.tutorialspoint.com/c_standard_library/c_function_getc.htm
int getc(FILE *); for instance requires a FILE* as argument and returns the character, but you are passing the character as argument, it should look like ch = getc(stdin);.
On another note, notice that when you use scanf you pass &c? This is because for a value to be modified, you need to pass the address where it's stored (&) and not the value itself. So even if getc took a char, it would have to be getc(&ch).
You should follow a quick tutorial on pointers and types, if you come from a higher level language or if you're new that could be a good idea.
I have this code here to search a string in a file and display it. How can I edit this piece of code to make it possible to search just a part of this string?
say, the file contains the author "dennis zill", if I search "zill" I want the code to display "dennis zill".
int searchauthor(FILE *ptr) //searching starts
{
int save;
char de;
char in_name[30];
int flag = 1;
save = readbooks();
ptr = fopen("books info.txt", "r");
fflush(stdin);
printf("\n Enter the name of the author: ");
gets(in_name);
printf("-------------------------------------------------");
for(int i = 0; i < save; i++){
if(strcmpi(in_name, info[i].bauthor) == 0){
printf("\n Book name: %s\n Price: %u\n Number of books available: %u\n Number of pages: %u\n-------------------------------------------------", info[i].bname, info[i].price, info[i].numavail, info[i].bpages);
flag = 0;
}
}
if (flag == 1){
printf("\n Not found.\n Do you want to try another search [Y/N]? ");
scanf("%c", &de);
if(de == 'y' || de == 'Y'){
system("cls");
in_name[MAX] = reset(in_name);
fclose(ptr);
return 2;
}
else if(de == 'n' || de == 'N'){
printf("\n You will be redirected to main menu");
for(int k = 1; k <= 5; k++){
Sleep(300);
printf(".");
}
system("cls");
return 1;
}
}
printf("\n\n Do you want to try another search [Y/N]? ");
scanf("%c", &de);
if (de == 'y' || de == 'Y') {
system("cls");
in_name[MAX] = reset(in_name);
return 2; //return 2 to case 3 to search again
}
else if (de == 'n' || de == 'N') {
system("cls");
printf("\n You will be redirected to main menu");
for(int k = 1; k <= 5; k++){
Sleep(300);
printf(".");
}
system("cls");
return 1;
}
} //searching ends
The problem is the line:
if(strcmp(in_name, info[i].bauthor) == 0){
It will succeed only on a full match.
Rather that using strcmp use strstr (or its case-insensitive variant strcasestr() if it's available).
char *strstr(const char *haystack, const char *needle);
The haystack is the full authors name, needle the user's input.
Function returns NULL on failure so it's enough to check if non-NULL was returned.
if(strstr(info[i].bauthor, in_name) != NULL){
I am trying to write a program that loops asking the user to continuously input either a float, int, or char and echo it back to them until they enter 'q', then the loop ends. I do not understand how to decipher between an int, char, or float before entering the loop. I have tried if (scanf("%c", ch)) and so on for float and int and that works great, but once I added the loop in it's messing me up. I have tried several different combinations, but I have still not found my answer.
Here is one attempt to show you exactly what I am trying to do:
char ch;
int num = 0;
float fl = 0;
printf("Enter a value: ");
while(ch != 'q') {
if (scanf("%c", &ch) && !isdigit(ch)) {
printf("You entered a character %c\n", ch);
}
else if (scanf("%d", &num)) }
printf("You entered an integer %d\n", num);
}
else if (scanf("%d", &num)) {
printf("You entered a floating point number %f\n", fl);
}
printf("Enter another value: ");
}
}
This keeps doing something strange and I cannot pinpoint my problem. Thank you in advance!
You cannot accomplish that with your approach. You can scan a line and parse it accordingly:
char line[128]; /* Create a buffer to store the line */
char ch = 0;
int num;
float fl; /* Variables to store data in */
int r;
size_t n; /* For checking from `sscanf` */
/* A `do...while` loop is best for your case */
do {
printf("Enter a value: ");
if(fgets(line, sizeof(line), stdin) == NULL) /* If scanning a line failed */
{
fputs("`fgets` failed", stderr);
exit(1); /* Exits the program with a return value `1`; Requires `stdlib.h` */
}
line[strcspn(line, "\n")] = '\0'; /* Replace `\n` with `'\0'` */
r = sscanf(buffer, "%d%zn", &num, &n);
if(r == 1 && n == strlen(line)) { /* If true, entered data is an integer; `strlen` requires `string.h` */
printf("You entered an integer %d\n", num);
}
else{
r = sscanf(buffer, "%f%zn", &fl, &n);
if(r == 1 && n == strlen(line)) { /* If true, entered data is a float; `strlen` requires `string.h` */
printf("You entered a floating point number %f\n", fl);
}
else{
if(strlen(line) == 1) /* If true, entered data is a character; `strlen` requires `string.h` */
{
ch = line[0];
printf("You entered a character %c\n", ch);
}
else{ /* Entered data is something else */
printf("You entered \"%s\"\n", line);
}
}
}
}while(c != 'q');
Disclaimer: I wrote the above code using a mobile and I haven't tested it.
Update (did not test and wrote with my mobile):
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
int main(void)
{
int c = 0;
bool random = false;
bool flag = true;
bool is_float = false, is_char = false, is_number = false;
do{
c = getchar();
if(c == EOF)
break;
if(!random)
{
if(isdigit(c))
{
is_number = true;
}
else if(c == '.')
{
if(is_number)
{
if(is_float)
{
random = true;
}
else
{
is_float = true;
}
}
else if(!is_number && !is_float && !is_char)
{
is_float = true;
}
}
else if(c == '-' && !is_float && !is_number && !is_char);
else if(isalpha(c))
{
if(is_char)
random = true;
else
{
is_char = true;
if(c == 'q')
flag = false;
}
}
else
{
random = true;
}
if((is_char && is_float) || (is_char && is_number))
random = true;
if(c == '\n' && !is_char && !is_float && !is_number)
random = true;
}
if(c == '\n')
{
if(random)
/* puts("You entered a random string!"); */
puts("Invalid input!");
else if(is_float)
puts("You entered a float!");
else if(is_number)
puts("You entered a number!");
else if(is_char)
puts("You entered a character!");
else
puts("Error!");
if(!flag && !is_number && !is_float && !random)
flag = false;
else
flag = true;
is_char = is_float = is_number = random = false;
}
}while(flag);
puts("Done");
return 0;
}
The problem is in this function. It's supposed to validate input for two variables as integers. What did I do wrong? O__O Thanks :)
I used an if else statement to check for change in the variable valid so that it will exit the loop once the right input is given. Even if I input the right values, the code still crashes.
void input(int *n1, int *n2, char *opt)
{
int valid = 0;
int v2 = 0;
char choice;
int a, b;
while (v2 == 0)
{
printf("Enter first number: \n");
if(scanf("%d", &a) == 1)
{
while(v2 == 0)
{
printf("Enter second number: \n");
if(scanf("%d", &b) == 1)
{
v2 =1;
getchar();
}
else
{
getchar();
printf("Invalid input!\n");
}
}
getchar();
}
else
{
getchar();
printf("Invalid input!\n");
}
}
while( valid == 0)
{
printf("Addition -> 1\nSubtraction -> 2\nMultiplication -> 3\nDivision -> 4\nReset -> R\nExit -> E\n");
scanf("%c", &choice);
if (choice == 'r' || choice == 'e')
{
choice = toupper(choice);
}
if ((choice == '1') || (choice == '2') || (choice == '3') || (choice == '4') || (choice == 'R') || (choice == 'E'))
{
valid = 1;
}
else
{
printf("Invalid input!\n\n");
}
}
*opt = choice;
*n1 = a;
*n2 = b;
}
Here's the whole code for reference. The answers earlier were able to fix the crash. Now, either the loop doesn't exit or it doesn't work right.
#include <stdio.h>
#include <ctype.h>
int add(int n1, int n2);
int subtract(int n1, int n2);
int multiply(int n1, int n2);
int divide(int n1, int n2);
void input(int *n1, int *n2, char *opt);
int main(void)
{
int n1, n2, ret;
char opt;
start:
input(&n1, &n2, &opt);
switch(opt)
{
case '1':
ret = add(n1, n2);
printf("The sum is %d\n", ret);
break;
case '2':
ret = subtract(n1, n2);
printf("The difference is %d\n", ret);
break;
case '3':
ret = multiply(n1, n2);
printf("The product is %d\n", ret);
break;
case '4':
ret = divide(n1, n2);
printf("The quotient is %d\n", ret);
break;
case 'R':
goto start;
break;
case 'E':
printf("Goodbye!\n");
return 0;
break;
}
goto start;
}
void input(int *n1, int *n2, char *opt)
{
int valid = 0;
int v2 = 0;
char choice;
int a, b;
while (v2 == 0)
{
printf("Enter first number: \n");
if(scanf("%d", &a) == 1)
{
while(v2 == 0)
{
printf("Enter second number: \n");
if(scanf("%d", &b) == 1)
{
v2 =1;
getchar();
}
else
{
getchar();
printf("Invalid input!\n");
}
}
getchar();
}
else
{
getchar();
printf("Invalid input!\n");
}
}
while( valid == 0)
{
printf("Addition -> 1\nSubtraction -> 2\nMultiplication -> 3\nDivision -> 4\nReset -> R\nExit -> E\n");
scanf("%c", &choice);
if (choice == 'r' || choice == 'e')
{
choice = toupper(choice);
}
if ((choice == '1') || (choice == '2') || (choice == '3') || (choice == '4') || (choice == 'R') || (choice == 'E'))
{
valid = 1;
}
else
{
printf("Invalid input!\n\n");
}
}
*opt = choice;
*n1 = a;
*n2 = b;
}
int add(n1, n2)
{
int result;
result = (n1+n2);
return result;
}
int subtract(n1, n2)
{
int result;
result = (n1-n2);
return result;
}
int divide(n1, n2)
{
int result;
result = (n1/n2);
return result;
}
multiply(n1, n2)
{
int result;
result = (n1*n2);
return result;
}
Change
if(scanf("%d", a) != 0)
to
if(scanf("%d", &a) == 1)
// ^^^^ This is the right check
// ^^^ Missing &
scanf return EOF if it fails to assign to the first receiving argument. In this case, it will return 1 if data was successfully read into &a.
Similarly, change
if(scanf("%d", b) != 0)
to
if(scanf("%d", &b) == 1)
// ^^^ ^^^^
Instead of
if(scanf("%d", a) != 0)
You should use
if(scanf("%d", &a))
Scanf could return 0,1 or EOF out of which only 1 indicates no error in input!
However if your a was a pointer to some integer address location you could have used the former code.Change it for inputting b as well
This is what the items.txt contains:
275,Fresh Fish,12.34,0
386,Soft kleenex,45.67,1
240,Ultra Tide,24.34,1
916,Red Apples,123.45,0
385,Magic Broom,456.78,1
495,Liquid Soap,546.02,1
316,Chocolate Cookies,78.34,1
355,Organic Milk,24.34,0
846,Dark Chocolate,123.45,1
359,Organic Banana,99.99,0
How can I rewind the file when user enters "Y"? It works if I enter the right value the first time.
#include <stdio.h>
#define TAX (0.13)
void keybFlush(){
while(getchar() != '\n');
}
int getInt(){
int val;
char nl = 'x';
while (nl != '\n'){
scanf("%d%c", &val, &nl);
if (nl != '\n'){
keybFlush();
printf("Invalid Integer, please try again: ");
}
}
return val;
}
int yes(){
char ch = 'x';
int res;
do{
ch = getchar();
res = (ch == 'Y' || ch == 'y');
keybFlush();
} while (ch != 'y' && ch != 'Y' && ch != 'n' && ch != 'N' && printf("Only (Y)es or (N)o are acceptable: "));
return res;
}
int main(){
int upc, userUpc, found = 0;
double price;
int isTaxed, i;
char item[21], ch, x, y;
FILE* fptr = fopen("items.txt", "r");
if (fptr){
do {
userUpc=getInt();
printf(" UPC | Name | Price | Tax | Total\n"
" -----+--------------------+-----------+---------+-----------\n");
printf("its : %d\n", userUpc);
while (!feof(fptr)){
fscanf(fptr, "%d,%[^,],%lf,%d", &upc, item, &price, &isTaxed);
if ( upc == userUpc){
if(!feof(fptr)){
printf("%-6d|%-20s|%11.2lf|", upc, item, price);
if (isTaxed)
printf("%9.2lf|%11.2lf\n", price * TAX, price * (1+TAX));
else
printf("%9.2lf|%11.2lf\n", 0.0, price);
found = 1;
}
}
}
if (!found){
printf("Can't find any matched records\n");
}
printf("Do you want to continue: ");
i=yes(); // if yes, rewind the file
} while (!userUpc || (i == 1));
fclose(fptr);
}
else{
printf("could not open the file\n");
}
return 0;
}
If I enter "y" to continue and the right value, it doesn't seem to output correctly.
To change the position of your file cursor to the beginning of the file:
fseek(filePtr, 0, SEEK_SET);
more on file manipulation :
http://www.gnu.org/software/libc/manual/html_node/File-Positioning.html