## C++ program

### C++ program

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");}`
bigfish
New User

Posts: 10
Joined: Tue Mar 24, 2009 6:03 am
Blog: View Blog (0)

### Re: C++ program

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");}`
hascon
New User

Posts: 8
Joined: Tue Mar 24, 2009 5:01 pm
Blog: View Blog (0)

### Re: C++ program

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
The whole secret of existence is to have no fear. Never fear what will become of you, depend on no one. Only the moment you reject all help are you freed.
--Buddha

mischief
Poster

Posts: 355
Joined: Wed Jan 07, 2009 4:16 pm
Blog: View Blog (0)

### Re: C++ program

yea mischief is right. you can use cin.get(); for the same thing without the baggage.
totalnoob314
New User

Posts: 2
Joined: Wed Jan 21, 2009 2:06 pm
Blog: View Blog (0)

### Re: C++ program

wow awsome thankz guyz

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

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

Posts: 10
Joined: Tue Mar 24, 2009 6:03 am
Blog: View Blog (0)

### Re: C++ program

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.
The whole secret of existence is to have no fear. Never fear what will become of you, depend on no one. Only the moment you reject all help are you freed.
--Buddha

mischief
Poster

Posts: 355
Joined: Wed Jan 07, 2009 4:16 pm
Blog: View Blog (0)

### Re: C++ program

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?
hascon
New User

Posts: 8
Joined: Tue Mar 24, 2009 5:01 pm
Blog: View Blog (0)

### Re: C++ program

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.
The whole secret of existence is to have no fear. Never fear what will become of you, depend on no one. Only the moment you reject all help are you freed.
--Buddha

mischief
Poster

Posts: 355
Joined: Wed Jan 07, 2009 4:16 pm
Blog: View Blog (0)

### Re: C++ program

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 ^^
hascon
New User

Posts: 8
Joined: Tue Mar 24, 2009 5:01 pm
Blog: View Blog (0)

### Re: C++ program

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..
bird7727
New User

Posts: 48
Joined: Sun Feb 01, 2009 12:43 am
Blog: View Blog (0)

Next