#include <cstdlib> 
#include <ctime> 
#include <iostream>

using namespace std; // prototype and name space declaration
bool testAnswer(int, int, int);  
void correctOutput();
void incorrectOutput();
bool mathTest();

//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------

int main()
{
	if (mathTest())
	{
		!(mathTest());
		mathTest();

	}
}

bool mathTest() //main function 
{ 
    srand((unsigned)time(0)); // random number generator 
    int random_integerA, random_integerB; 
	int input;

    for(int index=1; index<=2; index++){
		if (index == 1)
			{
			random_integerA = (rand()%10); 
			}
		else
			{
			random_integerB = (rand()%10);
			}
	}
        cout << random_integerA ; 
		cout << " * ";
		cout << random_integerB ; 
		cout << " is: ";
		cin >> input;
			
		if (testAnswer(random_integerA, random_integerB, input))
			{
				correctOutput();
			}
			else
				incorrectOutput();

		while(testAnswer(random_integerA, random_integerB, input) != true)
		{

			
				cout << random_integerA ; 
				cout << " * ";
				cout << random_integerB ; 
				cout << " is: ";
				cin >> input;

			if (testAnswer(random_integerA, random_integerB, input))
			{
				correctOutput();
				return true;
				mathTest();
				break;
			}
			else
				incorrectOutput();
	
     
}}

//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------

bool testAnswer(int numA, int numB, int inputNum) // 
{
	if ( numA * numB == inputNum)
		return true;
	else
		return false;
}
//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------
void correctOutput() // display correct output
void incorrectOutput() // display incorrect output

1) a simple math multiplication program, that will generate two random number and ask the user to input a correct result.
2) current output
1 * 3 = 3(user input)
correct move on
4 * 4 = 16(user input)
correct move on
1 *0 = 0 (user input)
correct
end of program
3) i need the program to continue to prompt the question everytime the input is correct
4) i don't know why it stop after i enter 3 correct answer, i am expecting it run forever

Recommended Answers

All 24 Replies

Line 17 should be moved to the first line of main() because srand() should be called only once throughout the lifetime of the program.

lines 17-21: If you want to keep asking the questions then why didn't you use a while loop here ? The if will execute it only once and then stop.

line 19: has no affect because of the semicolon at the end of the line. Your compiler should have warned you about that! DO NOT ignore warnings.

lines 31-39 do not have to be a loop. Just execute lines 34 and 38 directly without those loops and tests.


lines 68 and 69 are unreachable -- meaning that there is no way those lines will ever be executed because of the return on line 67.

Here are some problems you should fix:

#include <cstdlib> 
#include <ctime> 
#include <iostream>

using namespace std; // prototype and name space declaration
bool testAnswer(int, int, int);  
void correctOutput();
void incorrectOutput();
bool mathTest();

//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------

int main()
{
	if (mathTest())    // Just call mathTest.  This IF is really confusing...
	{
		!(mathTest());   // especially this stuff.  What are you trying to do?
		mathTest();

	}
}

bool mathTest() //main function 
{ 
    srand((unsigned)time(0)); // random number generator 
    // srand should be called only once.  Put it at the beginning of main

    int random_integerA, random_integerB; 
	int input;

    for(int index=1; index<=2; index++){  
		if (index == 1)
			{
			random_integerA = (rand()%10); 
			}
		else
			{
			random_integerB = (rand()%10);
			}
	}
// The above FOR loop can be written as:
        random_integerA = (rand()%10); 
        random_integerB = (rand()%10); 
// There is no reason for a loop.

        cout << random_integerA ; 
		cout << " * ";
		cout << random_integerB ; 
		cout << " is: ";
		cin >> input;
			
		if (testAnswer(random_integerA, random_integerB, input))
			{
				correctOutput();
			}
			else
				incorrectOutput();

		while(testAnswer(random_integerA, random_integerB, input) != true)
		{

			
				cout << random_integerA ; 
				cout << " * ";
				cout << random_integerB ; 
				cout << " is: ";
				cin >> input;

			if (testAnswer(random_integerA, random_integerB, input))
			{
				correctOutput();
				return true;
				mathTest();    // After you return, you can never reach this statement
				break;
			}
			else
				incorrectOutput();
	
     
}}

//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------

bool testAnswer(int numA, int numB, int inputNum) // 
{
	if ( numA * numB == inputNum)
		return true;
	else
		return false;
}
//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------
void correctOutput() // display correct output
void incorrectOutput() // display incorrect output

How do you want to end the program? You need to know that

After doing a little more testing I see that you have really made mathTest() much more complicated than necessary. You can just flat out delete lines 54-72 because they are redundent and unnecessary. Put a loop in main to call mathTest() as long as it returns true. And mathTest() should only prompt for the answer as it does on lines 42-45 then return either true or false if the user input is correct. You don't have to do any of that other stuff in that function.

how do you add lines to my code?? it is very neat.

how do you add lines to my code?? it is very neat.

just use the language feature
[code=c++] and [/code]

#include <cstdlib> 
#include <ctime> 
#include <iostream>

using namespace std; // prototype and name space declaration
bool testAnswer(int, int, int);  
void correctOutput();
void incorrectOutput();
bool mathTest();
	int rightNum=0;
	int wrongNum=0;
	int qNum = 0;
//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------

int main()
{
	srand((unsigned)time(0)); // random number generator 
	
	while(mathTest())
		mathTest();

}

bool mathTest() //main function 
{ 
    
    int random_integerA, random_integerB; 
	int input;

	random_integerA = (rand()%10); 
	random_integerB = (rand()%10);

		cout << "Q"<<++qNum<<" ";
		cout << "How Much is ";
        cout << random_integerA ; 
		cout << " times ";
		cout << random_integerB ; 
		cout << " ?: ";
		cin >> input;
			
		if(testAnswer(random_integerA, random_integerB, input))
			{
				correctOutput();
				return true;
				cout << "A "<<endl;
				++rightNum;

			}
		else 
			{
				
				while(testAnswer(random_integerA, random_integerB, input)!= true )
				{   
					incorrectOutput(); 
					cout << "Q"<<qNum<<" ";
					cout << "How Much is ";
					cout << random_integerA ; 
					cout << " times ";
					cout << random_integerB ; 
					cout << " ?: ";
					cin >> input;
					++wrongNum;
					
				}
			
				correctOutput();
			
			//if (input == int(e))
			//	return false;
			{
			cout<<"RN " << rightNum<<endl;
			cout<<"WN " << wrongNum<<endl;
			cout<<"QN " << qNum<<endl;
			}
			
		}}

//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------

bool testAnswer(int numA, int numB, int inputNum) // 
{
	if ( numA * numB == inputNum)
		return true;
	else
		return false;
}
//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------
void correctOutput() // display correct output
{

    int random_integer; 

    for(int index=0; index<5; index++)
	{ 
        random_integer = (rand()%10); 
	}
	
	switch (random_integer)
	{
				case 1: cout<<"Correct"<<endl;
		break;
				case 2: cout<<"Nice Answer"<<endl;
		break;
				case 3: cout<<"Great"<<endl;
		break;
				case 4: cout<<"Excellent"<<endl;
		break;
				case 5: cout<<"Awesome"<<endl;
		break;
				case 6: cout<<"Accurate"<<endl;
		break;
				case 7: cout<<"You Are Right"<<endl;
		break;
				case 8: cout<<"Very Good"<<endl;
		break;
				case 9: cout<<"Exactly"<<endl;
		break;
	
			
	}}
//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------
void incorrectOutput() // display incorrect output
{
    int random_integer; 

    for(int index=0; index<5; index++)
	{ 
        random_integer = (rand()%10); 
    }
	
	switch (random_integer)
	{
				case 1: cout<<"Incorrect"<<endl;
		break;
				case 2: cout<<"Wrong Answer"<<endl;
		break;
				case 3: cout<<"Sorry"<<endl;
		break;
				case 4: cout<<"Try One More Time"<<endl;
		break;
				case 5: cout<<"Bad Input"<<endl;
		break;
				case 6: cout<<"Not Right"<<endl;
		break;
				case 7: cout<<"Untrue"<<endl;
		break;
				case 8: cout<<"No, Please Try Again"<<endl;
		break;
				case 9: cout<<"Inaccurate"<<endl;
		break;
	
			
	}}

1) i change the code with all the suggestion provided.
2) my only concern left for this program is how do i end i want to use E or e.
like this if(input == int(e) does it work?
3) also it seems like i can't get to line 48, my correct number counter wounldn't work?

delete line 22 and replace it with a simple semicolon

while(mathTest())
   ;

>> if(input == int(e) does it work?
No. You can not enter a char instead of a digit because cin will not permit it. But what you can do is input as string and convert to int.

string line;
int input;
cin >> line;
if(line == "E" || line == 'e')
   return false
input = atol(line.c_str());
int main()
{
	srand((unsigned)time(0)); // random number generator 
	
	while(mathTest())
		;

}

bool mathTest() //main function 
{ 
    
    int random_integerA, random_integerB; 
	int input;

	random_integerA = (rand()%10); 
	random_integerB = (rand()%10);

		cout << "Q"<<++qNum<<" ";
		cout << "How Much is ";
        cout << random_integerA ; 
		cout << " times ";
		cout << random_integerB ; 
		cout << " ?: ";
		cin >> input;
			
				if (qNum % 10 == 0)
		{
			cout<<"RN " << rightNum<<endl;
			cout<<"WN " << wrongNum<<endl;
			cout<<"QN " << qNum<<endl;
		}
		if(testAnswer(random_integerA, random_integerB, input))
			{
				correctOutput();
				return true;
				cout << "A "<<endl;
				++rightNum;

			}
		else 
			{
				
				while(testAnswer(random_integerA, random_integerB, input)!= true )
				{   
					incorrectOutput(); 
					cout << "Q"<<qNum<<" ";
					cout << "How Much is ";
					cout << random_integerA ; 
					cout << " times ";
					cout << random_integerB ; 
					cout << " ?: ";
					cin >> input;
					++wrongNum;
					
				}
			
				correctOutput();
			

			
		}}

1) i think i will not need to end this because i need to display the % right and wrong after every 10 question.
2) why is my 33-39 not working, i try to tested it by placeing a cout<<"A" and it never prints so does my counter for the correct number never add up???

Oh i see i think i need to place the return at the last line of the if ()

move line 36 down to after line 38.

if ((qNum % 10) == 0)
		{
				cout<<"RN " << rightNum<<endl;
				cout<<"WN " << wrongNum<<endl;
				cout<<"QN " << qNum<<endl;
}

any good suggestion of where i should place these at??

one more quick help this

while ( input < 0 || !cin.good() ) // validation  
			{
				cout << "Positive Numbers Only.\n";
				cout << "Enter Again: ";
				cin.clear();
				cin >> input;
			}

1) this is my validation for the input
2) i want it to pick up error message when someone entered a character
3) input -1, prompt for reentering a new number
input a, infinite loop error
4) how do i fix that?

#include <cstdlib> 
#include <ctime>
#include <iomanip>
#include <iostream>

using namespace std; // prototype and name space declaration
int sqr(int);
int cube(int);
int fourthPower(int);
int fifthPower(int);
int sixthPower(int);
bool printNum();
void show(int (*fn)(int), int); 

//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------

void main()
{	
	
		
	
	printNum();

}
//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------

bool printNum()
{
	char input;
	int block =0;

	cout<< "To Square The Number Enter: S"<<endl;
	cout<< "To Cube The Number Enter: C"<<endl;
	cout<< "To the Fourth Power of The Number Enter: F"<<endl;
	cout<< "To the Fifth Power of The Number Enter: T"<<endl;
	cout<< "To the Sixth Power of The Number Enter: X"<<endl;
	cout<< "To Quit: Q"<<endl;
	cin >> input;

	if ( input == 'q' || input =='Q')
		{
		cout<<"end"<<endl;
		return 0;
		}
			
	while (true)

		{

			int lowVal ,maxVal;


			cout <<"Enter A Number For Low Bound: ";
			cin >> lowVal;
					
			while ( !cin.good() ||( lowVal < 0 || lowVal >100 )) // validation  
			{

				cout << "Positive Numbers Only.\n";
				cout << "Enter Again: ";
				cin.clear();
				cin >> lowVal;
			}
			cout << "Enter A Number For Upper Bound : ";
			cin >> maxVal;

			while ( !cin.good() || maxVal < lowVal || maxVal >100 ) 
			{
				cout << "Positive Numbers Only.\n";
				cout << "Upper Bound Can' Be Smaller Than Lower Bound";
				cout << "Enter Again: ";
				cin.clear();
				cin >> maxVal;
			}
		
			if ( (lowVal == 0 && maxVal == 0))
				printNum();

			cout <<"Printing "<<(maxVal-lowVal)+1<<" numbers"<<endl;

			for (int x = lowVal; lowVal<=maxVal; lowVal++)
			{
				if (input == int('s') || input == int('S'))
				{
					cout.setf(ios::left);
					cout<< setw(5);
					show(sqr, lowVal);
					block++;
				}
				else if (input == int('c') || input == int('C'))
				{
					cout.setf(ios::right);
					cout<< setw(5);
					show(cube, lowVal);
					block++;
				}
				else if (input == int('f') || input == int ('F'))
				{
					cout.setf(ios::right);
					cout<< setw(8);
					show(fourthPower, lowVal);
					block++;
				}
				else if (input == int('t') || input == int ('T'))
				{
					cout.setf(ios::right);
					cout<< setw(10);
					show(fifthPower, lowVal);
					block++;
				}
				else if (input == int('x') || input == int ('X'))
				{
					cout.setf(ios::right);
					cout<< setw(10);
					show(sixthPower, lowVal);
					block++;
				}
				if (block == 10)
				{
					block = 0;
					cout<<"\n";
				}
			}
			cout <<"\n";
			block = 0;
			
	}

   
}
//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------
int sqr(int num) // list of functions 
{
    return num * num;
}

int cube(int num) // this one is to cube
{
    return num * num * num;
}
int fourthPower(int num)
{
    return num * num * num * num;
}
int fifthPower(int num)
{
    return num * num * num * num * num;
}

int sixthPower(int num)
{
    return num * num * num * num * num * num;
}

void show(int (*fn)(int), int num) // pointer
{
    cout << fn(num);
}

1) a simple math power function
2) somehow i wasn't able to figure out how to quit the program
3) i enter q on the first time and it quit the program
but when i enter inputs like this
s- picked square
2- low bound
4- upper bound
it print like
4 9 16
and ask me to reenter the low and upper bound
i enter
0 and 0 which brings me back to the original option s, c, f, .... q
i enter q this time but it didn't stop the program???? why???
4) i am done with this program all i need is to being able to quit

#include <cstdlib> 
#include <ctime>
#include <iomanip>
#include <iostream>

using namespace std; // prototype and name space declaration
int sqr(int);
int cube(int);
int fourthPower(int);
int fifthPower(int);
int sixthPower(int);
bool printNum();
void show(int (*fn)(int), int); 

//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------

void main()
{	
	
		
	char first[30], last[30];  

	cout << "-------------------------------"<<endl; // title
	cout << "-------------------------------"<<endl;
	cout << "      Simple Math Function     "<<endl;
	cout << "-------------------------------"<<endl;
	cout << "-------------------------------"<<endl;
	cout << "*******************************"<<endl;
	cout << "This is a simple math function "<<endl;
	cout << "program design to show you     "<<endl;
	cout << "any of the 2nd,3rd,4th,5th,6th "<<endl;
	cout << "power of postitive integer     "<<endl;
	cout << "in any interval in between 0   "<<endl;
	cout << "and 100.                       "<<endl;
	cout << "\n\n";

    cout << "Please Enter Your First Name: ";
    cin >> first;
			
    cout << "\n";
	cout << "Please Enter Your Last Name: ";
    cin >> last;

    cout << "\n\nThank you " << first << " " << last;
	cout << "\n";
	cout << "First choose a power and then "
		 << "Enter the Bound limits        "<<endl;

	cout <<"Upper Bound And Lower Bound "
		 <<"need to be range of 0 and 100"<<endl;
	
	cout << "Begins Excerise";

	cout << "\n\n";
	printNum();

}
//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------

bool printNum()
{
	char input;
	int block =0;

	cout<< "To Square The Number Enter:              S"<<endl;
	cout<< "To Cube The Number Enter:                C"<<endl;
	cout<< "To the Fourth Power of The Number Enter: F"<<endl;
	cout<< "To the Fifth Power of The Number Enter:  T"<<endl;
	cout<< "To the Sixth Power of The Number Enter:  X"<<endl;
	cout<< "To Quit Enter:                           Q"<<endl;
	cin >> input;
	
	while ( !cin.good() || input!= 's' || input !='S'
		|| input != 'c' || input !='C'
		|| input != 'f' || input !='F'
		|| input != 't' || input !='T'
		|| input != 'x' || input !='X'
		|| input != 'q' || input !='Q')
		{			
			cout << "Wrong Choice.\n";
			cout << "Enter Again: ";
			cin.clear();
			cin.ignore(80,'\n');
			cin >> input;
		}
	
	if ( input == 'q' || input =='Q')
		{
		cout << "Program Ended. "<<endl;
			return 0;
		}
			
	while (true)
		{

			int lowVal ,maxVal;


			cout <<"Enter A Number For Low Bound: ";
			cin >> lowVal;
					
			while ( !cin.good() ||( lowVal < 0 || lowVal >100 )) // validation  
			{

				cout << "Positive Numbers Only.\n";
				cout << "Enter Again: ";
				cin.clear();
				cin.ignore(80,'\n');
				cin >> lowVal;
			}
			cout << "Enter A Number For Upper Bound : ";
			cin >> maxVal;

			while ( !cin.good() || maxVal < lowVal || maxVal >100 ) 
			{
				cout << "Positive Numbers Only.\n";
				cout << "Upper Bound Can' Be Smaller Than Lower Bound";
				cout << "Enter Again: ";
				cin.clear();
				cin.ignore(80,'\n');
				cin >> maxVal;
			}
		
			if ( (lowVal == 0 && maxVal == 0))
				printNum();

			cout <<"\n\n";
			cout <<"Printing "<<(maxVal-lowVal)+1<<" numbers"<<endl;

			for (int x = lowVal; lowVal<=maxVal; lowVal++)
			{
				if (input == int('s') || input == int('S'))
				{
					cout.setf(ios::left);
					cout<< setw(5);
					show(sqr, lowVal);
					block++;
				}
				else if (input == int('c') || input == int('C'))
				{
					cout.setf(ios::right);
					cout<< setw(5);
					show(cube, lowVal);
					block++;
				}
				else if (input == int('f') || input == int ('F'))
				{
					cout.setf(ios::right);
					cout<< setw(8);
					show(fourthPower, lowVal);
					block++;
				}
				else if (input == int('t') || input == int ('T'))
				{
					cout.setf(ios::right);
					cout<< setw(10);
					show(fifthPower, lowVal);
					block++;
				}
				else if (input == int('x') || input == int ('X'))
				{
					cout.setf(ios::right);
					cout<< setw(10);
					show(sixthPower, lowVal);
					block++;
				}
				if (block == 10)
				{
					block = 0;
					cout<<"\n";
				}
			}
			cout <<"\n";
			block = 0;
			
	}
   
}
//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------
int sqr(int num) // list of functions 
{
    return num * num;
}

int cube(int num) // this one is to cube
{
    return num * num * num;
}
int fourthPower(int num)
{
    return num * num * num * num;
}
int fifthPower(int num)
{
    return num * num * num * num * num;
}

int sixthPower(int num)
{
    return num * num * num * num * num * num;
}

void show(int (*fn)(int), int num) // pointer
{
    cout << fn(num);
}

1) line77-92
i want to validate the user input. so i set up a loop that only s,S,c,C.....q,Q can be accepted.
but when i inputs s it is also giving me a wrong option output. which is not suppose to happen.

2) if i take line 77-92 away.
if i enter q the program will end
if i enter s it will prompt me to enter low and upper bound
if i enter s, 1, 2, 0 and 0
it will brings me back to the original prompt i can choose s,c,f....q.
when i enter q or Q. the program did end why is that???

anyone?

Talk about thread bombing! A full 2/3 of the posts have been made by nicz888, the OP. I for one can't follow the progress here because of multiple posts of 200 line programs. I have no idea what the problem is.

2) if i take line 77-92 away.
if i enter q the program will end
if i enter s it will prompt me to enter low and upper bound
if i enter s, 1, 2, 0 and 0
it will brings me back to the original prompt i can choose s,c,f....q.
when i enter q or Q. the program did end why is that???

Probably because Q or q means quit?

commented: You have some pretty insane intiution(sp) +2

yes q means q, but when the prompt is brought up the second time after i enter 0 and 0. when i enter q it doesn't end the program, which is not right.

Output various values as the program runs and see what variables don't contain values you're expecting.

> I have no idea what the problem is.
And I gave up caring about the "me me me" attitude of the OP with the constant bumping, and the not reading the forum rules, or caring enough to format the code.
Too much effort, too little reward.

>>or caring enough to format the code
In defense of the OP his post #16 was posted to correct the code tag mistake he made in the previous post, which I deleted. So he did try to use code tags, but he could have just edited his post that contained the code tag error instead of creating yet another post. I'm not about to criticize someone for making typing mistakes -- God knows I do enough of that myself.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.