#include <stdio.h>
int main()
{
int comp,loop=1,tcomp=0;
char cont;
char name[50];
float donate=0,total,gtotal;
printf("\nGot representative? [Y to continue]: ");
scanf("%s", &cont);
while(cont=='y'){
printf("\nRepresentative name : ");
scanf("%s", &name);
printf("How many companies? : ");
scanf("%d", &comp);
tcomp+=comp;
do{
printf("Enter amount of donation : ");
scanf("%f", &donate); loop++;
total+=donate;}
while(loop<=comp);
printf("%s : %.2f\n", name, total);
printf("\nGot representative? [Y to continue]: ");
scanf("%s", &cont);}
printf("\nTotal Representative : %d", tcomp);
gtotal+=total;
printf("\nTotal Donations : %.2f\n", gtotal);
}
Current output :
Got representative? [Y to continue]: y
Representative name : ABC
How many companies? : 3
Enter amount of donation : 1
Enter amount of donation : 2
Enter amount of donation : 3
ABC : 6.00
Got representative? [Y to continue]: y
Representative name : ZXC
How many companies? : 3
Enter amount of donation : 1
ZXC : 7.00
As you can see here, the 2nd loop did not reset and it's summing the numbers of first loop. How do I rectify this? How do I make it that the loop starts fresh each time? p/s : I was asked specifically to use while and do while loop.
Below your corrected code.
int main()
{
int comp,loop=1,tcomp=0;
char cont;
char name[50];
float donate=0,total=0,gtotal=0;
printf("\nGot representative? [Y to continue]: ");
scanf("%c", &cont);
while(cont=='y')
{
printf("\nRepresentative name : ");
scanf("%s", name);
printf("How many companies? : ");
scanf("%d", &comp);
tcomp+=comp;
loop=0;
total=0;
do
{
printf("Enter amount of donation : ");
scanf("%f", &donate); loop++;
total+=donate;
}
while(loop<comp);
printf("%s : %.2f\n", name, total);
printf("\nGot representative? [Y to continue]: ");
scanf("%c", &cont);
gtotal+=total;
}
printf("\nTotal Representative : %d", tcomp);
printf("\nTotal Donations : %.2f\n", gtotal);
return 0;
}
As you can see:
You must reset loop variable before each loop where amount are asked.
You must reset total before that loop
You must save to gtotal for each iteration of first while loop.
You must use %c instead of %s to get a single char: scanf("%c", &cont);
Related
This code is about structures and includes basic input from the user but somehow that is not working properly with numeric values such as age, salary etc. I have tried fflush() but got no luck from using that and I can't find any other solution on the internet, everything except the garbage value works like a charm in this code
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct employee
{
unsigned int id;
char name[30];
int age;
char address[50];
char department[30];
unsigned int salary;
};
void main()
{
int i,num;
struct employee emp[50];
printf("\nEnter number of employees whose data you want to enter : ");
scanf("%d", &num);
for(i=0;i<num;i++)
{
printf("\n||| EMPLOYEE NUMBER %d |||\n",i+1);
printf("\nEnter your employee ID (5 Digits) : ");
fflush(stdin);
scanf("%u", &emp[i].id);
printf("\nEnter your name : ");
fflush(stdin);
gets(emp[i].name);
printf("\nEnter your age : ");
fflush(stdin);
scanf("%d", &emp[i].age);
printf("\nEnter your address : ");
fflush(stdin);
gets(emp[i].address);
printf("\nEnter your department : ");
fflush(stdin);
gets(emp[i].department);
printf("\nEnter your salary : ");
fflush(stdin);
scanf("%u", &emp[i].salary);
}
for(i=0;i<num;i++)
{
printf("\n||| EMPLOYEE %d DETAILS ARE |||", i+1);
printf("\nID : %u", &emp[i].id);
printf("\nNAME : %s", emp[i].name);
printf("\nAGE : %d", &emp[i].age);
printf("\nADDRESS : %s", emp[i].address);
printf("\nDEPARTMENT : %s", emp[i].department);
printf("\nSALARY : %u", &emp[i].salary);
}
getch();
}
USER INPUT
||| EMPLOYEE NUMBER 1 |||
Enter your employee ID (5 Digits) : 12345
Enter your name : The Crow
Enter your age : 24
Enter your address : 25/45 New York
Enter your department : HR
Enter your salary : 23000
OUTPUT
||| EMPLOYEE 1 DETAILS ARE |||
ID : 6612448
NAME : The Crow
AGE : 6612484
ADDRESS : 25/45 New York
DEPARTMENT : HR
SALARY : 6612568
Input:
Output:
You are printing the address of id, age, and salary. Delete the & in front of them in your printf statements to print their values.
printf("\n||| EMPLOYEE %d DETAILS ARE |||", i+1);
printf("\nID : %u", emp[i].id);
printf("\nNAME : %s", emp[i].name);
printf("\nAGE : %d", emp[i].age);
printf("\nADDRESS : %s", emp[i].address);
printf("\nDEPARTMENT : %s", emp[i].department);
printf("\nSALARY : %u", emp[i].salary);
On another note, main has return type int so you should define it like this
int main(int argc, char** argv) {
// your code goes here
return 0;
}
As #pm100 mentioned in the comments:
Never use gets. Use fgets.
Read the outputs of the compiler carefully. They are often very informative.
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
How to read / parse input in C? The FAQ
(1 answer)
Closed 1 year ago.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
struct date
{
int d, m, y;
};
struct student
{
char name[50];
int rollno;
char divi[50];
float marks;
char mobile_no[50];
};
struct st2
{
char name2[40];
int rollno_1;
char divi_1[40];
float marks_1;
char mobile_no_1[40];
};
struct date_1
{
int d, m, y;
};
int main()
{
struct student s;
printf("Enter the first student information: ");
printf("\nEnter the name of the student: ");
scanf("%[^\n]", s.name);
printf("Enter the roll numner of the student: ");
scanf("%d", &s.rollno);
printf("Enter the division of the student: ");
scanf("%s", s.divi);
printf("Enter the marks of the student: ");
scanf("%f", &s.marks);
printf("Enter the mobile number of the student: ");
scanf("%s", s.mobile_no);
struct date d;
printf("enter the student Date Of Birth: \n");
printf("DAY: ");
scanf("%d", &d.d);
printf("MONTH: ");
scanf("%d", &d.m);
printf("YEAR: ");
scanf("%d", &d.y);
printf("----------[%s] racord----------\n", s.name);
printf("| student name is: %s\n", s.name);
printf("| student roll no is: %d\n", s.rollno);
printf("| student division is: %s\n", s.divi);
printf("| student marks is: %f\n", s.marks);
printf("| student nmobile number is: %s\n", s.mobile_no);
printf("| student birth date is: %d/%d/%d\n", d.d, d.m, d.y);
struct st2 f;
printf("Enter the Second student information: ");
printf("\nEnter the name of the student: ");
scanf(" %[^\n]",f.name2); // added space before %[^\n]
printf("Enter the roll number of the student: ");
scanf("%d", &f.rollno_1);
printf("Enter the division of the student: ");
scanf("%s", f.divi_1);
printf("Enter the marks of the student: ");
scanf("%f", &f.marks_1);
printf("Enter the mobile number of the student: ");
scanf("%s", f.mobile_no_1);
struct date_1 b;
printf("enter the student Date Of Birth: \n");
printf("DAY: ");
scanf("%d", &b.d);
printf("MONTH: ");
scanf("%d", &b.m);
printf("YEAR: ");
scanf("%d", &b.y);
printf("----------[] racord----------\n");
printf("| student name is: %s\n", f.name2);
printf("| student roll no is: %d\n", f.rollno_1);
printf("| student division is: %s\n", f.divi_1);
printf("| student marks is: %f\n", f.marks_1);
printf("| student nmobile number is: %s\n", f.mobile_no_1);
printf("| student birth date is: %d/%d/%d", b.d, b.m, b.y);
return 0;
}
Output:
Enter the first student information:
Enter the name of the student: can yaman
Enter the roll numner of the student: 72
Enter the division of the student: a
Enter the marks of the student: 98
Enter the mobile number of the student: 6244738358
enter the student Date Of Birth:
DAY: 31
MONTH: 5
YEAR: 1993
----------[can yaman] racord----------
| student name is: can yaman
| student roll no is: 72
| student division is: a
| student marks is: 98.000000
| student nmobile number is: 6244738358
| student birth date is: 31/5/1993
Enter the Second student information:
Enter the name of the student: Enter the roll number of the student:
I am having problem with appending more records to this file. It allows me add just one record but I cannot add more than one record. And cannot figure out what is going wrong with it?
void new_customer()
{
char ch;
int flag=0;
FILE *fp;
fp=fopen("DataFile.txt", "a+");
printf("Enter today's date (dd/mm/yyyy) : ");
scanf(" %d/%d/%d", &add.deposit.day, &add.deposit.month, &add.deposit.year);
printf("Enter Account Number : ");
fflush(stdin);
scanf("%ld", &check.account_number);
while(fscanf(fp, "%ld %s %s %s %s %s %d %d/%d/%d %d %d/%d/%d %c", &add.account_number, add.customer_name, add.father_name, add.address, add.Nationality, &add.p_number, &add.age, &add.dob.day, &add.dob.month, &add.dob.year, &add.amount, &add.deposit.day, &add.deposit.month, &add.deposit.year, &add.account_type)!=EOF)
{
if(check.account_number==add.account_number)
{
printf("Account number already taken. Please contact administrator.\nPress enter to continue.");
getch();
system("cls");
main();
}
}
add.account_number=check.account_number;
printf("Enter name : ");
fflush(stdin);
gets(add.customer_name);
printf("Enter Father's name : ");
fflush(stdin);
gets(add.father_name);
printf("Enter your age : ");
fflush(stdin);
scanf("%d", &add.age);
printf("Enter Date of birth (dd/mm/yyyy) : ");
scanf("%d/%d/%d", &add.dob.day, &add.dob.month, &add.dob.year);
printf("Enter Phone Number : ");
fflush(stdin);
gets(add.p_number);
printf("Enter Nationality : ");
fflush(stdin);
gets(add.Nationality);
printf("Enter Address : ");
fflush(stdin);
gets(add.address);
printf("Enter Account Type:\nPress S for Savings, \nPress C for Current, \nF for Fixed : ");
fflush(stdin);
scanf("%c",&add.account_type);
while(flag!=1)
{
if (add.account_type=='S'|| add.account_type=='s'||add.account_type=='C'||add.account_type=='c'||add.account_type=='F'||add.account_type=='f')
{
flag=1;
}
else
{
printf("\nWrong Input. Input Again : ");
fflush(stdin);
scanf("%c", &add.account_type);
flag=0;
}
}
printf("Deposit Amount : ");
fflush(stdin);
scanf("%d", &add.amount);
fprintf(fp, "%ld %s %s %s %s %s %d %d/%d/%d %d %d/%d/%d %c\n", add.account_number, add.customer_name, add.father_name, add.address, add.Nationality, add.p_number, add.age, add.dob.day, add.dob.month, add.dob.year, add.amount, add.deposit.day, add.deposit.month, add.deposit.year, add.account_type);
printf("\nAccount Created Successfully!!\n");
fclose(fp);
while(1)
{
printf("Return to Main Menu? Y/N : ");
fflush(stdin);
scanf("%c", &ch);
if(ch=='Y' || ch=='y')
{
system("cls");
main();
}
else if(ch=='N' || ch=='n')
{
exit(0);
}
else
{
printf("\nWrong input. Try Again!\n");
}
}
}
This is just a function to a big program. I am attaching just the part which includes file handling. If you want I can attach more code.
Here I am adding the main driver code
#include<windows.h>
#include<stdio.h>
#include<stdlib.h>
#include "E:\Projects\C Language\Bank-Management-System\File Containing Functions.c"
int menu(void);
int gotoxy(int x, int y)
{
COORD c;
c.X=x;
c.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}
int menu()
{
int i, a;
gotoxy(40,0);
printf("BANK MANAGEMENT SYSTEM\n");
gotoxy(43, 3);
printf(":: MAIN MENU ::\n");
gotoxy(40, 5);
printf("[1] Create a new account");
gotoxy(40, 6);
printf("[2] Update information of existing account");
gotoxy(40, 7);
printf("[3] Transactions");
gotoxy(40, 8);
printf("[4] Check details of existing account");
gotoxy(40, 9);
printf("[5] Remove existing account");
gotoxy(40, 10);
printf("[6] View Customer List");
gotoxy(40, 11);
printf("[7] Exit\n");
gotoxy(40, 15);
printf("Enter your choice : ");
scanf("%d", &a);
return a;
}
int main()
{
int choice;
choice=menu();
switch(choice)
{
case 1:
{
system("cls");
new_customer();
break;
}
case 3:
{
system("cls");
transaction();
break;
}
case 7 :
{
system("cls");
printf("Thank You for using our services!!");
exit(0);
}
default:
{
printf("Wrong Input!!\n");
getch();
system("cls");
menu();
}
}
getch();
return 0;
}
Link to see input : https://pasteboard.co/Jt3xWrP.jpg
Here is the file after first input : https://pasteboard.co/Jt3yYA9.jpg
Another input :https://pasteboard.co/Jt3yHSC.jpg (this is where it gets stuck forever and doesn't let me add another record)
Text stored inside file :
123 John Papa John 15, Yemen Road, Yemen USA 12345678 22 11/2/0 2000 27/9/2020 S
So to start with the file holds:
123 John Papa John 15, Yemen Road, Yemen USA 12345678 22 11/2/0 2000 27/9/2020 S
And you try to scan it with
fscanf(fp, "%ld %s %s %s %s %s %d %d/%d/%d %d %d/%d/%d %c"
That is:
1 number
5 strings
1 number
...
As you can see, the file doesn't match that.
123 John Papa John 15, Yemen Road, Yemen USA 12345678 22 11/2/0 2000 27/9/2020 S
^^^ ^^^ ^^^ ^^^ ^^^ ^^^ ^^^
ok ok ok ok ok ok Not ok, so stop here
The next fscanf will not match anything (it expects a number but the file has "Road") so nothing is read.
In other words, you are stuck in the while forever.
I'll recommend reading the file in a line by line manner using fgets.
Then you can (in principle) use sscanf afterwards. But notice that %s reads a single word and your code allow multiple words for a single entry! In other words - your file can't be parsed using %s.
So consider another file format. For instance, you could use 1 line for each item in a record. That will make parsing much easier.
I have to write a program in C for a school project that registers a geriatric client, this is my code:
int position=0, correctData=0, day, month, year, phone, iRS, exercises;
float height, weight;
char confirmation, name[100], address[100], numberToCheck[9];
for(int i=0; Client[i]->name!="NULL";i++){
position++;
if(i==30){
break;
}
}
do{
fflush(stdin);
system("cls");
printf("\n\tINSERT NEW CLIENT");
printf("\nInsert client name: ");
gets(name);
printf("\nInsert client address: ");
gets(address);
printf("\nInsert client birthday(dd/mm/yyyy): ");
scanf("%d/%d/%d", &day,&month,&year);
do{
fflush(stdin);
strcpy(numberToCheck, "");
printf("\nInsert client phone number: ");
gets(numberToCheck); //THIS IS THE LINE ERASING MY ADDRESS VARIABLE
if(isPhone(numberToCheck)==0){
printf("\nNumber not valid");
}
}while(phone(numberToCheck)==0);
phone= validateInput(numberToChek);
do{
fflush(stdin);
strcpy(numberToCheck, "");
printf("\nInsert client tax number: ");
gets(numberToCheck);
if(isIRS(numberToCheck)==0){
printf("\nNumber not valid");
}
}while(isIRS(numberTocheck)==0);
iRS= validateInput(numberToCheck);
printf("\nInsert client height: ");
scanf("%f", &height);
printf("\nInsert client weight: ");
scanf("%f", &weight);
printf("\nInsert the number of exercises made: ");
scanf("%d", &exercise);
fflush(stdin);
system("cls");
printf("Name : %s", name);
printf("\nAddress : %s", address);
printf("\nBirthday : %02d/%02d/%04d", dia, mes, ano);
printf("\nPhone number : %d", telefone);
printf("\IRS : %d", contribuinte);
printf("\nHeight and Weight : %.2fm e %.2fKg", height, weight);
printf("\nNumber of exercises : %d", exercise);
printf("\n\nIs the data correct??(Y/N): ");
scanf("%c", &confirmation);
if(confirmation=='Y' || confirmation=='y'){
strcpy(Client[position]->name, name);
strcpy(Client[position]->address, address);
Client[position]->dayBirth = day;
Client[position]->monthBirth = month;
Client[position]->yearBirth = year;
Client[position]->phone = phone;
Client[position]->iRS = iRS;
Client[position]->height= height;
Client[position]->weight= weight;
Client[position]->exercise = exercise;
correctData = 1;
}
}while(correctData==0);
printf("\nUser registered");
printf("\n\nPress Enter to continue");
system("pause >nul /nobreak");
I receive by reference the array Client[100] so that i can modify its elements.
In the part of the code to check if it's a phone number/iRS number i use a pre-coded submodule to validate it.
The problem I'm having with the code is that it stores the address of the client but when the program receives the numberToCheck to check if it is a phone number the address variable is cleaned, becomes empty and I don't see why.
I appreciate any help and time given to help me solve the problem
First, the program wants you to enter the student name. When I want to show the student's name in the bottom with %s, xcode always tells me to replace with %c. Can you give me the solutions how to show the student name as it was entered, not using %c? Thank you.
#include <stdio.h>
void showthelastvalue () {
char name1, name2, name3, name4;
int value1a, value1b, value1c, value1d;
int value2a, value2b, value2c, value2d;
int value3a, value3b, value3c, value3d;
int value4a, value4b, value4c, value4d;
printf("\nenter student name-1 : ");
scanf(" %s", name1);
printf("enter student name-2 : ");
scanf(" %s", name2);
printf("enter student name-3 : ");
scanf(" %s", name3);
printf("enter student name-4 : ");
scanf(" %s", name4);
printf("\nEnter student grade-1 %s\n", name1);
printf("grade ke 1 : ");
scanf("%d", &grade1a);
printf("grade ke 2 : ");
scanf("%d", &grade1b);
printf("grade ke 3 : ");
scanf("%d", &grade1c);
printf("grade ke 4 : ");
scanf("%d", &grade1d);
printf("\nEnter student grade- 2 %s\n", name2);
printf("grade ke 1 : ");
scanf("%d", &grade2a);
printf("grade ke 2 : ");
scanf("%d", &grade2b);
printf("grade ke 3 : ");
scanf("%d", &grade2c);
printf("grade ke 4 : ");
scanf("%d", &grade2d);
printf("\nEnter student grade- 3 %s\n", name3);
printf("grade ke 1 : ");
scanf("%d", &grade3a);
printf("grade ke 2 : ");
scanf("%d", &grade3b);
printf("grade ke 3 : ");
scanf("%d", &grade3c);
printf("grade ke 4 : ");
scanf("%d", &grade3d);
printf("\nEnter student grade- 4 %s\n", name4);
printf("grade ke 1 : ");
scanf("%d", &grade4a);
printf("grade ke 2 : ");
scanf("%d", &grade4b);
printf("grade ke 3 : ");
scanf("%d", &grade4c);
printf("grade ke 4 : ");
scanf("%d", &grade4d);
printf("\nThe grade of 4 students: \n");
printf(" %s %d %d %d %d\n", name1, grade1a, grade1b, grade1c, grade1d);
printf(" %s %d %d %d %d\n", name2, grade1a, grade1b, grade1c, grade1d);
printf(" %s %d %d %d %d\n", name3, grade1a, grade1b, grade1c, grade1d);
printf(" %s %d %d %d %d\n", name4, grade1a, grade1b, grade1c, grade1d);
averange1 = (grade1a + grade1b + grade1c + grade1d) / 4;
averange2 = (grade2a + grade2b + grade2c + grade2d) / 4;
averange3 = (grade3a + grade3b + grade3c + grade3d) / 4;
averange4 = (grade4a + grade4b + grade4c + grade4d) / 4;
printf("Last grade of 4 studentsgra :\n");
printf("Last grade from student1 %s = %d\n", name1, averange1);
printf("Last grade from student2 %s = %d\n", name2, averange2);
printf("Last grade from student3 %s = %d\n", name3, averange3);
printf("Last grade from student4 %s = %d\n", name4, averange4);
}
int main() {
int choose;
printf("Welcome!\n");
do {
printf("Choose anda :\n");
printf("1. Show the last grade\n");
printf("2. Show the grade\n");
printf("3. Show the Table\n");
printf("4. Exit\n");
printf("\nWhat will you choose ? ");
scanf("%d", &choose);
switch (choose) {
case 1:
showthelastgrade ();
break;
case 4:
printf("Thank you /001\n");
break;
}
}
while (choose != 5);
return 0;
}
You are reading the names into single chars, which is why xcode is giving you that message. You need to use an array of chars.
The simplest way to do this would be to just change your declarations to:
char name1[N], name2[N], name3[N], name4[N];
where N is the maximum length a name can be.
You would then change all the scanf to be like:
scanf(" %s", name1); // & removed
as name1 etc. now point to the start of the char arrays.
Note that this is inherently a risky thing to do, as it means that the buffer can be overflown by inputting a name that is too long.
You would be better off using fgets, as it allows you to specify the maximum length of the buffer:
fgets(name1, N, stdin);
Anything longer than N-1 characters will be discarded, rather than overflowing your buffer.
To print a char you use %c.
To print a string or array-of-char you use %s
You can use array of char char name[30] or char pointer char *name, and then use %s to print.
I have not looked into the logic of your program but make the following amendments and it should work fine.
#include <stdio.h>
//The maximum allowed name size, you can change it to whatever you want
#define MAX 10
void showthelastvalue () {
char name1[MAX], name2[MAX], name3[MAX], name4[MAX];
//You need character arrays here, simple char would store only a single byte
int value1a, value1b, value1c, value1d;
int value2a, value2b, value2c, value2d;
int value3a, value3b, value3c, value3d;
int value4a, value4b, value4c, value4d;
printf("insert student name 1 : ");
gets(name1); //scanf(" %s", &name1);
printf("insert student name 2 : ");
gets(name2); //scanf(" %s", &name2);
printf("insert student name 3 : ");
gets(name3); //scanf(" %s", &name3);
printf("insert student name 4 : ");
gets(name4); //scanf(" %s", &name4);
printf("Insert student value-1 %c\n", name1);
printf("value ke 1 : ");
scanf("%d", &value1a);
printf("value ke 2 : ");
scanf("%d", &value1b);
printf("value ke 3 : ");
scanf("%d", &value1c);
printf("value ke 4 : ");
scanf("%d", &value1d);
printf("Insert student value- 2 %c\n", name2);
printf("value ke 1 : ");
scanf("%d", &value2a);
printf("value ke 2 : ");
scanf("%d", &value2b);
printf("value ke 3 : ");
scanf("%d", &value2c);
printf("value ke 4 : ");
scanf("%d", &value2d);
printf("Insert student value- 3 %c\n", name3);
printf("value ke 1 : ");
scanf("%d", &value3a);
printf("value ke 2 : ");
scanf("%d", &value3b);
printf("value ke 3 : ");
scanf("%d", &value3c);
printf("value ke 4 : ");
scanf("%d", &value3d);
printf("Insert student value- 4 %c\n", name4);
printf("value ke 1 : ");
scanf("%d", &value4a);
printf("value ke 2 : ");
scanf("%d", &value4b);
printf("value ke 3 : ");
scanf("%d", &value4c);
printf("value ke 4 : ");
scanf("%d", &value4d);
printf("Jadi value dari 4 mahasiswa sbb : \n");
printf(" %s %d %d %d %d", name1, value1a, value1b, value1c, value1d);
printf(" %s %d %d %d %d", name2, value1a, value1b, value1c, value1d);
printf(" %s %d %d %d %d", name3, value1a, value1b, value1c, value1d);
printf(" %s %d %d %d %d", name4, value1a, value1b, value1c, value1d);
}
int main() {
int choose;
printf("Welcome!\n");
do {
printf("Choose anda :\n");
printf("1. Show the last value\n");
printf("2. Show Grade\n");
printf("3. Show the Table\n");
printf("4. Exit\n");
printf("What you will choose ? ");
scanf("%d", &choose);
switch (choose) {
case 1:
showthelastvalue();
break;
case 4:
printf("Thank you /001\n");
break;
}
}
while (choose != 5);
return 0;
}
Some explanation to what you did wrong: You had defined the variables as 'char' which would mean that your program is expecting a single byte. Also, use 'gets()' function instead of using scanf to store strings. '%s' only works with character arrays in printf.
You are using char name1,name2,name3,name4
replace it with
char name1[N],name2[N],name3[N],name4[N]
then use %s instead of %c.
and then use gets() function instead of using scanf() function.
then try it i hope it will run perfectly.