Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6852281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:21:08+00:00 2026-05-27T01:21:08+00:00

typedef struct contact { char firstname [40]; char lastname [40]; char address [100]; char

  • 0
typedef struct contact {    

char firstname [40];    
char lastname [40]; 
char address [100]; 
char phone[10];

}contact;

int main ()
{
FILE *pFile;   
contact entry = {"", "", "", ""};
int choice;
char cont = 5;  

pFile = fopen("C:\\contacts.txt", "w+");

if(!pFile){
    printf("File could not be open");
    return 1;
    }

printf("Choose a selection\n\n");
printf("1. Enter First Name\n");
printf("2. Enter Last Name\n");
printf("3. Enter Address\n");
printf("4. Enter Phone Number\n\n");
scanf( "%d", &choice);

while (choice = 1|2|3|4|cont){   
    if (choice = 1){
        printf ("First name: ");      
        fgets(entry.firstname, sizeof(entry.firstname),stdin); 
    }
    else if(choice = 2){
        printf ("Last name: ");   
        fgets(entry.lastname, sizeof(entry.lastname),stdin);      
    }
    else if(choice = 3){
        printf ("Address: ");     
        fgets(entry.address, sizeof(entry.address),stdin);  
    }
    else if (choice = 4){
        printf ("Phone number: ");    
        fgets(entry.phone, sizeof(entry.phone),stdin);
    }
    else
        printf("Exiting");
        break;

    fwrite (&entry, sizeof (struct contact), 1, pFile); 
    printf ("Would you like to enter a new contact? (y/n)");    
    scanf ("%d", &cont);

    if (cont = 'n'|'N')
        return 0;
}

fclose(pFile);

getchar();
return 0;
}

is my code at the moment. Each time I give any option 1,2,3,4, put in a entry and press enter the window closes. I’m unsure if the logic makes sense and any suggestions are welcome but it “seems” okay to me but obviously I need another set of eyes. I want it where I don’t have to enter all entries for every person I put in the file. Also, to note, I initially cont to 5 just because it was complaining.. bad practice I know. Any helpful information is appreciated

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:21:08+00:00Added an answer on May 27, 2026 at 1:21 am

    Your program ends because the break; isn’t in the scope you think it is:

    else if (choice = 4){
        printf ("Phone number: ");    
        fgets(entry.phone, sizeof(entry.phone),stdin);
    }
    else
        printf("Exiting");
        break;
    

    Even though you’ve indented the break, it doesn’t belong to the else clause. So no matter what happens in the if/else block, the break gets executed and your program breaks out of the loop and ends.

    To fix it, add braces to enclose the break inside the scope of the else.:

    else if (choice = 4){
        printf ("Phone number: ");    
        fgets(entry.phone, sizeof(entry.phone),stdin);
    }
    else
    {
        printf("Exiting");
        break;
    }
    

    And once you fix that, this line will cause your program to terminate because it always evaluates to true and returns from main:

    if (cont = 'n'|'N')
        return 0;
    

    You want that line to say

    if (cont == 'n' || cont == 'N')
        return 0;
    

    These fixes will at least stop your program from terminating, but as others have pointed out there are numerous logical errors elsewhere that will prevent it from doing what you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

typedef struct child_list {int count; char vo[100]; child_list*next;} child_list; typedef struct parent_list { char
typedef struct child {int count; char word[100]; inner_list*next;} child; typedef struct parent { char
typedef struct What_if { char price [2]; } what_if ; what_if what_if_var[100]; int format_input_records();
#include <stdio.h> #pragma pack(push) #pragma (1) typedef struct contact { char firstname [40]; char
I have these structs: typedef struct _Frag{ struct _Frag *next; char *seq; int x1;
I have the following construction: typedef struct bucket { char *key; ENTRY *data; struct
typedef struct Value{ int id; char type; char a; } Value; void fooV(Value v){
typedef struct unit_class_struct { char *name; char *last_name; } person; int setName(person *array) {
EDIT: Typedef struct SPro{ int arrivalTime; char processName[15]; int burst; } PRO; I have
I have a struct: typedef struct _n { int type; union { char *s;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.