Page 1 of 2

### C++ program

Posted: Wed Mar 25, 2009 1:25 am
Ok i wrote this program, it generates 7 random multiplication questions then tells u if u got them right or wrong..

I'm guessing there is a way easier way to make this so if anybody knows how any help would be apreciated.

Also it wont run on my maths teachers laptop which is running vista

Code: Select all
`#include "stdafx.h"#include <stdio.h>#include <time.h>#include <stdlib.h>#include <iostream>using namespace std;int main (){   int a, b, c, d, e;   srand ( time(NULL) );    a = rand() % 12 + 1;    b = rand() % 12 + 1;    c = a*b;    e = 0;   cout << "ok well maths isn't fun but DO IT! \n copyrightÂ© Benjamin Mikhaiel(bigfish) \n";  system("pause");    cout << a << "x" << b << "=";   cin >> d;   if (c == d){    cout << "correct   ";   e = e+1;}   if ( c != d){   cout << "fail     ";}   system("pause");{   a = rand() % 12 + 1;   b = rand() % 12 + 1;    cout << a << "x" << b << "=";   cin >> d;   c = a*b;   if (c == d){   cout << "correct    ";   e = e+1;}   if ( c != d){   cout << "fail     ";}}   system("pause");{   a = rand() % 12 + 1;   b = rand() % 12 + 1;    cout << a << "x" << b << "=";   cin >> d;   c = a*b;   if (c == d){   cout << "correct    ";   e = e+1;}   if ( c != d){   cout << "fail     ";}}   system("pause");{   a = rand() % 12 + 1;   b = rand() % 12 + 1;    cout << a << "x" << b << "=";   cin >> d;   c = a*b;   if (c == d){    cout << "correct    ";   e = e+1;}   if ( c != d){   cout << "fail     ";}   system("pause");}{}{   a = rand() % 12 + 1;   b = rand() % 12 + 1;    cout << a << "x" << b << "=";   cin >> d;   c = a*b;   if (c == d){    cout << "correct    ";   e = e+1;}   if ( c != d){   cout << "fail     ";}   {   system("pause");}}   a = rand() % 12 + 1;   b = rand() % 12 + 1;    cout << a << "x" << b << "=";   cin >> d;   c = a*b;   if (c == d){    cout << "correct    ";   e = e+1;}   if ( c != d){   cout << "fail     ";}   {   system("pause");}a = rand() % 12 + 1;   b = rand() % 12 + 1;    cout << a << "x" << b << "=";   cin >> d;   c = a*b;   if (c == d){   e = e+1;   cout <<"correct\n     congrats or not u got " <<e<< " correct\n";    }   if ( c != d){   cout <<"fail\n        Congrats or not u got " <<e<< " correct\n";}   system("pause");}`

### Re: C++ program

Posted: Wed Mar 25, 2009 6:46 am
I shortened the code a bit. i also deleted the first header, didnt know what that was.
i also did not work with random numbers so far and just copied the passages (sill learning..)

Code: Select all
`#include <stdio.h>#include <time.h>#include <stdlib.h>#include <iostream>using namespace std;bool Ask () {          // asks the question  int a = rand()%12 + 1, b = rand()%12 + 1, c;  cout<<"\n"<<a<<" x "<<b<<" = ";  cin>>c;  return (c==(a*b));   // gives back wether the answer was right or wrong} int main (){   int e=0,i=0;   srand ( time(NULL) );      cout << "ok well maths isn't fun but DO IT! \n copyrightÂ© Benjamin Mikhaiel(bigfish) \n";   system("pause");   for (; i<5; ++i) {                  // i<5 means 5 questions         if (Ask()) {                  // calls the Ask-function and works with the return value                cout<<"\ncorrect.\n";                ++e;                }         else {                cout<<"\nfail.\n";                }         system("pause");   }   cout<<"\n\nCongrats or not u got "<<e<<" out of "<<i<<" correct\n";   // i is the number of questions   system("pause");}`

### Re: C++ program

Posted: Wed Mar 25, 2009 6:50 pm
just so you guys know, it's somewhat bad practice to rely on "system("pause")".

also, maybe you could set it up so that it would print the correct answer if it is wrong, or print out the long multiplication version of the problem, or maybe ask the user how many questions they want to answer

### Re: C++ program

Posted: Wed Mar 25, 2009 7:06 pm
yea mischief is right. you can use cin.get(); for the same thing without the baggage.

### Re: C++ program

Posted: Thu Mar 26, 2009 1:54 am
wow awsome thankz guyz

#include "stdafx.h" is required to compile though.

wats the difference between cin.get(); and system("pause"); ?

### Re: C++ program

Posted: Thu Mar 26, 2009 3:56 am
stdafx.h is not required to compile. it's probably because your compiler is using a precompiled header.

cin is an object of type istream. the system() function passes the text to a command interpreter (if available) and then that interpreter tries to execute it.

in this situation, system() passes the string "pause" to the windows command prompt, which in turn executes the program 'pause'. more information here about the system() function.

basically, it's bad because when you use system("pause"), you presume your system has a command interpreter, and you also presume there is a program or shell feature called 'pause', and you also presume that 'pause' does in fact block, since system() waits for the command passed to it to terminate before returning. in short, if pause is a program that does nothing and immediately dies, your program does not pause.

it is much better to use cin.get() or similar, because it is portable and has much less overhead.

### Re: C++ program

Posted: Thu Mar 26, 2009 7:06 am
i usually use cin.get(), too.
but is there an alternative to the pause-command where you only have to press 1 key?
i think its hard to find something that reacts on key press without enter, but maybe there is something that reacts on enter without any input?

### Re: C++ program

Posted: Thu Mar 26, 2009 8:06 am
hascon wrote:i usually use cin.get(), too.
but is there an alternative to the pause-command where you only have to press 1 key?
i think its hard to find something that reacts on key press without enter, but maybe there is something that reacts on enter without any input?

'enter' IS input.

### Re: C++ program

Posted: Thu Mar 26, 2009 8:50 am
of course it is, i wanted to express that i think of pressing enter without any other input.
i just want something that reacts on a single key press, and i think enter would be the only way?

but lets stop here, im steeling bigfishs thread ^^

### Re: C++ program

Posted: Fri Mar 27, 2009 2:42 am
hascon wrote:of course it is, i wanted to express that i think of pressing enter without any other input.
i just want something that reacts on a single key press, and i think enter would be the only way?

but lets stop here, im steeling bigfishs thread ^^

both cin.get() and system("pause") will react to ANY key being pressed, "enter" (or return) key or otherwise..