CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: loop loop loop

Threaded View

  1. #1
    Join Date
    Nov 2008
    Posts
    42

    loop loop loop

    right so far ive got this to genrate 1 random that the user has to guess and if correct then the program will end what im trying to put in is if the user guess first time or over and over till they get it right to then tell the user how many guess they took to get it right just dunno i tried using a for loop but that didnt work as its giving me 0 any ideas

    Code:
    #include <iostream>
    #include <time.h>
    #include <stdlib.h>
    #include <string>
    
    int main()
    {
    	std::string pause;
    
    	int number;
    	int max_value = 10, x; 
    	srand( (unsigned)time( NULL )); 
    	rand();  
    	x = rand()/ (RAND_MAX / max_value + 1);
    
    	std::cout << "guess the random number between 0 and 10: ";
    	std::cin >> number;
    
    		if (number > x)
    			std::cout << "number is lower than " << number;
    
    		if (number < x)
    			std::cout << "number is higer than " << number; 
    	for (int i = 0; i < 10; ++i)
    	{
    	while (number != x)
    	{
    		std::cout << "\n";
    		std::cout << "try again: "; 
    		std::cin >> number;
    		
    		if (number > x)
    		std::cout << "number is lower than " << number;
    
    		if (number < x)
    			std::cout << "number is higer than " << number; 
    
    	}
    		std::cout << "Guess is correct";
    		std::cout << " user took " << i << " tries";
    
    	std::cin >> pause;
    	return 0;
    	}
    }
    Last edited by curtcoollfgrg; November 16th, 2008 at 12:07 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured