If statement re-do problem

This is a discussion on If statement re-do problem within the Windows Programming forums, part of the Platform Specific Boards category; Ok i took a problem from the book where i had to write a progrm that will take how much ...

  1. #1
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331

    If statement re-do problem

    Ok i took a problem from the book where i had to write a progrm that will take how much is in yer bank account, wether you want to deposit or w/drawl, then checks yer new balance. It accepts capital and lowercase "W" and "D", but i want it to re-prompt for a transaction type when it gets a invalid type. Which it DOES but not till after it asks for the transaction amount:

    Code:
    //Program file chkbook.cpp
    //This program updates a checkbook
    //9-9-02
    
    #include <iostream.h>
    #include <iomanip.h>
    
    int main ()
    {
    	double startingbalance, endingbalance, transamount;
    	char transtype;
    
    	//Module for getting the data.
    
    	cout<<"Enter the Starting Balance abd press <enter>: ";
    	cin>>startingbalance;
    	cout<<"Enter the Transaction Type (D) Deposit or (W) Withdrawl ";
    	cout<<"And press <enter>: ";
    	cin>>transtype;
    	cout<<"Enter the transaction amount and press <enter>: ";
    	cin>>transamount;
    
    	//Module for performing computations.
    
    	if (transtype == 'D')
    		endingbalance = startingbalance + transamount;
    	else if (transtype == 'd')
    		endingbalance = startingbalance + transamount;
    	else if (transtype == 'W')
    		endingbalance = startingbalance - transamount;
    	else if (transtype == 'w')
    		endingbalance = startingbalance - transamount;
    	else
    		cout<<"Incorrect Transaction Type please re-enter "<<endl;
    		cout<<"Enter the Transaction Type (D) Deposit or (W) Withdrawl ";
    		cout<<"And press <enter>: ";
    		cin>>transtype;
    		
    
    	//Module for displaying results
    
    	cout<<setiosflags(ios::fixed | ios::showpoint | ios::right)<<setprecision(2);
    	cout<<endl;
    	cout<<"Starting Balance $"<<startingbalance<<endl;
    	cout<<"Transaction      $"<<transamount<<" "<<transtype<<endl;
    	cout<<"Ending Balance   $"<<endingbalance<<endl;
    
    	return 0;
    
    }

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    What you need to do is get the transaction typ, check and validate it is D, d, w or W and then get the transaction amount. If it is not a valid input then post a message and let them try again using a loop.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    so kinda like:

    If transaction type not equal to letter reprompt.

    I get that its how to do it. I'll figure it out in lunch tommorow i am beat i been doing different c++ scince 3rd period and i gotta tell ya, my vb oriented mind is hurt

  4. #4
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    i can't get it, i dunno why i cannot make this stupid if thingy work.

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Code:
    //Program file chkbook.cpp
    //This program updates a checkbook
    //9-9-02
    
    #include <iostream.h>
    #include <iomanip.h>
    
    int main ()
    {
    	double startingbalance, endingbalance, transamount;
    	char transtype;
    
    	cout<<"Enter the Starting Balance and press <enter>: ";
    	cin>>startingbalance;
    
    	//Module for getting the data.
    	while (1){
    		cout<<"Enter the Transaction Type (D) Deposit or (W) Withdrawl ";
    		cout<<"And press <enter>: ";
    		cin>>transtype;
    		cout<<"Enter the transaction amount and press <enter>: ";
    		cin>>transamount;
    
    		//Module for performing computations.
    
    		if(transtype == 'D' || transtype == 'd'){
    			endingbalance = startingbalance + transamount;
    			break;
    		}
    		else if(transtype == 'W' || transtype == 'w'){
    			endingbalance = startingbalance - transamount;
    			break;
    		}
    		else{
    			cout << "Improper transaction type. Try again" << endl;
    			continue;
    		}
    	}
    	
    	//Module for displaying results
    	
    	cout<<setiosflags(ios::fixed | ios::showpoint | ios::right)<<setprecision(2);
    	cout<<endl;
    	cout<<"Starting Balance $"<<startingbalance<<endl;
    	cout<<"Transaction      $"<<transamount<<" "<<transtype<<endl;
    	cout<<"Ending Balance   $"<<endingbalance<<endl;
    	
    	return 0;
    
    }

  6. #6
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    omg thanks i get it now. I just needed to see it done a different way, thanks man.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By nashsclay in forum C Programming
    Replies: 4
    Last Post: 05-03-2008, 02:34 PM
  2. Switch statement problem
    By jalex39 in forum C Programming
    Replies: 6
    Last Post: 03-08-2008, 03:05 PM
  3. having problem with string statement during a loop!
    By Hp7130p in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2005, 09:40 AM
  4. Major Problem
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2002, 12:06 PM

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