Théo
Théo

Reputation: 1

C++ while loops and for loops not working

First, I'll send the code.

    #include <iostream>

using namespace std;

int main() {

  int times = 0;
  cout << "Enter a number of candies:";
  int number;
  cin >> number;

  while (number >= 1000) {
    cout << "Please enter an integer number between 0 and 999.\n";
    cin >> number;

  }


  for (times <= 10; times++; number--) {
    cout << "nomnom I have" << number - 1 << "candies left.\n";
   }




}

I am trying to practice c++ because I'm a beginner so I made a little program that is supposed to allow you to enter a number between 1 and 999, then this is gonna be the number of candies. Then, it says how many there are left. It's not working though. Can anyone tell me why? Thanks.

Upvotes: 0

Views: 89

Answers (3)

Tor
Tor

Reputation: 1

You're pretty close! There was just a slight issue with the for loop syntax:

The first part of the for loop is actually the initializer, the middle bit is the conditional, and the last bit is the iterator, it's a bit different than the while loop.

Here is what you probably meant to do:

  for (times = 0; times < 9; number--, times++) {
    cout << "nomnom I have" << number - 1 << "candies left.\n";    
  }

Upvotes: 0

Gaben
Gaben

Reputation: 374

Your for loop is incorrect - it should be done according to this:
for( <initialization> ; <checking> ; <run_in_each_loop> ){ ...}

You can read more about for loop here.


You want your code to do 2 "increments" (<run_in_each_loop>). You can't do this using ; in between

A common idiom is to use the comma operator which evaluates both operands, and returns the second operand. Thus:

for(int i = 0; i != 5; ++i,++j) do_something(i,j);

read more here.

Other way would be to put number-- inside of { ... } brackets.


In the end, your code should look like that:

#include <iostream>
using namespace std;

int main() {

  int times = 0;
  cout << "Enter a number of candies:";
  int number;
  cin >> number;

  while (number >= 1000) {
    cout << "Please enter an integer number between 0 and 999.\n";
    cin >> number;
  }

  for (  ; times <= 10; times++, number--) {
    cout << "nomnom I have" << number << "candies left.\n";
   }

   return;
}

Upvotes: 0

bhristov
bhristov

Reputation: 3187

You should have your for loop formatted like this:

  for (; times <= 10; times++, number--) {
    cout << "nomnom I have" << number - 1 << "candies left.\n";
   }

The format of the for loop follows the following rule:

for(<initializer, which you can skip> ; <condition for execution which is required> ; <increment or decrement of the variable which you are bounding with the condition, this part is not required either>)

Within the initializer part you can create multiple variables, you just need to separate them with a ",", the same rule applies to the final part of the for loop. Neither the initializer not the modifier at the end are required for correct syntax. This is a valid example:

int i = 0;
for(; i < 5;)
{
   ++i;
}

For example:

for(int i = 0, j = 5; i < 5 && j > 0; ++i, --j){}
int i = 0, j = 5;
for(; i < 5 && j > 0; ++i, --j){}
int i = 0, j = 5;
for(; i < 5 && j > 0;){
  ++i;
  --j;
}

All of these examples are accomplishing the same task of incrementing i and decrementing j.

Upvotes: 1

Related Questions