First year c++ help

Hey guys having some trouble with an assignment I have due.

The criteria my prof gave is this :
You are asked to write a program for the users to guess a secret character (between lower case letter ‘a’ and
‘z’) stored inside the program. The user is allowed to have a maximum of 3 guesses. Read the following
scenario to understand the program requirements. (Hint: All letters are represented as numbers (e.g.
Unicode) inside the memory. So, when you are comparing letters, you are actually comparing their code in
numbers. Thus, letter ‘a’ is smaller than ‘b’. That is, ‘a’ < ‘b’ < ‘c’……………………….<’x’<’y’<’z’.

Also more criteria is I must use a loop, and go global variables are allowed.

I don't understand what he means by the letter a is smaller than b and so forth. If I could get some help with that it would be greatly appreciated.

This is the code I have so far, it shows the general idea in which I was going in but it gives me an error when I put the 2nd while.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  #include<iostream>
#include<iomanip>
#include<string>
using namespace std;

int main()
{
string cinput='\0';
char chrSecretLetter = 'g';
cout<< "Please enter a letter between a and z: ";
cin>> cinput;
while (cinput = "a", "b", "c","d","e","f")
{cout<<"Incorrect! Please enter a letter smaller than"<<cinput<<"."<<endl;

}
while (cinput == chrSecretLetter)
{

}
Inside a computer letters(char) are represented by numbers/bits(e.x 0110 1100). It comes from ASCII table(google it). Each letter is given a number and with those numbers you can compare each other.

For example, the letter 'a' is represented by the number 97 and the letter 'b' is represented by the number 98 and the letter 'c' is represented by the number 99, and so on until letter 'z'.

So when your comparing two letters within a computer like 'a'<'b' the computer actually sees 97<98(true) or 'a'=='a' (97 == 97, true).

Note: 'A' and 'a' are not the same. 'a' == 97 while 'A' == 65

In you code on starting on line 12
1
2
3
4
5
6
7
8
while (cinput = "a", "b", "c","d","e","f")
{cout<<"Incorrect! Please enter a letter smaller than"<<cinput<<"."<<endl;

}
while (cinput == chrSecretLetter)
{

}


lets say the user enters a letter 'a'-'f', then your program will loop forever, because you didn't put in any code to get out of this while loop.

example
user enters 'a'
-goes into while loop
 
while (cinput = "a", "b", "c","d","e","f")

-you print out that it's incorrect.
Well cinput is still 'a' so when it loops back around the while loop to the front it's still valid, so the loop happens all over again and again and again .

I would advise against using a while loop right there, and use an if statement.
Last edited on
Hey,

Here is a sample of the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>

using namespace std;

int main()
{
   unsigned short int count = 3;
   while (count > 0)
   {
      cout << "Please enter a letter between a and z: ";
   
      unsigned char input;
      cin >> input;

      if (input < 'a' || input > 'z') continue;

      if ('g' != input)
      {
         --count;
         cout << "Sorry, wrong guess. You have " 
              << count << " guess" 
              << (count > 1 ? "es" : "" ) 
              << " left." << endl;
      }
      else
      {
         cout << "You have guessed it! Congratulations!" << endl;
         return 0;
      }
   }

   cout << "Sorry, you failed to guess." << endl;

   return 0;
}
Topic archived. No new replies allowed.