Hello all,

I am taking a C++ class after not touching it for about 4 years now... I am rather dusty. My book is horrible, I am trying to randomly select a switch case for a word output. This is my latest attempt:
*******************************************************

void right();
void wrong();

int main()
{

int a, b;
int answer;
int input;	

cout << "This program will work on your multiplication up to 12\n"
	 << "If you wish to quit, enter -1 to exit this program.\n";

do 
{
a = rand() % 13 + 1;
b = rand() % 13 + 1;

answer = a * b;

cout << "How much is: " << a << " times " << b << "?\n\n";
	
cin  >> input;
system("cls");
cout << endl;

if (input == answer)
	right();

	else
	{
	do
	{
	if (input == -1)
		break;
	else
		wrong();
	cout << "How much is: " << a << " times " << b << "?\n\n";
	cin  >> input;
	}while (input != answer);
	}
} while (input != -1);

return 0;
}


void right()
{
int c;
c = rand() % 4 + 1;

switch (c)
{
case 1:
	cout << "VERY GOOD! \n";
case 2:
	cout << "EXCELLENT! \n";
case 3:
	cout << "COOL! \n";
default:
	cout << "KEEP UP THE GOOD WORK! \n";
}
}

int wrong()
{
int d;
d = rand() % 4 + 1;

switch (d)
{
case 1:
	cout << "No.  Please Try Again... \n";
case 2:
	cout << "Wrong Try One More Time... \n";
case 3:
	cout << "Try Until You Succeed... \n";
default:
	cout << "No. Keep Trying... \n";
}		
}

*************************************************
The book I have is a paper weight, and I don't have any other books with me. Any ideas would be very appreciated.

Thanks,

Nate

Usually I don't correct and repost code especially for assignments, but because you've got 95% of it right I see no harm. There were only two problems that I've seen that were C++ specific and that is failing to include iostream. Also wrong () and right () were declared as void's but required int's as a return in definition.

In wrong () and right (), breaks are required after each case or it gives you all answers each time it's called.

Also, noticed that rand () % 13 + 1 would return a value of 13 occasionally which is contrary to prompting. A null display in wrong() occured once too. I didn't investigate in detail though.

I've complied this code with VC++ 6.0 and it worked fine. 84 errors first time, but most of those were resolved by include appropriate headers

#include <iostream.h>
#include <stdlib.h>
void right();
void wrong();
int main()
 {
 int a, b, answer, input; 
 cout << "This program will work on your multiplication up to 12\n"
	<< "If you wish to quit, enter -1 to exit this program.\n";
 do 
  {
  a = rand() % 12 + 1;
  b = rand() % 12 + 1;
  answer = a * b;
  cout << "How much is: " << a << " times " << b << "?\n\n";
  cin >> input;
  system("cls");
  cout << endl;
  if (input == answer)
   right();
  else
   {
   do
	{
	if (input == -1)
	 break;
	else
	 wrong();
	 cout << "How much is: " << a << " times " << b << "?\n\n";
	 cin >> input;
   } while (input != answer);
  }
 } while (input != -1);
 return 0;
 }

void right()
 {
 int c;
 c = rand() % 4 + 1;
 switch (c)
  {
  case 1:
   cout << "VERY GOOD! \n";
   break;
  case 2:
   cout << "EXCELLENT! \n";
   break;
  case 3:
   cout << "COOL! \n";
   break;
  default:
   cout << "KEEP UP THE GOOD WORK! \n";
  }
 }
void wrong()
 {
  int d;
  d = rand() % 4 + 1;
  switch (d)
   {
   case 1:
	cout << "No. Please Try Again... \n";
	break;
   case 2:
	cout << "Wrong Try One More Time... \n";
	break;
   case 3:
	cout << "Try Until You Succeed... \n";
	break;
   default:
	cout << "No. Keep Trying... \n";
   } 
 }

Please include code tags next time, it makes reading a lot easier.

Lol, so close but yet so many hours... Thank you for turning on the lights. :o Next is to sure up the rest of the operations. I really appreciate the fresh set of eyes... Have a good one.

Nate

This article has been dead for over six months. Start a new discussion instead.