how to show a word in char? - c

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.

Related

Why am i getting null value when i try to print letter grade? Is it to do with scope?

Here is the code:
I am getting null value when i printf scanf from user for the letter grade.
I am expecting John DOE recived a 95% which is an A.
The following is my ouput
 make -s
 ./main
Enter Student 1:
Last Name: doe
First Name: john
Grade: 95
john doe recieved a 95% on the midterm exam which is a (null)
#include<stdio.h>
int main()
{
int score;
char firstname[20], lastname[20];
char grade =0;
printf("Enter Student 1:\n");
printf("Last Name: ");
scanf("%s", lastname);
printf("First Name: ");
scanf("%s", firstname);
printf("Grade: ");
scanf("%d", &score);
printf("%s %s recieved a %d%% on the midterm exam which is a %s \n", firstname, lastname, score,grade);
printf("Enter Student 2:\n");
printf("Last Name: ");
scanf("%s", lastname);
printf("First Name: ");
scanf("%s", firstname);
printf("Grade: ");
scanf("%d", &score);
printf("%s %s recieved a %d % on the midterm exam which is a %c. \n", firstname, lastname, score,grade);
printf("Enter Student 3:\n");
printf("Last Name: ");
scanf("%s", lastname);
printf("First Name: ");
scanf("%s", firstname);
printf("Grade: ");
scanf("%d", &score);
printf("%s %s recieved a %d % on the midterm exam which is a %c. \n", firstname, lastname, score,grade);
printf("Enter Student 4:\n");
printf("Last Name: ");
scanf("%s", lastname);
printf("First Name: ");
scanf("%s", firstname);
printf("Grade: ");
scanf("%d", &score);
if(score>=93 && score<=100)
grade = "A";
else if(score>=90 && score<=92)
grade = 'A';
else if(score>=87 && score<=89)
grade = 'B';
else if(score>=83 && score<=86)
grade = 'B';
else if(score>=80 && score<=82)
grade = 'B';
else if(score>=77 && score<=79)
grade = 'C+';
else if(score>=73 && score<=76)
grade = 'C';
else if(score>=70 && score<=72)
grade = 'C-';
else if(score>=67 && score<=69)
grade = 'D+';
else if(score>=63 && score<=66)
grade = 'D';
else if(score>=60 && score<=62)
grade = 'D-';
else
grade = 'F';
}
As I understand, the program will run top down, so it means that the grade should be modified before printf.

when i run my code, the output on "room number" will be just null and nothing else

void addadmin() {
FILE *f;
int menu;
int total;
int hargaTriple = 3000000;
int hargaDoubleBed = 2000000;
int hargaTwin = 1500000;
int hargaSingleBed = 1000000;
int a=0;
int i=0;
time_t t;
time(&t);
char test;
f=fopen("add.txt","a+");
if(f==0) {
f=fopen("add.txt","w+");
system("cls");
printf("Please hold on while we set our database in your computer!!");
printf("\n Process completed press any key to continue!!! ");
getch();
}
while(1) {
system("cls");
for(i=0;i<100;i++)
printf("-");
printf("\n Current date and time : %s",ctime(&t));
for(i=0;i<100;i++)
printf("-");
printf("\n");
for(i=0;i<100;i++)
printf("=");
printf("\n");
printf("\t\t\t\t\t|ROOM RESERVATION|\t\t\t\t\t\n");
for(i=0;i<100;i++)
printf("=");
printf("\n");
printf("\n NAME : ");
fflush(stdin);
scanf("%[^\n]%*c", s.name,20);
printf("\n--------------------------------------------------");
printf("\n GENDER [M/F] : ");
scanf_s("%s", s.gender, 2);
printf("\n--------------------------------------------------");
printf("\n AGE : ");
scanf_s("%d", &s.age);
printf("\n--------------------------------------------------");
printf("\n ADDRESS : ");
fflush(stdin);
scanf("%[^\n]%*c", s.address);
printf("\n--------------------------------------------------");
printf("\n PHONE NUMBER : ");
scanf("%s",s.phonenumber);
printf("\n--------------------------------------------------");
printf("\n NATIONALITY : ");
scanf("%s",s.nationality);
printf("\n--------------------------------------------------");
printf("\n E-MAIL ADDRESS : ");
scanf(" %s",s.email);
printf("\n--------------------------------------------------");
printf("\n ROOM TYPE:");
printf("\n------------------------------");
printf("\n Enter 1 -> EXECUTIVE TRIPLE");
printf("\n------------------------------");
printf("\n Enter 2 -> DOUBLE ROOM");
printf("\n------------------------------");
printf("\n Enter 3 -> TWIN ROOM");
printf("\n------------------------------");
printf("\n Enter 4 -> SINGLE ROOM");
printf("\n------------------------------");
printf("\n Enter your choice: ");
scanf_s("%s", s.typeroom, 10);
printf("\n--------------------------------------------------");
printf("\n NUMBER OF ROOM : ");
scanf_s("%d", &s.numberofroom);
printf("\n--------------------------------------------------");
for(a=0; a<s.numberofroom; a++)
printf("\n ROOM NUMBER : %s",s.roomnumber[a]);
fflush(stdin);
printf("\n--------------------------------------------------");
printf("\n PERIOD [DAYS] : ");
scanf_s("%d", &s.period);
printf("\n--------------------------------------------------");
printf("\n ARRIVAL [DD/MM/YYYY] : ");
scanf("%s",&s.arrivaldate);
for(i=0;i<100;i++)
printf("=");
printf("\n");
printf("\n\n ROOM BOOKING DATA:");
printf("\n--------------------------------------------------");
printf("\n NAME : %s", s.name);
printf("\n--------------------------------------------------");
printf("\n GENDER : %s", s.gender);
printf("\n--------------------------------------------------");
printf("\n AGE : %d", s.age);
printf("\n--------------------------------------------------");
printf("\n ADDRESS : %s",s.address);
printf("\n--------------------------------------------------");
printf("\n PHONE NUMBER : %s",s.phonenumber);
printf("\n--------------------------------------------------");
printf("\n NATIONALITY : %s",s.nationality);
printf("\n--------------------------------------------------");
printf("\n E-MAIL ADDRESS : %s",s.email);
printf("\n--------------------------------------------------");
for(a=0; a<s.numberofroom; a++)
printf("\n ROOM NUMBER : %s",s.roomnumber[a]);
printf("\n--------------------------------------------------");
printf("\n ROOM TYPE : %s", s.typeroom);
printf("\n--------------------------------------------------");
printf("\n NUMBER OF ROOM : %d", s.numberofroom);
printf("\n--------------------------------------------------");
printf("\n PERIOD : %d", s.period);
printf("\n--------------------------------------------------");
printf("\n ARRIVAL : %s",&s.arrivaldate);
printf("\n--------------------------------------------------");
I've tried to change roomnumber to numberofrooms but the output is just repetitive room number. The roomnumber was supposed to contain the room numbers but from what I understand, the roomnumber array is just a blank/empty array. So, how to change the output from null to number of rooms that i wanted?

problem in appending multiple records in a file

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.

Why doesn't my for loop work if it contains "scanf("%d", age)" [duplicate]

This question already has answers here:
Why scanf must take the address of operator
(8 answers)
Closed 2 years ago.
Below is my code.
For simplicity I made the code to take the info about 2 friends for now.
But the problem is my for loop in getting user input.
It only works if I remove this part here
printf("Enter Age: ");
scanf("%d", friends[a].age);
here is the full for-loop
for(a=0; a<2; a++) {
printf("Friend no. %d\n", friends[a].fnum+1);
printf("Enter Name: ");
gets(friends[a].name);
printf("Enter Town: ");
scanf("%s", friends[a].address);
printf("Enter Age: ");
scanf(" %d", friends[a].age);
printf("Enter Course: ");
scanf(" %s", friends[a].course);
printf("Favorite foods! \n");
printf("Dish:");
scanf(" %s", friends[a].fav_dish);
printf("Snack:");
scanf(" %s", friends[a].fav_snack);
printf("Drink:");
scanf(" %s", friends[a].fav_drink);
printf("\n");
}
At first, I thought it was the age part was the problem, so I brought it last. But it did not solve it.
I thought it was the scanf too, so I added a space in scanf(" %s", friends[a].course); But nothing work.
I also tried this
printf("Enter Age: ");
scanf(" %d\n ", friends[a].age);
so I tried deleting
printf("Enter Age: ");
scanf("%d", friends[a].age);
and the loop continued to friend 2
What should I do?
Here is my full source code:
#include <string.h>
struct myFriends {
char name[30];
char address[20];
int age;
char course[20];
char fav_dish[20], fav_snack[20], fav_drink[20];
}; struct myFriends friends[2];
int main () {
int a;
puts("Please enter the following info for 2 friends");
for(a=0; a<2; a++) {
printf("Friend no. %d\n", a+1);
printf("Enter Name: ");
scanf("%s", friends[a].name);
printf("Enter Town: ");
scanf("%s", friends[a].address);
printf("Enter Age: ");
scanf(" %d\n ", friends[a].age);
printf("Enter Course: ");
scanf(" %s", friends[a].course);
printf("Favorite foods! \n");
printf("Dish:");
scanf(" %s", friends[a].fav_dish);
printf("Snack:");
scanf(" %s", friends[a].fav_snack);
printf("Drink:");
scanf(" %s", friends[a].fav_drink);
printf("\n");
}
printf("Here are the details");
for (a=0; a<2; a++) {
printf("\n\n Friend no. %d", a+1);
printf("Name:");
puts(friends[a].name);
printf("Town: ");
puts(friends[a].address);
printf("Age: ");
printf("%d", friends[a].age);
printf("Fave FOODS!: ");
puts(friends[a].fav_dish);
puts(friends[a].fav_snack);
puts(friends[a].fav_drink);
}
return 0;
}
In here, I add & mark to pass pointers to scanf, so try this. I hope this works for you.
#include
#include <stdio.h>
#include <string.h>
struct myFriends {
char name[30];
char address[20];
int age;
char course[20];
char fav_dish[20], fav_snack[20], fav_drink[20];
}; struct myFriends friends[2];
int main () {
int a;
puts("Please enter the following info for 2 friends");
for(a=0; a<2; a++) {
char name[30];
char address[20];
int age;
char course[20];
char fav_dish[20], fav_snack[20], fav_drink[20]
printf("Friend no. %d\n", a+1);
printf("Enter Name: ");
scanf("%s", &name);
friends[a].name=name;
printf("Enter Town: ");
scanf("%s", &address);
friends[a].address=address;
printf("Enter Age: ");
scanf(" %d\n ", &age);
friends[a].age=age;
printf("Enter Course: ");
scanf(" %s", &course);
friends[a].course=course;
printf("Favorite foods! \n");
printf("Dish:");
scanf(" %s", &fav_dish);
friends[a].fav_dish=fav_dish;
printf("Snack:");
scanf(" %s", &fav_snack);
friends[a].fav_snack=fav_snack;
printf("Drink:");
scanf(" %s", &fav_drink);
friends[a].fav_drink=fav_drink;
printf("\n");
}
printf("Here are the details");
for (a=0; a<2; a++) {
printf("\n\n Friend no. %d", a+1);
printf("Name:");
puts(friends[a].name);
printf("Town: ");
puts(friends[a].address);
printf("Age: ");
printf("%d", friends[a].age);
printf("Fave FOODS!: ");
puts(friends[a].fav_dish);
puts(friends[a].fav_snack);
puts(friends[a].fav_drink);
}
return 0;
}

How to reset do while loop within while loop?

#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);

Resources