Background Theory

I need help because I have trouble understanding my teacher: Write a program that will prompt the user for a four-letter word and then convert that to an unsigned int value. Note that you can get individual letters of a string by using the bracket operators. So if the string is fourLetterWord = “easy”, then fourLetterWord[3] is ’y’, fourLetterWord[2] is ’s’, fourLetterWord[1] = ’a’, and fourLetterWord[0] is ’e’. Have the program loop the input and check that the input is a proper four-letter word. Here’s an example of how the program should interact with the user: produce results like these shown here: Enter a four-letter word (keep it clean!): easiest That’s not a four-letter word. Try again: easy The ASCII value of y is 121 The ASCII value of s is 115 The ASCII value of a is 97 The ASCII value of e is 101 If the four bytes of easy were read as an int, its value would be 2037604709 Enter another four-letter word: Include as a comment at the end of your code the results of a trial run or two like that shown above. Use the modulus operator and integer division to reverse the process, taking any 4-byte unsigned integer value and interpreting that as a four-character word.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# include <iostream>
using namespace std;

int main () {
    string fourLetterWord ;
    cout << "\ nEnter a four - letter word ( keep it clean !): ";
    while ( cin >> fourLetterWord ) {
        if(/* a proper 4 - letter word is entered */) {
            // Convert each letter of the word to its ASCII value .
            // Sum up the values of each byte ( with its power of 2)
            // and report the summed value .
            // loop around to the prompt
        }
        else {
            // Give an error message and loop around to the prompt
        }
    }
} /*
The ASCII value of E is 69
The ASCII value of R is 82
The ASCII value of O is 79
The ASCII value of F is 70
If the four bytes of FORE were read as an int , its value would be 1163022150
Enter another four - letter word : */
I need help because I have trouble understanding my teacher

Which part of that do you have trouble with?

One unsigned byte can have values in range [0..255]
The second byte has values [0..255]*256
The third byte has values [0..255]*256*256

Conversion from char to int is implicit.


The reverse -- "taking any 4-byte unsigned integer value and interpreting that as a four-character word" -- is more tricky, because not all numeric values correspond to printable characters.
I'm new to c++ programming and the teacher just went through everything like he was talking to pros. I'm not familiar with it so I just need help understanding it!
Break the problem into smaller pieces. For example,


On line 15 is a comment about what should be in that branch of a if else construct.
What is "if else"? http://www.cplusplus.com/doc/tutorial/control/
How to give an error message? Like line 6.


On line 8 is a conditional expression. The fourLetterWord is a std::string that should store a four letter word.
How to know? Perhaps http://www.cplusplus.com/reference/string/string/size/
Thank you
Topic archived. No new replies allowed.