I'm a beginner of C++ as you might figure when reading my code below, but I can't get arrays to work as I want it to. I've now spent hours of reading tutorials and other help guides without any success. So now I'm turning to you in hoping one of you could help me in solving this problem with arrays.
I need to store all my guesses in an array, and also display 5 of my guesses in another array.
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
usingnamespace std;
int main(){
srand(time(0));
int number = rand() % 50 + 1;
int tries = 0;
constint max = 100;
constint array_size = 5;
int array[array_size] = {};
int guessnumber[max];
int points = 0;
char enter;
int guesses[50];
int n[5];
cout << "Welcome to the guessing game against the computer. The computer will handpick a number that's up to the user to guess." << endl;
do
{
cout << "Please guess my number (1-50) : ";
cin >> guesses[max];
antal++;
if (guesses[max] > number)
cout << "Sorry, you guessed to high." << endl;
elseif (guesses[max] < number)
cout << "Sorry, you guessed to low." << endl;
else
cout << "You guessed the correct number in your " << tries << " guesses." << endl;
} while (guesses[max] != number);
if (tries == 1)
{
points = points + 10;
cout << "Points : " << points << endl;
}
elseif (tries <= 3)
{
points = points + 5;
cout << "Points : " << points << endl;
}
elseif (tries <= 10)
{
points = points + 1;
cout << "Points: " << points << endl;
}
else
{
points = points + 0;
cout << "Points: " << points << endl;
}
if (points = points > 0)
{
cout << "Since you won some points you now have the right to play nothing or double. Do you want to play? (y/n)";
cin >> enter;
}
if((enter=='y')|(enter=='Y'))
cout << "Displaying numbers you guessed on..." << endl;
elsereturn(0);
for (int i = 0; i < array_size; ++i) {
cout << "Array[" << i << "] => " << array[i] << endl;
}
{
}
return 0;
}
line 5 causes a problem on line 13, std::array exists in the std namespace. If you don't know what I mean about this, then read up about namespaces.
line 25: What is antal ?
line 36 can be better written: points += 10;
Similar for line 41
Try to avoid magic numbers in your code like 5 or 10, make them const variables instead, like you did earlier in the code.
line 51: Is, well, pointless :+)
Line 55: Did you meanif (points > 0) ?
Line 63 ends the program, before lines 64 & 65, did you mean these two line to be part if the if statement on line 60? If so, make use of some braces to associate those statements with the if statement.
If your code doesn't compile, then you should post the errors here, verbatim.
hope all goes well.
Edit : Consider making all your variables unsignedint, except the char