Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++ for programmers - Piggy Bank issue #91

Open
Armocalypsis opened this issue Oct 2, 2024 · 0 comments
Open

C++ for programmers - Piggy Bank issue #91

Armocalypsis opened this issue Oct 2, 2024 · 0 comments

Comments

@Armocalypsis
Copy link

Armocalypsis commented Oct 2, 2024

Hi all!

I wrote some additional code as part of the Piggy Bank exercise to retake input values if they are not valid (i.e. while std::cin has an error flag, retake value), and it throws me an infinite loop when I click save. I was wondering if this is an error on my part, or that this is an error of whatever tests are run to check for errors. If it is the latter, I think this should be accounted for in whatever tests this exercise runs, as error checking is not impossible to implement at this skill level even if it is not part of the exercise steps.

#include <iostream>

/*
pesos to USD = 0.05
reais to USD = 0.18
soles to USD = 0.27
*/

double pesos, reais, soles;
double dollars;
double take_input(void) {
  double currency;
  std::cin >> currency;
  /* the below should be a while loop, but this only works if I check for correct input a limited number of times */
  if (std::cin.fail()) {
    std::cin.clear();
    std::cin.ignore();
    std::cin >> currency;
  }
  return currency;
}

int main() {
  std::cout << "Enter number of Colmbian Pesos:";
  pesos = take_input();
  std::cout << "Enter number of Brzilian Reais:";
  reais = take_input();
  std::cout << "Enter number of Peruvian Soles:";
  soles = take_input();
  std::cout << "Pesos: " << pesos << ", Reais: " << reais << ", Soles: " << soles << "." << std::endl;
  dollars = (0.05 * pesos) + (0.17 * reais) + (0.27 * soles);
  std::cout << "US Dollars = $" << dollars << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant