2 Replies - 159 Views - Last Post: 14 October 2012 - 03:04 PM Rate Topic: -----

#1 exbound  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-October 12

Simple Multipli. Program (Requesting additional attempts if wrong)

Posted 14 October 2012 - 11:35 AM

I'm extremely new to C++ and programing in general just so you have a heads up. I'm currently trying to write a program that randomly generates two values (0-10) and asks the user to enter the answer. If code is correct, the program will reply stating "very good!" or something along those lines to let the user know they were correct. However if not correct a reply will be returned to the user stating that they were wrong and to please try again.

This is where my problem pops up. The program is only using a sentinel value to exit the loop. I want the user to be able to attempt the question again until they get it correct. However after a incorrect response is give it returns a message letting them know it was incorrect, but then creates a new problem for the user without allowing them to attempt the incorrect problem again. How do I go about letting them have additional attempts while only keeping a sentinel value to exit the loop?

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;


void main()
{

	int product;
	int userInput;

	do
	{
		srand(time(0));

		int firNum = (rand() % 10);
		int secNum = (rand() % 10);
	
		int product = firNum*secNum;

		cout << "How much is " << firNum << " times " << secNum << " (-1 to End) ?" << endl;
		cin >> userInput;

		if (userInput == -1)
			break;

		if (userInput == product)
		cout << "Very Good!" << endl;

		else
		cout << "No. Please try again.\n";
		

	} while (userInput != -1);

	cout << "That's all for now. Bye." << endl;
	
}



Is This A Good Question/Topic? 0
  • +

Replies To: Simple Multipli. Program (Requesting additional attempts if wrong)

#2 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: Simple Multipli. Program (Requesting additional attempts if wrong)

Posted 14 October 2012 - 12:36 PM

The program prompts a new question every time whether you enter correct answer or wrong is because you are changing values of your variables and getting new values on every iteration on line 18 and 19. Also you need to seed the rand only once. Besides that you have declared a variable product on line 11 and didn't used it. You have declared another variable product with scope local to the loop.

And there is no such thing known as void main()... It's :
int main()
{
    // Your code here...
    return 0;
}


I would suggest that move the declaration out from the loop body and srand() too.
    int product, userInput, firNum, secNum;
    srand(time(0));



As for your problem, there can be a good solution. But what I come up with now is:
// Insert a nested do-while
do 
{
   // Same code
   if (userInput == product)
       cout << "Very Good!" << endl;
   else	
   {
       cout << "No. Please try again.\n";
       cout << '?';

       // Prompt the user to try again
       cin >> userInput;
   }
// Check if it is still wrong
} while(userInput != product);



Hope this Helps!
Was This Post Helpful? 0
  • +
  • -

#3 exbound  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-October 12

Re: Simple Multipli. Program (Requesting additional attempts if wrong)

Posted 14 October 2012 - 03:04 PM

Thank you very much! Got me heading in the right direction. I had a couple issues afterwards but was able to figure it out! :bigsmile:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1