Thread: HELP - New to C++ and most other programming

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    6

    Exclamation HELP - New to C++ and most other programming

    The program I wrote below is supposed to play a guessing game based on the computer picking a random letter. All it does is loop twice then say you win. Also the if higher/lower portion is not processing properly. eg: (depending on if the if statement is guess < randomLetter) guess a - the letter is lower, guess a - You win. I have spent hours looking thru this site and others for help for most of my problems but I generally can't find the answer I need or am just so C++ disabled I can't see the answer in front of me.

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

    int main()
    {
    //declaration of variables
    int number = 0;
    string letters = "abcdefghijklmnopqrstuvwxyz";
    string randomLetter = "";
    string guess = "";

    //initialize the random number generator
    srand(time(NULL));

    //assign a the random value to number
    number = 0 + rand() % (26 - 10 + 1);

    //find the letter that coresponds to the number
    randomLetter.assign(letters, number, 0);

    //get users guess
    cout << "The computer has secretly picked a letter." << endl;
    cout << "Try to guess the letter." << endl;
    cout << "Your guess is: ";
    getline(cin, guess);
    transform(guess.begin(), guess.end(), guess.begin(), tolower);

    // verify that user entered only one letter
    if (guess.size() == 1)
    {

    //repeat code till users guess is correct
    while (guess != randomLetter)
    {
    //display higher or lower clue
    if (guess < randomLetter)
    cout << "The letter you are looking for is lower in the alphabet." << endl;
    else
    cout << "The letter you are looking for is higher in the alphabet." << endl;
    //end ifs

    cout << "Please try again!" << endl;
    cout << "Your guess is: ";
    getline(cin, guess);
    transform(guess.begin(), guess.end(), guess.begin(), tolower);
    }//end while

    cout << "That is correct!" << endl;
    cout << " YOU WIN !!! " << endl;

    }
    else
    cout << "Please enter one letter only.";

    return 0;
    }// end of main function

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You've got the condtional operator the wrong way round. If guess is less than randomLetter then the letter you're looking for will be higher in the alphabet. Also I think you're making the task more compilcated by using strings when you're only guessing single characters -

    Code:
    #include <iostream> 
    #include <cstdlib> 
    #include <ctime> 
    using namespace std; 
    
    int main() 
    { 
    	//declaration of variables 
    	char randomLetter = 0; 
    	char guess = 0; 
    
    	//initialize the random number generator 
    	srand(time(NULL)); 
    
    	//assign a the random value to number 
    	randomLetter ='a'+ rand() % 26; 
    
    
    	//get users guess 
    	cout << "The computer has secretly picked a letter." << endl; 
    	cout << "Try to guess the letter." << endl; 
    	cout << "Your guess is: "; 
    	cin >> guess; 
    
    	guess = tolower(guess); 
    
    	//repeat code till users guess is correct 
    	while (guess != randomLetter) 
    	{ 
    	//display higher or lower clue 
    		if (guess > randomLetter) 
    			cout << "The letter you are looking for is lower in the alphabet." << endl; 
    		else 
    			cout << "The letter you are looking for is higher in the alphabet." << endl; 
    	//end ifs 
    
    		cout << "Please try again!" << endl; 
    		cout << "Your guess is: "; 
    		cin>> guess; 
    		guess = tolower(guess); 
    	}//end while 
    
    	cout << "That is correct!" << endl; 
    	cout << " YOU WIN !!! " << endl; 
    
    	return 0; 
    }// end of main function
    zen

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    6
    Sorry I should have been more explicit. This a homework assign. and I must be able to except more that 1 char. from the user and display the warning. As for the operator it doesn't matter which way it is < or > except for the cout statement the program does not run properly which means I have messed up the logic and I am not sure how. Thanks for the answer you gave though it will help me get my head around this language.

Popular pages Recent additions subscribe to a feed