Continuous loop or counter problem or both?

This is a discussion on Continuous loop or counter problem or both? within the C++ Programming forums, part of the General Programming Boards category; I have coded a "guess the number" program. When the user inputs his guess, the program is supposed to tell ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    14

    Question Continuous loop or counter problem or both?

    I have coded a "guess the number" program. When the user inputs his guess, the program is supposed to tell him: "Excellent! Would you like to play again (y or n)?" or "Too Low. Try again." or "Too High. Try again.". The user is supposed to have 6 guesses before the game ends. My problem is that the program gives him one chance to enter a guess and then gives outputs all of the choices . I am not sure how to fix my program or where the error(s) are within the program.

    Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    
    #include <cstdio> 
    
    #include <cstdlib> 
    
    #include <ctime> 
    
    int randomNumber(); 
    void test(int yourGuess, int rnum); 
    void intro(); 
    
    void main(void) 
    { 
    
    int i=0; 
    
    int rnum=0; 
    
    int yourGuess = 0; 
    
    int count = 0;
    
    long ltime = time(NULL); 
    
    unsigned stime = (unsigned)(ltime/2); 
    
    
    intro(); 
    
    cin >> yourGuess; 
    
    srand( time( 0 )); 
    
    for (i=0; i<6; i++) 
    { 
    rnum=randomNumber(); 
    test( yourGuess, rnum);
    }
    
    
    return; 
    } 
    
    void intro(void) 
    { 
    cout << "I have a number between 1 and 1000.\n"; 
    cout <<  "Can you guess my number?\n"; 
    cout <<  "Please type your first guess.\n"; 
     
    } 
    
    int randomNumber() 
    { 
    int number =-99999; 
    
    number= 1 + rand() % 1000; 
    
    return number; 
    } 
    
    void test(int yourGuess, int rnum) 
    { 
    
    if ( yourGuess < rnum )
    	cout << "Too Low. Try again" << endl;
    
    else if ( yourGuess > rnum )
    	cout << "Too High. Try again" << endl;
    
    if ( yourGuess = rnum )
    	cout << "EXCELLENT! You guessed the number!\n";
    	cout << "Would you like to play again (y or n)?" << endl;
    
    
    	    
    
    }
    I would appreciate it if someone could please point me to where in my program that my error(s) are so that I can try to fix them. Sorry to post the whole code but since I am not sure where the problem is I thought it might be needed.

  2. #2
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    You read "yourGuess" only once.
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    > void main(void)
    main returns an int

    Learn the magic of indenting code to make your intentions more obvious.

    > rnum=randomNumber();
    Changing the number every guess is a bit mean don't you think?
    The whole point of too high / too low is to home in on the actual result.

    > if ( yourGuess = rnum )
    It's == for comparison.
    Turn up the warning level of your compiler.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Besides, your random number is not "between 1 and 1000" as advertised, but between 0 and 999, courtesy of the % operator.

    Also, it's quite impossible to guess the number correctly in 6 guesses. Perhaps you want to replace "6" with "10".
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Here's a hint: instead of asking the COMPUTER to guess the USER's number, try making the USER guess the COMPUTER's number
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 11:47 PM
  2. for loop counter problem
    By karipap in forum C Programming
    Replies: 4
    Last Post: 05-05-2009, 08:05 AM
  3. Replies: 8
    Last Post: 12-09-2008, 12:07 PM
  4. weired problem in while loop
    By avisik in forum C Programming
    Replies: 14
    Last Post: 12-01-2008, 01:41 PM
  5. having problem with string statement during a loop!
    By Hp7130p in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2005, 10:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21