943,552 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 373
  • C++ RSS
Apr 19th, 2009
0

Require user action before continuing..

Expand Post »
I need a little help with the below if statement. What i would like to have happen is for the loop caused by the first if statment to also be conditional to a second if statement that will pause the loop output and require user interaction to either continue displaying the looped information or quit. My thought was to pause the loop after every 12 lines of output and require the user to continue or exit the loop. Any help is greatly apreciated. As you can see below i was not succeful in my attempt. Thanks again.

 for (int a = 1; a < months+1; a++)
	{
	if (a < months){
	currInt = principal * interest;
	principalPaid = payment - currInt;
	principal = principal - principalPaid;
cout << "Payment Number" << "\tPayment Amount" << "\tInterest Paid" << "\tBalance "<< endl;
cout << a << "\t\t$" <<payment << "\t$" <<currInt << "\t\t$" <<principal << endl;
if (a = months+12){
          cout << "Would you like to Continue (Y/N)?\n";
           cin >> R;
           while (R == 'y'  ||  R == 'Y');
return 0;
	}
            }
	else {
cout << endl;
cout << a << "\t*******Your loan is paid in FULL!!*******" << endl;
}
Last edited by Beginerman; Apr 19th, 2009 at 11:33 pm. Reason: additional comment
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Beginerman is offline Offline
3 posts
since Apr 2009
Apr 19th, 2009
0

Re: Require user action before continuing..

I think you need another ending bracket for the for loop.
Reputation Points: 21
Solved Threads: 12
Junior Poster in Training
MatEpp is offline Offline
79 posts
since Jan 2009
Apr 20th, 2009
0

Re: Require user action before continuing..

I have not seen your code thoroghly but the immediate thing I would like to point out it this:
if (a = months+12){ as you may see you have used a = rather than ==. In c++( and even in C) = will always evaluate true. To test for a condition == should be used.
So change it too ==.
Last edited by siddhant3s; Apr 20th, 2009 at 3:30 am.
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007
Apr 20th, 2009
0

Re: Require user action before continuing..

OK. I have changed the conditional operator to the == and the additional bracket has been added but now the output will stop after the first line of output data. I guess i may not be evaluating the months + 12 condition correctly. I originally wanted it to stop after every 12 lines of output data and ask to continue. I think im almost there but still need a little more assistance.

C++ Syntax (Toggle Plain Text)
  1. if (a == months+12){
  2. cout << "Would you like to Continue (Y/N)?\n";
  3. cin >> R;
  4. } while (R == 'y' || R == 'Y');
  5. return 0;
Last edited by Beginerman; Apr 20th, 2009 at 8:45 am. Reason: additional comment
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Beginerman is offline Offline
3 posts
since Apr 2009
Apr 20th, 2009
0

Re: Require user action before continuing..

>>I originally wanted it to stop after every 12 lines of output
Your condition is wrong.
Basically you want to stop at every multiple of 12. that is when a is 12,24,36...
That is a divided by 12 should yield no remainder.
So change your condiditon to :
C++ Syntax (Toggle Plain Text)
  1. if(a %12==0)
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007
Apr 23rd, 2009
0

Re: Require user action before continuing..

Problem solved for my first question. I thank everyone for your help.
I solved the problem as follows.
C++ Syntax (Toggle Plain Text)
  1. //Beak loop for every 15 lines of displayed payments
  2. if( a %15== 0)
  3. {
  4. cout << "Press Enter to continue.";
  5. cin.get();
  6. }
  7. }

Next question:
I know the text highlighted in red below is incorrect. So, i am asking if anyone knows of how i can validate whether or not the user has entered a number and if they did not enter a number then i want to offer them to re-enter information or exit the program. I have searched both my educational document and the net and keep coming back to first converting the user input data into a string and then validate each character in the string. However, i do not want to go this far with this code at all. I simply want to validate if it is a number or not and then throw an error and ask to re-enter the entry. If you kind programming gurus believe the string validation is the best method then please give me an example of how to do this with ease. I appreciate your help immensly.

#include <iostream>
#include <math.h>
#include <iomanip>
#include <cctype>

using namespace std;

int main()
{
	char R; //response variable
    do{
	//Declaring variables
	float loanAmt;  
	int months;    
	int loanTerm;   
	float loanRate;  
	float principal;
	float principalPaid;
	float currInt;
	float payment, interest; 
	//Beginning of user input data
         cout << endl;
	     cout << "Enter the Loan Principal Amount: ";
         cin >> loanAmt;
		 if (!isdigit(R))
		 {
			cout << "You entered an invalid Loan Amount" <<endl;
			cout << "please enter a valid amount." <<endl;		
			return main();  
		 }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Beginerman is offline Offline
3 posts
since Apr 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: i have a problem in programming
Next Thread in C++ Forum Timeline: Serial Port Programming Visual C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC