Why my boolean function result is not recognized in main function - c

so I'm trying to build an ATM program, and I can't really understand why my code isn't working.
code:
#include <stdio.h>
#include <stdbool.h>
bool accessCheck(int);
const int pin =1234;
int main(){
int code, access;
printf("Hi, please enter your password:");
scanf("%d",&code);
accessCheck(code);
if(accessCheck(code)==1){
printf("Password recognized.");
}
else {
printf("Password unrecognized.");
}
return 0;
}
bool accessCheck(int x){
bool access=0;
if(x == pin){
bool access = 1;
}
return access;
}

You're declaring new variable access inside the if block. This shadows the variable with the same name in the main block of accessCheck.
You should just assign that variable rather than declaring another variable.
#include <stdio.h>
#include <stdbool.h>
bool accessCheck(int);
const int pin =1234;
int main(){
int code;
printf("Hi, please enter your password:");
scanf("%d",&code);
if(accessCheck(code)){
printf("Password recognized.");
}
else {
printf("Password unrecognized.");
}
return 0;
}
bool accessCheck(int x){
bool access=false;
if(x == pin){
access = true;
}
return access;
}
Since you're using stdbool you can use true and false instead of 1 and 0.
There's no need to call accessCheck() before the if statement.

Related

why the bool value doesnt modified by void function? [duplicate]

This question already has answers here:
Why has this function no effect when called?
(3 answers)
Closed 3 months ago.
#include <stdio.h>
#include <stdbool.h>
#include "header.h"
int main() {
bool check = true;
while (check) {
test(check);
//printf("%d", check);
}
return 0;
}
this is my main.c file
and this is my header.c file
#include <stdbool.h>
void test(bool check)
{
while (true)
{
if(check){
check = false;
break;
}
}
}
i notice this while im doing the another programm , and it goes inifite loop.
why the check value doesnt change to false?
get a false value and end loop
C is pass-by-value, not by-reference.
This means that test() gets a copy of the value of the check variable's value. It has no way to reach back and change the caller's variable, that the argument is named the same as the variable does not matter.
To fix it, you need to pass the address of the value instead, using pointers:
void test(bool *check)
{
while (true)
{
if (*check){
*check = false;
break;
}
}
}
and then of course you need to explicitly pass the address, in main():
int main() {
bool check = true;
while (check) {
test(&check);
//printf("%d", check);
}
return 0;
}
Of course, it would be cleaner to return the new value, instead of passing the pointer.

when i compile it give me this error can anyone help me?

this is code written in c
#include <stdio.h>
#include "simpio.h"
#include "genlib.h"
#include <stdbool.h>
bool Valid_Time(int h,int min,int sec);
int main()
{
int h,min,sec;
printf("Dwse tis ores: ");
h=GetInteger();
printf("Dwse ta lepta: ");
min=GetInteger();
printf("Dwse ta defterolepta: ");
sec=GetInteger();
if ( Valid_Time (int h,int min,int sec) == true)
{
printf("Valid: yes");
}
else
{
printf("Valid: no");
}
return 0;
}
bool Valid_Time(int h,int min,int sec)
{
bool valid;
valid=true;
if(h<0 || h>23)
{
valid=false;
}
if(min<0 || min>59)
{
valid=false;
}
if(sec<0 || sec>59)
{
valid=false;
}
return valid;
}
error:expected expession befor 'int'
error:too few arguments to function 'Valid_Time'
i cant undersand why there is an error
why does this error pop up
The function call has type parameters in your usage. Remove the type specification when using the function.
Try this
Valid_Time (h,min,sec) == true

Problem when trying to copy a struct in C

So I am starting to use C, and I have some problems almost always with memory allocation. Basically I am getting stuck when I am trying to copy a struct to another struct.
I will put you the code here:
The headers file is:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
//declaration constants
#define MAX_NAME 15+1
#define SEATS_PERCENTAGE 0.95
#define IN_TIME 60
#define OUT_TIME 120
//type declarations
typedef enum {FORBIDDEN, ALLOWED_WITH_COMPANION, ALLOWED} tFairgroundRideAccess;
typedef struct{
tFairgroundRideAccess lessThan100;
tFairgroundRideAccess between100_120;
tFairgroundRideAccess between120_140;
tFairgroundRideAccess greaterThan140;
} tFairgroundRideHeightRequirement;
typedef struct {
char name[MAX_NAME];
tFairgroundRideHeightRequirement accessHeight;
int durationTrip;
int numPersonsTrip;
int peopleInQueue;
} tFairgroundRide;
tFairgroundRide myFairgroundRide;
//define function headers
void readFairgroundRide(tFairgroundRide *fRide);
void writeFairgroundRide(tFairgroundRide fRide);
void copyFairgroundRide(tFairgroundRide fRideSrc, tFairgroundRide *fRideDst);
int waitingTime (tFairgroundRide fRide, int people);
bool accessWithoutCompanion (tFairgroundRide fRide, int height);
void selectFairgroundRide (tFairgroundRide fRide1, tFairgroundRide fRide2, int people1, int people2, int height2);
The file with the functions is the following one (what I want to check why I am not allocating properly in memory the new copy in the function copyFairgroundRide):
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "fairgroundRide.h"
void readFairgroundRide(tFairgroundRide *fRide){
printf("NAME >> \n");
scanf("%s", fRide->name);
getchar();
printf("ACCESS HEIGHT, LESSTHAN100 (0-FORBIDDEN, 1-ALLOWED_WITH_COMPANION, 2-ALLOWED) >> \n");
scanf("%d", &fRide->accessHeight.lessThan100);
getchar();
printf("ACCESS HEIGHT, BETWEEN100_120 (0-FORBIDDEN, 1-ALLOWED_WITH_COMPANION, 2-ALLOWED) >> \n");
scanf("%d", &fRide->accessHeight.between100_120);
getchar();
printf("ACCESS HEIGHT, BETWEEN120_140 (0-FORBIDDEN, 1-ALLOWED_WITH_COMPANION, 2-ALLOWED) >> \n");
scanf("%d", &fRide->accessHeight.between120_140);
getchar();
printf("ACCESS HEIGHT, GREATERTHAN140 (0-FORBIDDEN, 1-ALLOWED_WITH_COMPANION, 2-ALLOWED) >> \n");
scanf("%d", &fRide->accessHeight.greaterThan140);
getchar();
printf("TRIP DURATION >> \n");
scanf("%d", &fRide->durationTrip);
getchar();
printf("NUMBER OF PERSONS ON A TRIP >> \n");
scanf("%d", &fRide->numPersonsTrip);
getchar();
}
void writeFairgroundRide(tFairgroundRide fRide){
printf("NAME: %s\n", fRide.name);
printf("ACCESS HEIGHT, LESSTHAN100 (0-FORBIDDEN, 1-ALLOWED_WITH_COMPANION, 2-ALLOWED): %d\n",fRide.accessHeight.lessThan100);
printf("ACCESS HEIGHT, LESSTHAN100 (0-FORBIDDEN, 1-ALLOWED_WITH_COMPANION, 2-ALLOWED): %d\n",fRide.accessHeight.between100_120);
printf("ACCESS HEIGHT, LESSTHAN100 (0-FORBIDDEN, 1-ALLOWED_WITH_COMPANION, 2-ALLOWED): %d\n",fRide.accessHeight.between120_140);
printf("ACCESS HEIGHT, LESSTHAN100 (0-FORBIDDEN, 1-ALLOWED_WITH_COMPANION, 2-ALLOWED): %d\n",fRide.accessHeight.greaterThan140);
printf("TRIP DURATION: %d\n", fRide.durationTrip);
printf("NUMBER OF PERSONS ON A TRIP: %d\n", fRide.numPersonsTrip);
}
void copyFairgroundRide(tFairgroundRide fRideSrc, tFairgroundRide *fRideDst){
strcpy(fRideDst->name,fRideSrc.name);
fRideDst->accessHeight.lessThan100 = fRideSrc.accessHeight.lessThan100;
fRideDst->accessHeight.between100_120 = fRideSrc.accessHeight.between100_120;
fRideDst->accessHeight.between120_140 = fRideSrc.accessHeight.between120_140;
fRideDst->accessHeight.greaterThan140 = fRideSrc.accessHeight.greaterThan140;
fRideDst->durationTrip = fRideSrc.durationTrip;
fRideDst->numPersonsTrip = fRideSrc.numPersonsTrip;
fRideDst->peopleInQueue = fRideSrc.peopleInQueue;
}
int waitingTime (tFairgroundRide fRide, int people){
int result;
result = ((IN_TIME+OUT_TIME+(fRide.durationTrip*60)) * (fRide.numPersonsTrip * SEATS_PERCENTAGE) * people);
return result;
}
bool accessWithoutCompanion (tFairgroundRide fRide, int height){
if (height < 100 && fRide.accessHeight.lessThan100 == 2){
return true;
}
if(height >= 100 && height<120 && fRide.accessHeight.between100_120 == 2){
return true;
}
if(height >= 120 && height<=140 && fRide.accessHeight.between120_140 == 2){
return true;
}
if (height > 140 && fRide.accessHeight.greaterThan140 == 2){
return true;
}else{
return false;
}
}
void selectFairgroundRide (tFairgroundRide fRide1, tFairgroundRide fRide2, int people1, int people2, int height2){
if((accessWithoutCompanion(fRide1,height2) == true) && (waitingTime(fRide1,people1)<=waitingTime(fRide2,people2))){
copyFairgroundRide(fRide1,&myFairgroundRide);
}
if((accessWithoutCompanion(fRide1,height2) == true) && (accessWithoutCompanion(fRide2,height2)==false)){
copyFairgroundRide(fRide1,&myFairgroundRide);
}
if((accessWithoutCompanion(fRide1,height2) == false) && (accessWithoutCompanion(fRide2,height2))){
copyFairgroundRide(fRide2,&myFairgroundRide);
}
}
And the main code is:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "fairgroundRide.h"
int main(){
tFairgroundRide myFairgroundRide;
tFairgroundRide fairgroundRide1;
tFairgroundRide fairgroundRide2;
int height1,people1,people2;
printf("ENTER DATA FOR FIRST FAIRGROUND RIDE >>\n");
readFairgroundRide(&fairgroundRide1);
printf("ENTER THE PEOPLE IN THE QUEUE OF FAIRGROUND RIDE 1 >> \n");
scanf("%d", &people1);
getchar();
printf("ENTER DATA FOR SECOND FAIRGROUND RIDE >>\n");
readFairgroundRide(&fairgroundRide2);
printf("ENTER THE PEOPLE IN THE QUEUE OF FAIRGROUND RIDE 2 >> \n");
scanf("%d", &people2);
getchar();
printf("ENTER THE HEIGHT >> \n");
scanf("%d", &height1);
getchar();
selectFairgroundRide(fairgroundRide1,fairgroundRide2,people1,people2,height1);
printf("RESULTS:\n");
writeFairgroundRide(myFairgroundRide);
getchar();
return 0;
}
So when I am running the program I can get all the info of both inputs, faigroundRide1 and fairgroundRide2, but if I copy it to another struct called myFairgroundRide I start getting weird characters and numbers, and I know that is due to memory allocation but I cannot find why or where is the issue. If you need further explanations about the code or what is my doubt just let me know and I will try to re-do it in another way.
Thanks in advance,
Jorge.
You have two myFairgroundRide. One in global scope, and one as local variable in main. In selectFairgroundRide you copy to the global one, but later in main you print the local one.
And by the way, copyFairgroundRide can be simplified:
void copyFairgroundRide(tFairgroundRide fRideSrc, tFairgroundRide *fRideDst)
{
*fRideDst = fRideSrc;
}

Sudoku Solver in C printing

Okay so I have all this code for a sudoku solver (it's not done, I still need my find next function and legal move function) but I could use some help with printing. The code asks for the user to upload a file to read, and then prints out the puzzle. This means sudoku can be 3x3 or 4x4, all the way up to 25x25. What I need help with, is in my print_puzzle function, how do I make sure that it prints out in blocks? Like if it was 3x3, there would be 9 3x3 boxes for the puzzle, etc. Hopefully that makes sense, anyways here's the code:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
bool prompt_for_file (void);
bool load_puzzle(char filename[]);
void print_puzzle (void);
bool solve_puzzle(void);
bool find_next( int *row, int *col, int dir);
bool legal_move(int num; int row; int col);
#define MAX_SIZE 25
#define FORWARD 1
#define BACKWARD 0
int puzzle[MAX_SIZE][MAX_SIZE];
int puzzle_size;
int block_size;
int main(void)
{
if (!prompt_for_file())
{return -1;}
print_puzzle();
if (solve_puzzle())
{print_puzzle();}
else
{return -1;}
return 0;
}
bool solve_puzzle(void)
{
int dir;
int R;
int C;
int n;
int previous_n;
dir = FORWARD;
while(find_next(&R, &C, dir))
{
dir= BACKWARD;
previous_n=abs(puzzle[R][C]);
puzzle[R][C]= 0;
for (n=previous_n+1; n<=puzzle_size; n++)
{
if (legal_move(n, R, C))
{
puzzle[R][C] = -n;
dir = FORWARD;
break;
}
}
}
if (dir== FORWARD)
{return true;}
else
{return false;}
}
void print_puzzle (void)
{
int r;
int c;
printf("\n");
for (r=0;r<puzzle_size;r++)
{
for (c=0;c<puzzle_size;c++)
{
printf("%3d", abs(puzzle[r][c]));
//put something in to define blocks
}
printf("\n");
}
printf("\n");
}
/*
bool find_next( int *row, int *col, int dir)
{
*R=r;
*C=c;
}
*/
bool prompt_for_file (void)
{
char filename[100];
do
{
printf("Enter a file name (or ""quit""): ");
scanf("%s",&filename[0]);
if ( strcmp(filename,"quit")==0 )
{
filename[0]='\0';
printf("quitting\n");
return false;
}
} while( load_puzzle(filename)!=true );
return true;
}
bool load_puzzle(char filename[])
{
int r;
int c;
int dummy;
FILE *fileptr;
// open the file but return if not successful
fileptr = fopen(filename,"r");
if ( fileptr==NULL)
{
printf("the file was not opened: %s\n", filename);
return false;
}
else
{
printf("the file was opened: %s\n", filename);
}
// read the sizing info
fscanf(fileptr,"%d",&puzzle_size);
printf("the number of rows and cols: %d\n",puzzle_size);
if (puzzle_size>MAX_SIZE)
{
printf("puzzle is too big %d. Max size is %d\n", puzzle_size,MAX_SIZE);
return false;
}
block_size=sqrt(puzzle_size);
// read the data
for (r=0;r<puzzle_size;r++)
{
for (c=0;c<puzzle_size;c++)
{
if ( fscanf(fileptr,"%d",&puzzle[r][c])!=1 )
{
printf("insufficient number of elements\n");
return false;
}
}
}
// check to see if all of the data was read
// and that the end of the input file has been reached
if (fscanf(fileptr,"%d",&dummy) ==1)
{
printf("WARNING: not all numbers from file were used.\n");
}
if (feof(fileptr))
{
printf("The end of the input file has been reached.\n");
}
fclose(fileptr);
return true;
}
Thank you!!:)

How to omit quotation marks usage in char type?

I'm having a really hard time adjusting function to my needs. First of all look at those three files and notice how I have to call f_texture function in main function in order to make it work:
externs.h
#ifndef EXTERNS_H_
#define EXTERNS_H_
extern char t_about[100];
extern int friction;
extern int f_texture(char* ,char*);
#endif
functionA.c
#include <stdio.h>
#include "externs.h"
int main()
{
f_texture("rough","friction");
printf("Friction: %d\n", friction);
f_texture("rough","t_about");
return 0;
}
functionB.c
#include "externs.h"
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
char t_about[100];
int friction;
int f_texture(char* texture,char* what_is_needed)
{
/*Checking if both values are present*/
assert(what_is_needed);
assert(texture);
/*Static array in order to prevent it's disappearance*/
memset(t_about, '\0', sizeof(t_about));
/*Configuring variables for desired texture*/
if (strcmp(texture, "smooth") == 0)
{
strcpy(t_about, "The surface is perfectly smooth, without any "
"protuberances.\n");
friction = 0;
}
else if (strcmp(texture, "rough") == 0)
{
strcpy(t_about, "Rough bumps can be feeled under my fingertips.\n");
friction = 4;
}
/*In case of absent keyword of desired texture it will crash the program*/
else
{
assert(!what_is_needed);
}
/*Returning desired value*/
if (strcmp(what_is_needed, "t_about") == 0)
{
int i=0;
while (t_about[i] != '\0')
{
printf("%c", t_about[i]);
i++;
}
}
else if (strcmp(what_is_needed, "friction") == 0)
{
return friction;
}
/*In case of absent keyword of desired value it will crash the program*/
else
{
assert(!what_is_needed);
}
return 0;
}
And now here is my question: How to rewrite this code to make it possible to call f_texture function without using quotation marks inside? I mean instead of f_texture("abcd","efgh") just to type f_texture(abcd,efgh). I've noticed that this way it's required just after I've wrote this code.
Thanks in advance.
If you don't want to assign string constants to variables or preprocessor object macros, another option is to use preprocessor function macros, using the stringification feature:
#define call_f_texture(a,b) f_texture(#a,#b)
....
call_f_texture(rough,friction);
The C preprocessor will turn this into
f_texture("rough","friction");
You can also use some macros:
#define ROUGH "rough"
#define FRICTION "friction"
#define T_ABOUT "t_about"
int main()
{
f_texture(ROUGH, FRICTION);
printf("Friction: %d\n", friction);
f_texture(ROUGH, T_ABOUT);
return 0;
}
You can do like this,
char rough[]="rough";
char friction[]= "friction";
and call
f_texture(rough, friction);
char a[MAX] = "rouch";
char b[MAX} = "friction";
int main()
{
f_texture();
...
}
int f_texture()
{
/*Checking if both values are present*/
assert(b);
assert(a);
}
or
int f_texture(char* a,char* b)
{
/*Checking if both values are present*/
assert(b);
assert(a);
...
}
int main()
{
char a[MAX] = "rouch";
char b[MAX} = "friction";
f_texture(a,b);
...
}

Resources