1
int main() {
    power=1;
    while (1 == 1){
        tapcost=power*3;
        cout << "type upgrade/buy/a" << endl;
        cin >> way;
        if (way == "upgrade"){
            cout << "1. A Power " << "(Costs: " << tapcost << ")" << endl;
            cin >> upgr;
            if  (upgr == 1){
                if (0<=money-power*3){
                    power=power+1;
                    money=money-power*3;
                }
                else
                    cout << "You can't afford that!!!" << endl;
            }
        }
        if (way == "a"){
            money=money+power;
        }
    }
    return 0;
}

When I type upgrade and then type anything else other than the variable "1", the code will repeat infinitely.

  • 2
    If by "repeat infinitely" you mean it will no long wait for user input, it's likely `std::cin` is in a bad state. Please provide a [MCVE], what you've shown is incomplete. – François Andrieux May 03 '18 at 14:21
  • 1
    unclear what you are asking, if you write an infinite loop (aka `while(true)`) what you get is a infinite loop. Are you looking for `break` ? – 463035818_is_not_an_ai May 03 '18 at 14:21
  • get rid of the while loop – C.J. May 03 '18 at 14:32
  • 1
    instead of writing 1==1 you can write true, in order to enhance legibility. Same goes for money=money+power; which could become money += power. – Gladaed May 03 '18 at 14:42
  • 2
    Terminology bug: `"1"` is not a variable. – molbdnilo May 03 '18 at 14:44
  • 1
    Compile with all warnings and debug info; `g++ -Wall -Wextra -g` with [GCC](http://gcc.gnu.org/). Improve your code to get no warnings, then [use the `gdb` debugger](https://sourceware.org/gdb/current/onlinedocs/gdb/) to understand the behavior of your program (which is likely to be wrong). Improve your code, repeat. – Basile Starynkevitch May 03 '18 at 14:53

4 Answers4

6

This is a never-ending problem.
See this question: Infinite loop with cin when typing string while a number is expected

I think your code have some mistakes.

int upgr;
cin >> upgr; // you can type any number you want (-2 147 483 648   /   2 147 483 647)

I suggest you to use getline, cin.getline or fgets instead of cin >> when reading a line.

And just use while(1) or while(true)

1

You have created an infinite loop by never changing the value of your ‘1’ variable. In some way you need to change that value when iterating through your conditions or else you’ll never get out of your loop.

0

You could also try out something like that.

char i;

while((std::cin >> i) && i != '1') {
    .... 
}
retinotop
  • 483
  • 11
  • 17
0

In your code, while (1 == 1) creates an infinite loop. Since I assume you want this code to keep asking players for their input until they decide to stop, you can add an option exit which breaks out of the loop when the player wants to.

#include <iostream>

int main() {
  int power = 1;
  int money = 1000;

  while (1 == 1) {
    int tapcost = power * 3;
    std::string way;
    std::cout << "type upgrade/buy/a/exit" << std::endl;
    std::cin >> way;
    if (way == "upgrade") {
      std::cout << "1. A Power " << "(Costs: " << tapcost << ")" << std::endl;
      int upgr;
      std::cin >> upgr;
      if (upgr == 1) {
        if (0 <= money - power * 3) {
          power = power + 1;
          money = money - power * 3;
        }
        else {
          std::cout << "You can't afford that!!!" << std::endl;
        }
      }
    }
    if (way == "a") {
      money = money + power;
    }
    if (way == "exit") {
      break;
    }
  }
  return 0;
}
mammothb
  • 16
  • 5