In: Computer Science
please code in c language and follow all instructions and sample run.
Simone works for a group that wants to register more people to
vote. She knows her team can only ask a
certain number of people at any given time since they have other
obligations. She has asked you to create a
program for her team that allows the user to type in how many
people they want to ask at one time (the
duration of the current program run). The program will then ask
that many people (see sample run). If a
person answers y, his or her name is added to a registration list
(a file) and the current registration list is
output to screen. If they answer n, Ok is output to screen and the
program continues. Once at least 10 people
have been registered, whenever the program is opened the phrase
Target Reached! Exiting… should be
output to screen.
Step 1: You will be defining the following two functions (DO NOT
MODIFY THE DECLARATIONS):
/*This function takes a file pointer, goes through the file and
prints the file contents to screen. It returns the
number of lines in the file*/
int registered(FILE *fp)
/*This function takes a file pointer and adds a new line to the
file*/
void new_register(FILE *fp)
Step 2: Use the functions you defined to make a working
program.
Sample Run:
computer$ gcc –o vote vote.c
computer$ ./vote register.txt
***Registered so far:***
How many people to ask right now?
3
-Person 1: Would you like to register to vote?
n
Ok.
-Person 2: Would you like to register to vote?
y
Enter name: Bill Gates
Adding: Bill Gates
***Registered so far:***
1. Bill Gates
-Person 3: Would you like to register to vote?
y
Enter name: Mark Zuckerberg
Adding: Mark Zuckerberg
***Registered so far:***
1. Bill Gates
2. Mark Zuckerberg
3 people asked! Taking a break.
computer$ ./vote register.txt
***Registered so far:***
1. Bill Gates
2. Mark Zuckerberg
How many people to ask right now?
4
-Person 1: Would you like to register to vote?
n
Ok.
-Person 2: Would you like to register to vote?
n
Ok.
-Person 3: Would you like to register to vote?
y
Enter name: Jeff Bezos
Adding: Jeff Bezos
***Registered so far:***
1. Bill Gates
2. Mark Zuckerberg
3. Jeff Bezos
-Person 4: Would you like to register to vote?
y
Enter name: Susan Wojcicki
Adding: Susan Wojcicki
***Registered so far:***
1. Bill Gates
2. Mark Zuckerberg
3. Jeff Bezos
4. Susan Wojcicki
4 people asked! Taking a break.
#include<stdio.h>
char names[100],chr ;
int people,i=0,lines = -1,out,c=0;
char response='y';
FILE *fp;
char currentName[100];
void registered();
int main(){
printf("How many people to ask right now? ");
scanf("%d",&people);
fp = fopen("vote register.txt","w");
fputs("***Registered so far.***\n",fp);
while(i!= people){
printf("People %d:", i+1);
printf("Would you like to register for vote?");
scanf("%c",&response);
if(response == 'y'){
printf("Enter your first name:");
scanf("%s", &names);
fputs(names, fp);
new_register();
i += 1;
}
if(response == 'n')
i += 1;
}
printf("%d people asked. Take a break. \n",people);
registered();
printf("\nNumber of people registered %d",lines);
}
void new_register(){
fputs("\n", fp);
}
void registered(){
fclose(fp);
fp = fopen("vote register.txt", "r");
chr = getc(fp);
while (chr != EOF)
{
printf("%c", chr);
if (chr == '\n')
{
lines =lines + 1;
}
chr = getc(fp);
}
}