d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Love Calclutor Program Development Error
Add Reply New Topic New Poll
Banned
Posts: 14
Joined: Jan 16 2023
Gold: 0.00
Jun 15 2023 03:16am
Hello, this is Gulshan Negi
Well, I am writing a program for making love calculator in C but it shows some error at the time of its execution.
Here is my source code:

#include<ctype.h>
#include<stdio.h>
#include <conio.h>
#include<string.h>

int Sum_of_Digits(int digit)
{
int sum =0;
while(digit > 0)
{
sum +=(digit%10);
digit/=10;
}
return sum;
}

void main()
{
char name[100], partner[100];
int p,ns = 0, ps =0,love =54,i,j,love_percentage, opt;
clrscr();

do
{
printf("Enter Your Name: ");
gets(name);

printf("Enter Your Partner Name: ");
gets(partner);

for(i =0; i<strlen(name); i++)
{
ns += (tolower(name)-96);
}

for(j = 0; partner[j] != '\0'; j++)
{
ps+=(tolower(partner[j]) 96);
}
p= Sum_of_Digits(ns);
love_percentage = (Sum_of_Digits(ns)+Sum_of_Digits(ps)+love);
printf("The Love Percentage is %d", love_percentage);
printf("\nPress 1 To continue or 0 to Exit: ");
scanf("%d",&opt);

}while(opt!=0);
getch();
}

Well, I also checked and took a reference from [URL]https://www.techgeekbuzz.com/blog/c-program-to-design-love-calculator/[/URL], but still there is same problem while execution.
Thanks


Member
Posts: 1,178
Joined: Jun 12 2010
Gold: 79,289.70
Jun 21 2023 02:29am
You seem to have a few small issues

1. remember gets will overflow the name and partner buffers,
2. it seems like ns only ever looks at the first character of name because it is missing the array index
3. minor - p=... is unused

here are some minor updates

Code
#include <ctype.h>
#include <stdio.h>
#include <string.h>

int Sum_of_Digits(int digit) {
int sum = 0;
while (digit > 0) {
sum += (digit % 10);
digit /= 10;
}
return sum;
}

int main() {
char name[100], partner[100];
int ns = 0, ps = 0, love = 54, i, j, love_percentage, opt;

do {
printf("Enter Your Name: ");
[B]fgets(name, 100, stdin);[/B]

printf("Enter Your Partner Name: ");
[B]fgets(partner, 100, stdin);[/B]

for (i = 0; i < strlen(name); i++) {
[B]ns += (tolower(name[i]) - 96);[/B]
}

for (i = 0; i < strlen(partner); i++) {
ps += (tolower(partner[i]) - 96);
}

love_percentage = Sum_of_Digits(ns) + Sum_of_Digits(ps) + love;
printf("The Love Percentage is %d\n", love_percentage);

printf("\nPress 1 To continue or 0 to Exit: ");
scanf("%d", &opt);
while (getchar() != '\n');

} while (opt != 0);

return 0;
}


This post was edited by iceisfun on Jun 21 2023 02:29am
Banned
Posts: 14
Joined: Jan 16 2023
Gold: 0.00
Jun 23 2023 11:10am
@iceisfun, thanks a lot for your kind response.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll