Program not printing (cs50 cash) - c

#include <stdio.h>
#include <cs50.h>
int cents;
int main(void)
{
do
{
// Prompt the user for input
cents = get_int("Change owed: ");
}
while (cents <= 0);
return cents;
int count_coins();
}
int count_coins() // count the amount of coins
{
int coins;
for(coins = 0; cents == 0; coins++)
{
// count quarters
if (cents >= 25)
{
cents = cents - 25;
return coins;
}
// count dimes
else
if (cents >= 10)
{
cents = cents - 10;
return coins;
}
// count nickels
else
if (cents >= 5)
{
cents = cents - 5;
return coins;
}
// count pennies
else
if (cents >= 1)
{
cents = cents - 1;
return coins;
}
while (cents == 0)
{
// print total amount of coins
printf("%i" , coins);
}
}
return cents;
}
I'm trying to do the cs50 pset 1 problem cash. When I run this code, it asks for an input and successfully keeps asking for input until I give it a positive number, but after that, it doesn't print the number of coins. This is my first-time programming anything, so any constructive criticism is more than welcome.

Related

CS50 credit doesn't run for some numbers

My code seems to work on some credit card numbers but for others it doesn't even run. I've tried to use long long for the credit card number but it didn't worked. I've spent like 2 hours trying to solve this issue but I can't figure it out. All help is welcomed :)
int main(void)
{
// Get credit card number
long num = get_cc_number();
// Check if the credit card number is valid
valid = check_sum(num);
// Check the length of the credit card number
length = check_length(num);
// Get the first two digits of the credit card number and first number of VISA
digits = get_first_digits(num);
digit_visa = digits / 10;
// Check if the card is American Express, Mastercard, Visa or Invalid
if (valid == 1)
{
if (length == 16)
{
if (digits <= 55 && digits >= 51)
{
printf("MASTERCARD\n");
}
else if (digit_visa == 4)
{
printf("VISA\n");
}
else
{
printf("INVALID\n");
}
}
else if (length == 15)
{
if (digits == 34 || digits == 37)
{
printf("AMEX\n");
}
else
{
printf("INVALID\n");
}
}
else if (length == 13)
{
if (digit_visa == 4)
{
printf("VISA\n");
}
else
{
printf("INVALID\n");
}
}
}
else
{
printf("INVALID\n");
}
}
long get_cc_number(void)
{
long cc_number;
cc_number = get_long("Credit Card Number: ");
return cc_number;
}
int check_sum(int num)
{
int num1 = num;
while (num1 >= 10)
{
sec_to_last = num1 % 100;
double_sec_to_last = sec_to_last * 2;
if (double_sec_to_last >= 10)
{
first_dig = double_sec_to_last / 10;
sec_dig = double_sec_to_last % 10;
first_sum += first_dig;
first_sum += sec_dig;
}
else
{
first_sum += double_sec_to_last;
}
num1 = num1 / 100;
}
int num2 = num;
while (num2 >= 10)
{
last = num2 % 10;
second_sum += last;
}
second_sum += first_sum;
if (second_sum % 10 == 0)
{
return 1;
}
else
{
return 0;
}
}
long check_length(long num)
{
long num_length = floor(log10(labs(num))) + 1;
return num_length;
}
long get_first_digits(long num)
{
long i = num;
while (i >= 100)
{
i /= 10;
}
return i;
}
A while back, I reviewed an issue like this where the user was getting tripped up on acquiring a credit card check digit. With that, I wrote a small proof-of-principle test program that allows validation of credit card numbers using the Luhn algorithm. Following, is that code snippet.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int check_valid(char * num)
{
int sum = 0;
int work = 0;
char card[20];
if ((strlen(num) %2 == 0)) /* Even numbers - do not need a leading zero */
{
strcpy(card, num);
}
else /* Odd numbers - add a leading zero to evaluate */
{
strcpy(card, "0");
strcat(card, num);
}
printf("Length of number is: %d\n", (int)strlen(num));
for (int i = 0; i < strlen(card); i++)
{
work = card[i] - '0';
if ((i %2) == 0)
{
work = (card[i] - '0') * 2;
if (work > 9)
{
work = work - 9;
}
}
sum = sum + work;
printf("Digit is: %d Value is: %d Sum is %d\n", (card[i]- '0'), work, sum);
}
return ((sum % 10) == 0);
}
int main()
{
char number[20];
int x = -1;
printf("Enter a number: ");
x = scanf("%s", number);
x = check_valid(number);
if (x == 0)
printf("Invalid\n");
else
printf("Valid\n");
return 0;
}
It doesn't identify the card issuer, just verifies that the number is valid.
As noted in the comments, one probably would want to utilize a string entry as this code snippet does rather than trying to utilize a very large integer. You might try going that route with a string as well. Give this a try and see if it allows you to progress.

while function C programing

I'm a first year collage student and I'm working on an exercise with a while function.
my current objective is to identify the lowest and highest value digits in a given number.
this is my code so far:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
void main() {
printf("enter an integer\n");
int one,onemax,onemin, min=9, max=0;
scanf("%d", &one);
onemax = one;
onemin = one;
while (onemax >= 0) {
if (max <= onemax % 10) {
max = onemax % 10;
onemax=onemax / 10;
if (onemax == 0) {
break;
}
}
else {
onemax=onemax / 10;
}
}
while (onemin != 0) {
if (min >= onemin % 10) {
min = onemin % 10;
onemin=onemin / 10;
if (onemin == 0) {
break;
}
}
else {
onemin=onemin / 10;
}
}
printf("min = %d max = %d", min, max);
}
now I know I'm stuck in a loop here and I need help getting out.
when onemax reaches 0 you are done. You should change the >= condition to >:
while (onemax > 0) {
// Here ------^

Program will not run for larger values in tens and hundred except floating

I am writing a program to solve the problem of changed owed after a customer purchases goods.
The changes are meant to be mostly in floating variable like 0.50 for 50cents and that works fine but for situation where the change owed are in whole number like 10 and 9 the program will not carry out necessary operations
I have converted the input from float to input but to no avail
#include <cs50.h>
#include <stdio.h>
#include <math.h>
int change(void);
int main()
{
float num;
int y;
int counter = 0;
do
{
num = get_float("Change owed: ");
num = round(num * 100);
y = (int) num;
printf("%d\n", y);
}
while(num <= 0);
while(y>0)
{
if(y > 25)
{
y-=25;
counter++;
}
else if(y < 25 && y >= 10)
{
y-=10;
counter++;
}
else if(y < 10 && y >= 5)
{
y-=5;
counter++;
}
else if(y < 5 && y >= 1)
{
y-=1;
counter++;
}
else
{
return 0;
}
}
printf("%d \n", counter);
}
I want to be able to run the program for all numeric data types

CS50 pset1 Greedy not working

Like others here, I am trying to make a program called greedy that will tell me the least amount of coins that I have to give someone as change for a given amount of money. I made this but it gives me the wrong amount of coins and I don't know why :(
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void)
{
float given_amount;
int cent_count;
int coin_count = 0;
do
{
printf("What is the amount of change I owe you?\n");
given_amount = GetFloat();
}
while (given_amount < 0);
cent_count = (int)round(given_amount * 100);
while (cent_count > 25)
{
coin_count++;
cent_count -= 25;
}
while (cent_count > 10)
{
coin_count++;
cent_count -= 10;
}
while (cent_count >= 1)
{
coin_count++;
cent_count -= 1;
}
printf("Take these %d coins\n", coin_count);
}
If I tell the program I need to give back 25 cents the program tells me I have to give the person 7 coins, but it should just tell me I have to give him one coin... a quarter.
Your first 2 loops don't take care of value 25 and 10.
In fact your algorithm is giving you 7, that is: 2 coins of 10 cent, and 5 coins of 1 cent.
You must check for them, so:
while (cent_count >= 25)
{
coin_count++;
cent_count -= 25;
}
while (cent_count >= 10)
{
coin_count++;
cent_count -= 10;
}
Here's my code:
#include <cs50.h>
#include <stdio.h>
#include <math.h>
int main(void)
{
float input = -1.00;
int n = 0;
int c = 0;
printf("O hai! How much change is owed?: ");
do
{
input = GetFloat();
if(input < 0)
printf("Positive number, please!: ");
}
while (input < 0);
c = roundf(input * 100);
while(c >= 25)
{
c = c - 25;
n++;
}
while (c >= 10)
{
c = c - 10;
n++;
}
while (c >= 5)
{
c = c - 5;
n++;
}
n = n + c;
printf("%i\n", n);
}

"new_dollars = dollars + cents / 100" NO CHANGE

In output, new_dollars always displays the number of dollars, so I don't know how to solve the problem. Please help me figure out what the problem is.
int main(void)
{
int dollars, cents, count, new_dollars;
for ( count = 1; count <= 10; ++count){
printf ("Enter dollars: ");
scanf ("%i", &dollars);
printf ("Enter cents: ");
scanf ("%i", &cents);
if ( cents >= 100 ){
cents = cents % 100;
new_dollars = dollars + cents / 100;
printf ("%i\n", new_dollars);
printf ("$%i.%2i\n\n", new_dollars, cents);
}
else {
printf ("$%i.%2i\n\n", dollars, cents);
}
}
return 0;
}
The commenters are right about integer division. Try this, it has the order two lines that do the calculation swapped, so cents will still have a useful value when you need it to:
int main(void)
{
int dollars, cents, count, new_dollars;
for ( count = 1; count <= 10; ++count){
printf ("Enter dollars: ");
scanf ("%i", &dollars);
printf ("Enter cents: ");
scanf ("%i", &cents);
if ( cents >= 100 ){
new_dollars = dollars + cents / 100;
cents = cents % 100;
printf ("%i\n", new_dollars);
printf ("$%i.%2i\n\n", new_dollars, cents);
}
else {
printf ("$%i.%2i\n\n", dollars, cents);
}
}
return 0;
}
convert "int new_dollars" into "float new_dollars";
new_dollars = (float) dollars + (float) cents / 100;
printf ("$%.2f\n", new_dollars);

Resources