Hiding strings dependent on the user input

This is a discussion on Hiding strings dependent on the user input within the C++ Programming forums, part of the General Programming Boards category; Hello guys I'm new here this is my first post. I'm also new to c++ and programming in general so ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    5

    Hiding strings dependent on the user input

    Hello guys I'm new here this is my first post. I'm also new to c++ and programming in general so don't be to hard on me if my code is sloppy. I'm taking my first programming course in my college career so yes this is a question about my homework I did read the rules about homework but if this post isn't within the rules let me know. Ok now that I've given you some background that you probably don't care about here is my code:

    Code:
    #include <iostream>
    #include <cmath>
    #include <stdlib.h>
    #include <time.h>
    #include <unistd.h>
    using namespace std;
    int main ()
    {
    	int c,i,y,x; // c is menu choice x and y are random numbers
    	double a; // user answer
    	int t=0; // to track number of correct answers
    	char repeat='y';
    	
    	srand(time(NULL) + getpid()); // seed random number from current time
    	
    	while (repeat == 'y')
    	{
    		// menu
    		cout << "Which math skill would you like to test? (Press  a number between 0 and 4)" << "\n";
    		cout << "0. Quit" << "\n";
    		cout << "1. Addition" << "\n";
    		cout << "2. Subtraction" << "\n";
    		cout << "3. Multiplication" << "\n";
    		cout << "4. Division       ";
    		cin >> c;
    	
    		if (c>4 || c<0) // display an error if there is an invalid choice
    		{
    			cout << "Error! Terminating program." << endl;
    		}
    		else if (c == 0) // quit the program
    		{
    			cout << "Quitting program. Goodbye!" << endl;
    		}
    		for (i=0; i<4; i++)  // repeat statements 4 times
    		{
    			x=((int)rand() % 10+1);
    			y=((int)rand() % 10+1);
    			if (c == 1) // addition
    			{
    				cout << x << " + " << y << " = ";
    				cin >> a;
    			
    				if (a == x + y)
    				{
    					cout << "Correct!" << endl;
    					t++;
    				}
    				else if (a != x + y)
    				{
    					cout << "Incorrect!" << endl;
    				}
    			}
    			else if (c == 2) // subtraction
    			{
    				cout << x << " - " << y << " = ";
    				cin >> a;
    			
    				if (a == x - y)
    				{
    					cout << "Correct!" << endl;
    					t++;
    				}
    				else if (a != x - y)
    				{
    					cout << "Incorrect!" << endl;
    				}
    			}
    			else if (c == 3) // multiplication
    			{
    				cout << x << " * " << y << " = ";
    				cin >> a;
    			
    				if (a == x * y)
    				{
    					cout << "Correct!" << endl;
    					t++;
    				}
    				else if (a != x * y)
    				{
    					cout << "Incorrect!" << endl;
    				}
    			}
    			else if (c == 4) // division
    			{
    				cout << x << " / " << y << " = ";
    				cin >> a;
    			
    				if (a == x / y)
    				{
    					cout << "Correct!" << endl;
    					t++;
    				}
    				else if (a != x / y)
    				{
    					cout << "Incorrect!" << endl;
    				}
    			}
    		}
    	cout << t << " correct out of 4" << endl; // display number of correct answers	
    	cout << "Would you like to try again? (y/n) "; // prompt user to repeat or quit program
    	cin >> repeat;
    	}
    }
    As you see the program is pretty simple. The problem I am having is that if the user inputs 0 or 0>c>4 then it still displays the amount correct and gives them the option to repeat the program. If they press 0 it should completely quit the program and if they input anything less than 0 or greater than 4 it should ask them if they would like to repeat, but not display the total correct. I have tried placing the last two cout statements inside of the for (i=0; i<4; i++) braces but that just repeated the total correct four times. I'm not sure where to place these statements or how I should edit my code. Thanks in advance for any help!

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    In the cases where c is 0 or out of range, you print an error message but otherwise don't do anything about it, so your program continues on. You should either exit() or return when the user inputs a 0. For the number correct output, you'll need to put another "if" check there to see if c is out of range.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    Thank you that is exactly what I needed. Can't believe I didn't think of doing that. Thanks again.

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Please note that although correct, your second if is redundant in almost all cases:

    Code:
    if (a == x - y)
    {
    ...
    }
    else /* at this point, we already determined that a is not x-y or else we'd be in the if block above, so the following is not neccessary: -> */   if (a != x - y)
    {
    ...
    }
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-07-2006, 09:53 PM
  2. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 07:37 AM
  3. Hiding Input
    By bigbrainstorms in forum C++ Programming
    Replies: 4
    Last Post: 03-06-2003, 04:24 AM
  4. Format User Input When using strings
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 03-08-2002, 03:59 AM
  5. Hiding input password
    By kagemand in forum C++ Programming
    Replies: 2
    Last Post: 11-23-2001, 08:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21