I've wiped the slate clean...

Started by
46 comments, last by rip-off 15 years, 10 months ago
Quote:Original post by Mathmo
it is not random because numbers is the address in memory of your array. Not the values in the array. cout << numbers[0]; will give you the value of the first element in your array.

Please go and find a good tutorial and try and work through it - if you keep posting questions on here asking us to help you with everything, without starting to learn something by yourself you will never become a good programmer. Hell, even if you do go and learn something by yourself you may never be a good programmer...

I already suggested http://www.cplusplus.com/doc/tutorial/ which is a good, solid tutorial. Once you have worked through all of that tutorial, compiling, testing and fiddling with all of the code, then hopefully you will start to have an understanding of how some of c++ works, and then you will be able to start making simple games. It really is the best way to learn, and that tutorial really covers the basics you need quite well. It will probably take you a couple of weeks at least to get through it, but trust me it is the best way to get into programming.

Also, given that you are a beginner, what makes you want to learn c++? It isn't the most intuitive language for most people, although it suits me just fine. A lot of people on here would recommend c# as a better language for first time programmers. I have been coding in c++ for 8 months now, and there are still quirks of the language that catch me out almost every time I hit compile.


I wholeheartedly agree.

Throughout this entire thread you have simply been asking how to fix your code and have not tried to understand *why* it doesnt work.

You are asking us how to make your conditions work properly when you don't yourself understand why they arent working properly.

I'd strongly suggest going through the above tutorial or a similar tutorial because, until you understand how each of these constructs that your trying to use work, you'll just keep on asking us how to make your program which is what you seem to be doing.

Don't start walking until you know what your legs are there for!
Advertisement
Ha... got a solution. I've replaced "Try Again..." with "... ". The users will never know when they see "... YOU GOT IT!"

My next step is to add a timer of some sort: I plan the program to show the time taken to answer the problem, and give time penalties for wrong answers
I have a crush on <algorithm> & co :-)

#include <algorithm>#include <boost/bind.hpp>#include <ctime>#include <functional>#include <iostream>#include <iterator>#include <numeric>#include <string>#include <vector>const int NUMBERS = 5;const int RANGE = 10;int main(){    srand(time(0));    std::vector<int> numbers(NUMBERS);    for(;;)    {        std::generate(numbers.begin(), numbers.end(), boost::bind(std::modulus<int>(), boost::bind(rand), RANGE));        int sum = std::accumulate(numbers.begin(), numbers.end(), 0);        std::copy(numbers.begin(), numbers.end() - 1, std::ostream_iterator<int>(std::cout, " + "));        std::cout << numbers.back() << " = ";        int input = -1;        clock_t t1 = clock();        std::cin >> input;        clock_t t2 = clock();        if (input == sum)        {            clock_t dt = 1000 * (t2 - t1) / CLOCKS_PER_SEC;            std::cout << "Well done! You needed " << dt << " milliseconds.\n\n";        }        else        {            std::cout << input << " is wrong, the correct answer is " << sum << ".\nGAME OVER\n";            break;        }    }}


[Edited by - DevFred on June 6, 2008 9:59:17 AM]
Looks a bit too complicated for me... You seem to be #including a lot though! :-)
Any simpler method?
Quote:Original post by cj270608
Ha... got a solution. I've replaced "Try Again..." with "... ". The users will never know when they see "... YOU GOT IT!"


Oh Lord.

I strongly, strongly suggest that you read and understand some more C++ tutorials and/or literature. It's a much better learning path than attempting something beyond your skill level and having other people fix your code.
finished!

#include <cstdlib> #include <ctime> #include <iostream>using namespace std;int main(){	time_t time0,time1;	srand ( time(NULL) );	int numbers [10] = { rand() % 10, rand() % 10,rand() % 10,rand() % 10,rand() % 10,rand() % 10,	rand() % 10,rand() % 10,rand() % 10,rand() % 10,};	int total = numbers[0]+numbers[1]+numbers[2]+numbers[3]+numbers[4]+numbers[5]	+numbers[6]+numbers[7]+numbers[8]+numbers[9];	cout << "Welcome to SPEEDY ADDITION! Press enter to start!";	getchar();	cout << "Add up the following numbers as fast as possible! Press enter to begin...";	getchar();	time (&time0);	for (int i = 0; i < 10; ++i){    std::cout << numbers << "\n";}	int guess = 0;	while (guess != total) {	cin >> guess;			cout << "... ";	}		cout << "YOU GOT IT!";	cout << "\n";	time (&time1);	cout << "You did it in ";	cout << difftime(time1, time0);	cout << " seconds!";	cout << "\n";	cout << "Press enter to exit.";	getchar();	cin.get();}


Tell me what you think of it!
I think it's good you completed it.

Now I think you need to take a step back, and actually listen to the advice of everybody in the thread. Start actually learning how to program rather than fiddling with what everybody told you to do until you have something that semi-kinda-works-almost-correctly.

It's obvious you don't understand control structures, from your code. You don't truly understand the power of loops or arrays. You don't seem to really grasp conditionals. You don't really even seem to understand concept of flow control in a program.

Stop trying to make complete "games" and start trying to understand what each individual statement is really doing.

You don't need to worry about headers and functions yet. You need to worry about understanding each specific line of code and what it means to the overall program.

Get a beginner's book to C++ programming. Google for C++ tutorials. Nobody starts by building a dog house when they don't even know what a hammer and nails are.
Exercise #1

Replace this code with a variable declaration and a loop:
int numbers [10] = { rand() % 10, rand() % 10,rand() % 10,rand() % 10,rand() % 10,rand() % 10,	rand() % 10,rand() % 10,rand() % 10,rand() % 10,};


Exercise #2

Replace this code with a variable declaration and a loop:
int total = numbers[0]+numbers[1]+numbers[2]+numbers[3]+numbers[4]+numbers[5]	+numbers[6]+numbers[7]+numbers[8]+numbers[9];


Exercise #3

Write this code such that "..." isn't printed if the guess is correct.
while (guess != total) {	cin >> guess;	cout << "... ";}


Everything you need to do this has been discussed in this thread.

This topic is closed to new replies.

Advertisement