Program cannot match datas in variables [duplicate] - c

This question already has answers here:
How do I properly compare strings in C?
(10 answers)
Closed 6 years ago.
This is a part of my 'Phonebook' program.
void viewone(){
char name[25], fname[25];
int n, ncheck, op;
fp = fopen("Phonebook.txt","r");
printf ("\n Search by : \n 1: Name\n 2: Phone Number\n");
printf ("Enter option : ");
scanf ("%d",&op);
switch(op){
case 1:
printf ("\n Enter Name : ");
scanf ("%s",name);
fscanf(fp, "%s %d", fname, &ncheck);
while (!feof(fp)){
printf ("\n\n %s \n\n",fname);
if (fname == name){ \\ Problem in here
printf ("\n\n Contact Found...\n");
printf (" %s +880%d", fname, ncheck);
break;
}
else{
fscanf(fp, "%s %d", fname, &ncheck);
}
if (feof(fp)){
printf ("\n\n Contact Not Found...\n\n");
}
}
menu();
break;
case 2:
printf ("\n\n Enter Contact Number (+880) : ");
scanf ("%d",&n);
fscanf(fp, "%s %d", fname, &ncheck);
while (!feof(fp)){
if (ncheck == n){
printf ("\n\n Contact Found...\n");
printf (" %s +880%d\n", fname, ncheck);
break;
}
else{
fscanf(fp, "%s %d", fname, &ncheck);
}
if (feof(fp)){
printf ("\n\n Contact Not Found...\n\n");
}
}
menu();
break;
default:
printf ("\n Wrong option...\n\n");
viewone();
break;
}
}
When it comes to the marked line, the program should search the file for the 'fname' character until it matches the 'name' character. But though they match, nothing happens and the program still goes on. And in the end, it does what is told in the else statement. My question is why is this happening and how can i fix it?
My program runs perfectly when i search with phone number. But why is it not happening with character?

Comparing string is not done by ==. Instead use string compare function like this
strcmp(fname,name)

Related

Update the specific value in text file c

I am facing an issue with the last part of the assignment. I want to update(modify) the specific value in a text file but this program stop working from the if statement in the while loop. I have tried to fix this issue but I couldn't figure it out. Please help.
int Equant()
{
DisplayList();
FILE * fpr_r = fopen("donation.txt", "r");
FILE * fpr_w = fopen("Edonation.txt", "w");
if (fpr_r == NULL || fpr_w == NULL)
{
printf ("An able to open the file for writing");
exit(1);
}
int Inumber, v=0;
double NQuant, NVQuant;
struct Donate Esearch;
printf ("\nEnter the ID you want to change the quantity: ");
scanf ("%d", &Inumber);
while (fread(&Esearch, sizeof(struct Donate),1,fpr_r))
{
fscanf (fpr_r,"%d %s %s %s %d %lf,", &Esearch.IDd, &Esearch.supply, &Esearch.Scode, &Esearch.Cdonator, &Esearch.Nship, &Esearch.QuantityR);
if (Esearch.IDd == Inumber)
{
printf ("ID: %d Name of Supply: %s, Supply Code: %s, Donator: %s, No. of Shipment: %d, Quantity Received (Million): %lf\n",
Esearch.IDd, Esearch.supply, Esearch.Scode, Esearch.Cdonator, Esearch.Nship, Esearch.QuantityR);
printf ("\nEnter the new Quantity value: ");
scanf ("%lf", &NVQuant);
Esearch.QuantityR = NVQuant;
}
fwrite (&Esearch, sizeof(struct Donate),1,fpr_w);
}
fclose(fpr_r);
fclose(fpr_w);
if (!v)
{
printf ("No line founded.\n");
}

Is there a better way to accept full name of user in structures from the console in C language?

This is a mini project for library management system. The problem is that first fgets function for user input in case 1 falls through no matter the content but that of the subsequent ones works. I want to accept the full name of the book name and the author which contains whitespaces from the console.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
// Structure definition
struct library
{
char bookName[50];
char authorName[50];
int numberOfPages;
float price;
};
int main()
{
// Structure Variable declaration
struct library lib[50];
// Variables initialization
int i,j,keepcount;
i=j=keepcount = 0;
// Character arrays
char arth_nm[50],book_nm[50];
while(j!=7)
{
// Menu Selection
printf("\n\n1. Add book information\n2. Display book information\n");
printf("3. List all books of given author\n");
printf("4. List the title of specified book\n");
printf("5. List the count of books in the library\n");
printf("6. Display Highest Price Book\n");
printf("7. Exit");
printf ("\n\nSelect one of the above: \n");
scanf("%d",&j);
switch(j)
{
// Entering book details
case 1:
printf ("Enter book name: ");
fgets(lib[i].bookName, sizeof(lib[i].bookName), stdin);
printf ("Enter author name: ");
fgets(lib[i].authorName, sizeof(lib[i].authorName), stdin);
printf ("Enter pages: ");
scanf ("%d",&lib[i].numberOfPages);
printf ("Enter price: ");
scanf ("%f",&lib[i].price);
keepcount++;
break;
// All book details entered
case 2:
printf("You have entered the following information\n");
for(i=0; i<keepcount; i++)
{
printf ("Book name = %s",lib[i].bookName);
printf ("\tAuthor name = %s",lib[i].authorName);
printf ("\tPages = %d",lib[i].numberOfPages);
printf ("\tPrice = %f",lib[i].price);
printf("\n");
}
break;
// Searching for book details by using the name of the Author
case 3:
printf ("Enter author name : ");
scanf ("%s",arth_nm);
for (i=0; i<keepcount; i++)
{
if (strcmp(arth_nm, lib[i].authorName) == 0)
printf ("%s %s %d %f",lib[i].bookName,lib[i].authorName,lib[i].numberOfPages,lib[i].price);
}
break;
// Searching for book details by using the name of the book
case 4:
printf ("Enter book name : ");
scanf ("%s",book_nm);
for (i=0; i<keepcount; i++)
{
if (strcmp(book_nm, lib[i].bookName) == 0)
printf ("%s \t %s \t %d \t %f",lib[i].bookName,lib[i].authorName,lib[i].numberOfPages,lib[i].price);
}
break;
// Case for Total many of books shelved
case 5:
printf("\n No of books in library : %d", keepcount);
break;
// Case for Highest paid book
case 6:
printf ("Highest Price Book : ");
float temp = 0;
for (i=0;i<keepcount;i++)
{
if(temp < lib[i].price)
temp = lib[i].price;
}
printf("%f", temp);
break;
case 7:
exit (0);
}
}
return 0;
}
Use the fgets() statement for accepting whitespaces:
fgets(arth_nm, 50, stdin);
You should fflush(stdout) it to clear the output buffer like this:
fflush(stdout);
fgets(arth_nm, 50, stdin);
It'll help you to fix this.
yes , there is, as #Rohan Bari said, you can use fgets() , though some compilers does not supports the use of this function. Alternatively, you can use
scanf ("%[^\n]%*c", variableName);
this function takes the input till a new line or enter is pressed, i.e, it will read any character including whitesapces.i hope i've been able to help.

Not able to know the reason of segmentation fault

I want make a tictactoe game program. It's unfinished yet but probably there are some problems that I can't figure out.
#include <stdio.h>
#include <string.h>
play(){
char input[3][3],player1[100],player2[100];
int i,j,k,times;
for (j=0;j<3;j++){
for (k=0;k<3;k++){
input[j][k]='_';
}
}
printf ("How many times do you want to play?\n");
scanf ("%d",&times);
printf ("Enter the name of first player : \n");
scanf ("%s",player1);
printf ("Enter the name of second player : \n");
scanf ("%s",player2);
printf ("Who will enter first letter?" "\n\n1. %s" "\n1. %s" "\n\nEnter 1 or 2 : \n",player1,player2);
scanf ("%d",&i);
for (j=0;j<times;j++){
for (k=0;k<9;k++){
if (i==1){
if (k==0 || k%2==0){
printf("This is %s's chance.",player1);
}
if (k==1 || k%2==1){
printf("This is %s's chance.",player2);
}
}
if (i==2){
if (k==0 || k%2==0){
printf("This is %s's chance.",player2);
}
if (k==1 || k%2==1){
printf("This is %s's chance.",player1);
}
}
printf ("%s %s %s\n\n%s %s %s\n\n%s %s %s\n\n",input[0][0],input[0][1],input[0][2],input[1][0],input[1][1],input[1][2],input[2][0],input[2][1],input[2][2]);
}
}
}
int main(){
int i;
printf("Welcome to TicTacToe made by Saurabh.\n\n1. Play\n2. Help\n\nEnter 1 or 2 : ");
scanf("%d",&i);
switch (i){
case 1:
play();
break;
case 2:
break;
default:
printf ("Invalid response from user.");
}
}
After running scanf("%d",&i) in the play function it says segmention fault but I can't figure out why that's happening. Thanks in advance.
With some prints I have seen there are some problems in this line:
printf ("%s %s %s\n\n%s %s %s\n\n%s %s %s\n\n",input[0][0],input[0][1],input[0][2],input[1][0],input[1][1],input[1][2],input[2][0],input[2][1],input[2][2]);
You defined char input[3][3] so input[0][0] is a single char (and the others with different indexes too) just like if I define char my_str[10];, my_str[0] is a single char.
I don't know if this is what you want but changing to:
printf ("%c %c %c\n\n%c %c %c\n\n%c %c %c\n\n",input[0][0],input[0][1],input[0][2],input[1][0],input[1][1],input[1][2],input[2][0],input[2][1],input[2][2]);
should not generate the error.

C Program error with char

When I execute the program it asks me for my name if I put more than one letter it has an error and it shuts down and in the second one anyting I put it fails and closes instantly
#include <stdio.h>
int main () {
char firstname[20];
char lastname[20];
char response[20];
printf ("Type your first name:\n");
scanf ("'c'",&firstname);
printf ("\n");
printf ("Type your last name:\n");
scanf ("'c'",&lastname);
printf ("\n");
printf ("Hi %s %s do you want to stop giving me information?\nSay Y or N");
scanf ("%s",&response);
if (response == 'Y' || response == 'y'); {
system ("pause<NULL");
}
printf("Thank you for using my program. Good Bye!\n\n");
system ("pause<NULL");
}
There are many issues:
You probably want this (untested code)
#include <stdio.h>
int main () {
char firstname[20];
char lastname[20];
char response[20];
printf ("Type your first name:\n");
scanf ("%s", firstname);
printf ("\n");
printf ("Type your last name:\n");
scanf ("%s", lastname);
printf ("\n");
printf ("Hi %s %s do you want to stop giving me information?\nSay Y or N", firstname, lastname);
scanf ("%s",response);
if (response[0] == 'Y' || response[0] == 'y') {
system ("pause<NULL");
}
printf("Thank you for using my program. Good Bye!\n\n");
system ("pause<NULL");
return 0;
}
There is no 'c' format specifier, it's %s
scanf ("%s", &firstname) is wrong, firstname is already the address of the buffer
response == 'y'is wrong, response is the address of the buffer, you just need the first char of the buffer, that is responde[0]
if (response[0] == 'Y' || response[0] == 'y'); {, there was a stray ; before the {
Your problem is that you are asking for a char input, you want a string.
tutorialspoint has a simlpe and claean example.
https://www.tutorialspoint.com/c_standard_library/c_function_scanf.htm

Write records into file : c [duplicate]

This question already has answers here:
Using fflush(stdin)
(7 answers)
Closed 9 years ago.
int main()
{
FILE *fp;
char another = 'Y';
struct emp
{
char name[20];
int age;
float bs;
};
struct emp e;
fp = fopen("employee.dat", "w");
if(fp == NULL)
{
printf("file cannot be opened for writing\n");
exit(1);
}
while(another == 'Y')
{
printf("\n enter name, age and basic salary: ");
scanf("%s %d %f", e.name, &e.age, &e.bs);
fprintf(fp, "%s %d %f\n", e.name, e.age, e.bs);
printf(" Add another record (Y/N)");
fflush(stdin);
scanf("%c", &another);
}
fclose(fp);
return 0;
In this program I am trying to write records into file named, employee.dat. Program executed fine, but it will takes only one record of employee and then program gets terminated. It is not asking for next record to add i.e.,
fflush(stdin);
scanf("%c", &another);
are not getting executed in the program.
Thanks in advance....
The problem that you're experiencing is that scanf("%c", &another); only grabs a single character from the input buffer. This would be fine, except that there's still a newline left in the input buffer that resulted from hitting 'enter' after your input. You need to clear the input buffer after you use getchar() like this:
char c;
while ((c = getchar()) != '\n');
You can do this:
while(another == 'Y' || another == 'y')
{
printf("\n enter name, age and basic salary: ");
scanf("%s %d %f", e.name, &e.age, &e.bs);
fprintf(fp, "%s %d %f\n", e.name, e.age, e.bs);
printf(" Add another record (Y/N)");
another=getch();
//scanf("%c", &another);
}
You can fixed it simply by use : scanf("\n%c",&another); instead of scanf("%c",&another);
scanf("%s %d %f", e.name, &e.age, &e.bs);
Example--->Here your input is : name_1 22 6000.000 <"Enter">
Then in the buffer : name_1 -->e.name 22-->e.age 6000.00-->e.bs <"Enter">-->nothing
fflush(stdin);//it didn't delete the <"Enter">.
scanf("\n%c", &another);//here we deal with <"Enter">

Resources