You are viewing our Forum Archives. To view or take place in current topics click here.
Why doesn't this work? (+Rep)
Posted:

Why doesn't this work? (+Rep)Posted:

Lood
  • Blind Luck
Status: Offline
Joined: Apr 18, 201113Year Member
Posts: 1,175
Reputation Power: 911
Status: Offline
Joined: Apr 18, 201113Year Member
Posts: 1,175
Reputation Power: 911
Writing this for a class, VERY VERY stuck.

Program flips 5 coins and keeps doing so until they all match.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main();
{
  srand(time(NULL));
 
  int d1, d2, d3, d4, d5;
  int tries = 0;
 
  d1 = 1 + rand() % 2;
  d2 = 1 + rand() % 2;
  d3 = 1 + rand() % 2;
  d4 = 1 + rand() % 2;
  d5 = 1 + rand() % 2;
 
  if (d1 != d2);
  {
    tries++;
    else(d1 == d2 == d3 == d4 == d5);
    {
    cout << "All of the coins match! It took " << tries << " tries!" << endl;
    }
  }   
}
#2. Posted:
vLuna
  • TTG Senior
Status: Offline
Joined: Dec 29, 201211Year Member
Posts: 1,428
Reputation Power: 56
Status: Offline
Joined: Dec 29, 201211Year Member
Posts: 1,428
Reputation Power: 56
I know its irrelevant.. but its amazing to see someone else who loves Deep House
#3. Posted:
Lood
  • Halloween!
Status: Offline
Joined: Apr 18, 201113Year Member
Posts: 1,175
Reputation Power: 911
Status: Offline
Joined: Apr 18, 201113Year Member
Posts: 1,175
Reputation Power: 911
vLuna wrote I know its irrelevant.. but its amazing to see someone else who loves Deep House
House music is life... <3 lol

Back to class stuff...
#4. Posted:
speed
  • 2000 Thanks
Status: Offline
Joined: Jun 11, 200915Year Member
Posts: 9,897
Reputation Power: 3160
Motto: "I'l no I grew up to fast speed I no u will be little famous" - Famous_Energy
Motto: "I'l no I grew up to fast speed I no u will be little famous" - Famous_Energy
Status: Offline
Joined: Jun 11, 200915Year Member
Posts: 9,897
Reputation Power: 3160
Motto: "I'l no I grew up to fast speed I no u will be little famous" - Famous_Energy
if (d1 != d2);
  {
   tries++;
   else(d1 == d2 == d3 == d4 == d5);
   {
   cout << "All of the coins match! It took " << tries << " tries!" << endl;
   }
  }


This isn't how an if/else works.
#5. Posted:
Lood
  • Blind Luck
Status: Offline
Joined: Apr 18, 201113Year Member
Posts: 1,175
Reputation Power: 911
Status: Offline
Joined: Apr 18, 201113Year Member
Posts: 1,175
Reputation Power: 911
speed wrote
if (d1 != d2);
  {
   tries++;
   else(d1 == d2 == d3 == d4 == d5);
   {
   cout << "All of the coins match! It took " << tries << " tries!" << endl;
   }
  }


This isn't how an if/else works.
What would you suggest then? Feeling overwhelmed about it cause I have been messing with it for a while and nothing seems to work.
#6. Posted:
speed
  • Summer 2023
Status: Offline
Joined: Jun 11, 200915Year Member
Posts: 9,897
Reputation Power: 3160
Motto: "I'l no I grew up to fast speed I no u will be little famous" - Famous_Energy
Motto: "I'l no I grew up to fast speed I no u will be little famous" - Famous_Energy
Status: Offline
Joined: Jun 11, 200915Year Member
Posts: 9,897
Reputation Power: 3160
Motto: "I'l no I grew up to fast speed I no u will be little famous" - Famous_Energy
[ Register or Signin to view external links. ]
#7. Posted:
Cyimking
  • TTG Senior
Status: Offline
Joined: May 02, 201212Year Member
Posts: 1,129
Reputation Power: 34
Status: Offline
Joined: May 02, 201212Year Member
Posts: 1,129
Reputation Power: 34
1) Implement a new variable. You can called it match and set it to false. (OPTIONAL)

2) Set a while loop and set it to false (or do while(!match) )

3) Increment the counter by 1.

4) Check if all the coins are equal. Just use ONE if statement... and else is not needed.

5) If they are equal, break out of the loop by setting the variable "match" to true OR by using "break;" .

6) Done.
#8. Posted:
Lood
  • Fairy Master
Status: Offline
Joined: Apr 18, 201113Year Member
Posts: 1,175
Reputation Power: 911
Status: Offline
Joined: Apr 18, 201113Year Member
Posts: 1,175
Reputation Power: 911
Cyimking wrote 1) Implement a new variable. You can called it match and set it to false. (OPTIONAL)

2) Set a while loop and set it to false (or do while(!match) )

3) Increment the counter by 1.

4) Check if all the coins are equal. Just use ONE if statement... and else is not needed.

5) If they are equal, break out of the loop by setting the variable "match" to true OR by using "break;" .

6) Done.
got it! Thanks so much for the help:) I really really appreciate it
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.