Here, this is all the logic to guess numbers. I was bored. REALLY bored, and decided to re-write parts of this etc.
What exactly is the assignment? I see you have an alphabet.. are they supposed to guess a letter? The logic is the same, but you need to learn how to use strcmp with char[]'s.. It's easy and fast.
your functions are unnecessary in a program this small. If you decide to keep them, they need editing. You don't even use your random function inside of main at all.....
Otherwise this has everything you need. just fill in your blanks.
Code:
/********************
* I'm bored.. so i played around some
* to change this to guessing letters.. it would be easy
* no need for stupid strings just fill a char array how i did
* and use char[]'s instead of ints. Then use strcmp().
* http://www.cplusplus.com/reference/clibrary/cstring/strcmp/
* I'm assuming your assignment was to do it with an alphabet.. Hence me doing the numbers so you can see
* the logic. You can figure out the rest on your own.
* This compiles just fine on the GNU Compiler running on Debian Linux.
* Real programmers use some kind of POSIX system.. :)
*/
#include <iostream>
#include <ctime>
#include <iomanip>
#include <stdlib.h>
using namespace std;
int main()
{
//declare variables
// WHY HAVE AN ALPHABET?? I DONT KNOW!! You had one!
char alphabet[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v$
int guess = 0; // the number we are guessing
int randomLetter = 0; // the randomletter (it's a type int???) I'd rename to just 'random'
int x; // why declare this and set as rand()? Waste of time.
// create our random number
srand((unsigned)time(NULL)); // you need use srand before rand.. (look it up! Seeds the generator)
x=rand()%25; // keeps us under 25
// while they haven't quit.. or guessed right.... WE MAKE THEM PLAY!
while(1)
{
//ask the user to guess a number
cout<<"Guess a number (0 to quit!): ";
cin >> guess;
if (guess==0)
{
cout << "\n\nThanks for playing!" << endl << endl;
break;
}
cout << "YOUR GUESS WAS: " << guess << endl << endl;
// debug
// cout << "THE RANDOM NUMBER WAS: " << x << endl << endl;
// we decide if guess = x.
if(guess==x)
{
cout << "\n\nYou win!!!!!\n\nThe number was " << x << endl;
break;
}
if(guess>x)
{
cout <<"The number was lower than " << guess << endl;
}
if(guess<x)
{
cout <<"The number was higher than " << guess << endl;
}
} // end of while
} // end of main