N
N
Nastyuuuushka2016-07-21 18:09:39
C++ / C#
Nastyuuuushka, 2016-07-21 18:09:39

Why is the function not returning the desired value?

// I want the function to reset the entered value to a given limit and display the remainder. That is, when we enter 100, when dividing by 25, there is no remainder and 0 comes out, and if 99, then the remainder is 1. Tell me how to arrange? Outputs always "0" for any value.

#include <stdio.h>

int main(void)
{
  int surrender = scanf_s("ENTER...");
  int coin25 = 25,
    coin10 = 10,
    coin5 = 5,
    coin1 = 1;
  int countCoin25 = 0,
    countCoin10 = 0,
    countCoin5 = 0,
    countCoin1 = 0;
  while (coin25 > 0 && surrender - coin25 >= 0) {
    surrender - coin25;
    countCoin25++;
  }
  printf("SURRENDER IS %d " , countCoin25);
  system("pause");
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Movchan, 2016-07-21
@Alexander1705

  • You can reset a variable, but not a value.
    If I understand correctly, and you need to calculate the minimum number of coins, then you need something like this:
    #include <iostream>
    
    int main()
    {
        const int N = 4;
        const int coinsValue[N] = {25, 10, 5, 1};
        int coinsCount[N] =       { 0,  0, 0, 0};
    
        int sum;
        std::cin >> sum;
    
        for (int i = 0; i < N; ++i)
        {
            while (sum >= coinsValue[i])
            {
                coinsCount[i]++;
                sum -= coinsValue[i];
            }
            std::cout << coinsCount[i] << " (" << coinsValue[i] << 
            (coinsCount[i] == 1 ? ") coin\n" : ") coins\n");
        }
    
    
    }

A
AtomKrieg, 2016-07-21
@AtomKrieg

int surrender = scanf_s("ENTER...");
int count25 = surrender / 25,
    remainder25 = surrender % 25;
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question