School Assignment

First to begin. I am not a professional programmer by any swing of any imagination. I hate computers and they frighten me. I'm trying to fix that. So anyway I'm in a C++ programming class and am having an enormous amount of difficulty with my homework.

So what I need to do is make a program that makes an equation 4 + 5 = 9 for example. And then the program hides 2 pieces of it 4 _ 5 = _ for example. I took some code out of the textbook for a hangman game. I figured almost the same principle and I'm just having issue reverse engineering it. Here's my code.

// Hangman
// The classic game of hangman

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>

using namespace std;

int main()
{
// set-up

vector<int> first; // collection of possible words to guess
first.push_back((rand() % 100 ));
first.push_back((rand() % 100));
first.push_back((rand() % 100));
first.push_back((rand() % 100));

srand(static_cast<unsigned int>(time(0)));
random_shuffle(first.begin(), first.end());
const int operator = first[0]; // word to guess
int soFar(operator.size(), '-'); // word guessed so far
int used = ""; // letters already guessed

// main loop
while (( soFar != operator))
{

cout << "\nYou've used the following letters:\n" << used << endl;
cout << "\nSo far, the word is:\n" << soFar << endl;

char guess;
cout << "\n\nEnter your guess: ";
cin >> guess;
while (used.find(guess) != int::npos)
{
cout << "\nYou've already guessed " << guess << endl;
cout << "Enter your guess: ";
cin >> guess;
guess = toupper(guess);
}

used += guess;

if (operator.find(guess) != int::npos)
{
cout << "That's right! " << guess << " is in the word.\n";

// update soFar to include newly guessed letter
for (unsigned int i = 0; i < operator.length(); ++i)
{
if (operator[i] == guess)
{
soFar[i] = guess;
}
}
}

}


system("pause");
return 0;
}
First of all, please wrap your code in the code tags (under Format).

You have chosen a reserved word (operator) for the name of an integer variable.

What exactly are your errors or problems?
Well the first issue I'm encountering appears to be that I was unaware that it was a reserved word. So after rectifying THAT
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Hangman
// The classic game of hangman

#include <iostream>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>

using namespace std;

int main()
{
	// set-up

	vector<int> first;  // collection of possible words to guess
	first.push_back((rand() % 100 ));
	first.push_back((rand() % 100));
	first.push_back((rand() % 100));
	first.push_back((rand() % 100));

	srand(static_cast<unsigned int>(time(0)));
	random_shuffle(first.begin(), first.end());
	const int mathsymbol = first[0];          // word to guess
	int soFar(mathsymbol.size(), '-');          // word guessed so far
	int used = "" ;                            // letters already guessed

	// main loop
	while (( soFar != mathsymbol))
	{
		
		cout << "\nYou've used the following letters:\n" << used << endl;
		cout << "\nSo far, the word is:\n" << soFar << endl;

		char guess;
		cout << "\n\nEnter your guess: ";
		cin >> guess;
		while (used.find(guess) != int::npos)
		{
			cout << "\nYou've already guessed " << guess << endl;
			cout << "Enter your guess: ";
			cin >> guess;
			guess = toupper(guess);
		}

		used += guess;

		if (mathsymbol.find(guess) != int::npos)
		{
			cout << "That's right! " << guess << " is in the word.\n";

			// update soFar to include newly guessed letter
			for (unsigned int i = 0; i < mathsymbol.length(); ++i)
			{
				if (mathsymbol[i] == guess)
				{
					soFar[i] = guess;
				}
			}
		}
		
	}

	
	system("pause");
	return 0;
}

NOW the issue I'm getting is
Error 1 error C2228: left of '.size' must have class/struct/union c:\users\brian\documents\visual studio 2013\projects\guessthenumber\guessthenumber\source.cpp 28 1 Guessthenumber
I don't know what the error means. Theres about 20 more of those and in interest of saving space lets start with that one.
OK That error means that you are calling a method of a variable that has no methods. An integer is simply that: It just holds integers.

You should use characters for the inputs and the string class to hold soFar. That means you need to change your vector to a vector of characters. However, if you really want to use ints, then just change your prompts to say "number" instead of "word".

Let me know if that helps or if you have more questions. I'll be away from my computer for about an hour but I'll check back after that.

Good luck: I think you are getting it!
Okay. Started too large. Went back to the drawing board. Start simple. Get the equation first. How do I get it to randomly select the symbol for the equation?
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
#include <iostream>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>

using namespace std;

int main()
{
	srand(static_cast<unsigned int>(time(0)));
	int numberone = rand() % 100 + 1;
	int numbertwo = rand() % 100 + 1;
	vector<int> symbol;
	symbol.push_back( - );
	symbol.push_back( + );
	symbol.push_back( / );
	symbol.push_back( * );
	random_shuffle(symbol.begin(), symbol.end());
	answer = numberone symbol[0] numbertwo;
	

	cout << numberone << symbol[0] << numbertwo << "=" << answer;

	system("pause");
}
It looks like your vector of symbols should be sorted by random_shuffle, so symbol[ 0 ] should be random. You aren't going to get a lot of randomness with only four items, but it should be enough without writing a much more complex algorithm yourself.

Where is answer defined? What are you trying to do on line 23?

Does this compile?
It does NOT compile. What I'm trying to do on line 23 is make it refer back to whatever random symbol was selected in the vector so it would look like.

numberone (random symbol) numbertwo = answer

23 * 47 = XXX

I only need it to random select one of the 4 math symbols.
The issue I'm getting is it won't accept the symbols. I get the "expected an expression" error on all of the vector inputs.
Last edited on
I would first like to note that I am simply going by the most recent code you've posted.

Currently, answer is not defined, which is one of the issues. This can easily be changed by simply adding
int answer;
somewhere at the beginning of the main() function scope.

The second issue is that you can't simply push.back() stuff like +, -, /, *, or anything to that effect. Think of these as reserved words. There are multiple ways to fix this.

If you are absolutely adiment to use an int vector, you can simply push_back() the char version. Like so:

1
2
3
4
	symbol.push_back( '-' );
	symbol.push_back( '+' );
	symbol.push_back( '/' );
	symbol.push_back( '*' );


Most, if not all, C++ compilers will automatically convert the character to it's ASCII equivalent. You can see a full table of ASCII here:
http://www.asciitable.com/
I would recommend bookmarking that website. It includes the Extended ASCII characters, and there is a button on the top where you can see the Unicode table.

NOTE: You could also simply use the char datatype for the vector instead of having to deal with using the decimals.

For instance, if you were to just run this after:
std::cout << symbol[0];
and the symbol that was chosen was '+' then it would print out the integer 43 (it casts it to 43 because you put a char value into an int variable).

However, you would still need to re-translate that back into a usable symbol. Because of that, it would be much easier if you didn't use vectors at all for this case. I simply included the previous example in case the use of vectors was part of your assignment.

Since this is a homework assignment, I won't just give you the code, but I will give you some helpful advice on at least what I would do if I needed to use a random symbol.

> Have a 3rd random number.
> Use an If Statement to check what number that is equivalent to.
> Each If Statement would do the appropriate math and print out the results.
OR
> Also have a char value.
> Within the If Statement, set the char value to the appropriate symbol.
> Use std::cout at the end like you currently have it.

EDIT:
I forgot to mention why you were getting that error. It was "expecting an expression" around the symbols. This is basically what the compiler was seeing:
symbol.push_back( NULL + NULL );
C++ Compilers do not like to calculate NULL values.

NOTE:
Though it is fairly unimportant in today's time of the memory difference (especially with such a tiny program), using an int variable for something that would never have a number higher than 100 stored in it is a waste of space.
The int datatype uses 32 bits, while the short uses 16.
The int datatype can hold a little over 4 billion numbers (around negative 2B to positive 2B, or 0 to a little over 4B unsigned).
The short only uses about 65k numbers.

HOWEVER: You should only ever really need to worry about such a big difference if you are a part of a team that is making a VERY extensive program, or if you just want to make a program as efficient as possible. At your level though, I would just worry about getting the code to work. I just thought it would be a good idea to mention this early in your learning process.
Last edited on
Line 23 doesn't have any symbols to tell the compiler what to do. So numberone is assigned the result of rand() % 100 + 1. Are you trying to add one to the result of rand() % 100? If so,
you might want to place parentheses around the expression this way:

1
2
3

    int numberone = ( rand() % 100 ) + 1;


However, you need to change your output similar to this:

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
37
38
39
40
41
42
43
44
45
46
47
48
49

    bool bValidEquation = true;

    if( '-' == symbol[ 0 ] )
        answer = numberone - numbertwo;

    else if( '+' == symbol[ 0 ] )
        answer = numberone + numbertwo;

    else if( '*' == symbol[ 0 ] )
        answer = numberone * numbertwo;

    else if( '/' == symbol[ 0 ] ) 
        {
        if( 0 != numbertwo )
            {
            answer = numberone / numbertwo;

            }    /*    if( 0 != numbertwo )    */
        else
            {
            bValidEquation = false;

           cerr << "Divide by zero!!!" << endl;

            }   /*    if( 0 == numbertwo )    */

        }    /* if( '/' == numbertwo )    */
    else
        {
        cerr << "Invalid symbol: " << symbol[ 0 ] << "!" << endl;

        bValidEquation = false;

        }

    if( bValidEquation )
        {
        cout << numberone << " " << symbol[ 0 ] << " " << numbertwo << " = " <<
            answer << endl;

        }    /* if( bValidEquation )    */
    else
        {
        cerr << "Invalid equation!" << endl;

        }    /* if( ! bValidEquation )    */



Keep in mind that your division might need to show decimal places -- you should change answer to a float or double in those cases.
Correct me if I'm wrong, but the modulus is calculated prior to the addition sign. Because of this, a parentheses set isn't needed.

Secondly, this is a school assignment. Because of that, I would humbly ask you to edit out that code. If he doesn't do at least that much himself, he won't learn.
@rlc4: I think he's done very well for a non-programmer. He is very close on the algorithm of what he's trying to do -- I was trying to get him past these problems. I'm sure you are right about the modulus: I was very busy yesterday and/or too lazy to look up the operator precedence rules.

Instead of editing out that code, I'm going to ask him to ignore it:

Hey GrimFandango1992: Please ignore line two of my example in my post at 1141.


Isn't compromise a wonderful thing? ;)
sigh...
I really have no real way to respond to that...
Last edited on
@rcl4: I don't blame you: I was being goofy. I meant to lighten-up the conversation -- I certainly didn't mean any disrespect.
Topic archived. No new replies allowed.