Thread: Basic Program Problems

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230

    Basic Program Problems

    I'm getting unwanted output. It doesn't make sense. The output is either 0 new cars won or 1000 new cars one. Basically what I want to do is assign car, chosen, and revealed random values from 1 - 3. I have a do while loop so that revealed's value keeps getting changed if it is the same as the chosen value, once it isn't the same it should break the do while loop, and if the chosen value is the same as car's random value then it should increment the wins integer. Once it has looped through 1000 times wins should have a value of how many times chosen and car matched. The thing is it either outputs 0 wins or 1000 wins, which is obviously not right... Here is the code:

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    
    int main() {
    
    	int car = 0;
    	int chosen = 0;
    	int revealed = 0;
    	int wins = 0;
    	srand(time(NULL));
    	car = (rand() &#37; 3 + 1);
    	chosen = (rand() % 3 + 1);
    	for (int i = 0; i < 1000; i++) {
    		do {
    			revealed = (rand() % 3 + 1);
    		} while (revealed == chosen);
    		
    		if (chosen == car) {
    			wins++;
    		}
    	}
    	cout << "You won: " << wins << " new cars!";
    	cin.get();
    }
    By the way, in case you're wondering why I have a 'revealed' integer which at the moment has no visible purpose, it's because later on I'm going to be adding more code where it is vital :P

    EDIT: NEVER MIND I'm an idiot, I wasn't even assigning new random values to the integers each pass through the loop, which explains the problem... Stupid mistake lol.
    Last edited by pobri19; 09-15-2008 at 06:10 AM.
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Will this show you what you're trying to see? Wouldn't it be clearer to make a win when chosen != car (because it means that switching would switch to the car)?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic C++ Program help
    By Ronzel in forum C++ Programming
    Replies: 3
    Last Post: 06-07-2009, 02:24 AM
  2. Blackjack program, having problems with the ace
    By yigster in forum C Programming
    Replies: 6
    Last Post: 05-07-2009, 06:41 AM
  3. Basic C input output program help
    By trevordunstan in forum C Programming
    Replies: 2
    Last Post: 01-27-2008, 06:41 PM
  4. Help me with my basic program, nothing I create will run
    By Ravens'sWrath in forum C Programming
    Replies: 31
    Last Post: 05-13-2007, 02:35 AM
  5. GPA Program Problems
    By Clint in forum C Programming
    Replies: 3
    Last Post: 04-28-2005, 10:45 AM