Hi there everyone.

I'm new to the boards (and C++ as well) :confused:

I was wondering if someone out there would be so kind as to guide me or explain the best way to handle this dilemma I'm having.

I''ve spent a full week coding my programming assignment for this week, and I finally got it working. The goal was to take last week's assignment and modify it to do the following:

*Accept user input for total shares desired to be purchased
*Accept user input for the price per share
*Display the results (some values are hard-coded, such as the commission, minimum commission amount, etc, which is correct).
*Offer the user a choice to continue buying shares or quit

I managed to get all of the above working. Now, here's where it all fell apart. I failed to read properly and noticed that this is also a requirement:

I need to check for input errors (I guess make sure numbers are entered instead of letters), and if the input is incorrect, it should display a message asking the users if they'd like to start from the beginning or quit.

I'm afraid of even thinking that I may have to rewrite this code again. I'm hoping one of you C++ gurus can take a look at my code and advise if there's an easier way to add this requirement to what I have. I thought about using strings, but I don't know how to work with those too well...

Any help is very much appreciated, and if you need more info from me, feel free to ask!! :o)

Thanks so much!
Cooberman76

Attachments
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{	
	//Using double because I realized that using 'int' caused
	//several errors in the compiling.  The textbooks recommended
	//this approach.  The values below are hard-coded per the requirement
	double pricePerShare;
	int totalShares; 
	double commissionRate = 0.02;
	double minCommission = 39.95;
	int option;
	
	//Displays the title of the program per the requirement
	//Modified for Week Three
	cout << "This is the Second Individual Assignment" << endl;
	cout << "'Total Shares Paid Program using Input and Menus'" << endl;
	cout << "by (my name was here)" << endl;
	cout << endl;
	
	//Started the do-while loop to allow the user to "continue" purchasing shares
	//or end the program
	do {
	
		//Graphical explanation on how to proceed
		cout << "----------------------------------------------------------" << endl;
		cout << "|                        MAIN MENU                       |" << endl;
		cout << "----------------------------------------------------------" << endl;
		cout << "| Follow the prompts to purchase specific share amounts. |" << endl;
		cout << "| After finishing, you may press '1' to continue, or     |" << endl;
		cout << "| press '0' to end this transaction.                     |" << endl;
		cout << "----------------------------------------------------------" << endl;
		cout << endl;
	
		//Displays the information after all calculations have been performed
		//Added precision
		cout << fixed << showpoint;
		cout << setprecision(2);
		cout << "Please enter the total shares you wish to purchase: ";
		cin >> totalShares;
		cout << "Please enter the current price per share: ";
		cin >> pricePerShare;
		cout << endl;
	
	//Sets the equation for the following displayed items
	//Relocated within the loop to correct min commission calc errors
	double commissionFee = totalShares * commissionRate;
	double totalPaid = pricePerShare * totalShares + commissionFee;

		//setting the condition that makes the minimum
		//commission change to $39.95 if less than that
		//amount
		if (commissionFee < minCommission)
		    commissionFee = 39.95;

		cout << "Total Shares Purchased are: " << totalShares << endl;           
		cout << "Total Commission Due is: $" << commissionFee << endl;
		cout << "The Total Amount that will be paid is: $" << totalPaid << endl;
		cout << endl;
		cout << "Would you like to continue purchasing shares?" << endl;
		cout << "(Type '1' to continue, '0' to end): ";
		cin >> option;
		cout << endl;
		cout << endl;
		cout << "===============================================================" << endl; //added for cosmetic reasons
		cout << endl;
		cout << endl;
	} while (option == 1); //ends the loop if condition is met
	
	cout << "Thank you for using our Total Shares Paid Program!" << endl;  //acknowledges that the user has ended the program
	return 0;
}

Hi there everyone.

I'm new to the boards (and C++ as well) :confused:

I was wondering if someone out there would be so kind as to guide me or explain the best way to handle this dilemma I'm having.

I''ve spent a full week coding my programming assignment for this week, and I finally got it working. The goal was to take last week's assignment and modify it to do the following:

*Accept user input for total shares desired to be purchased
*Accept user input for the price per share
*Display the results (some values are hard-coded, such as the commission, minimum commission amount, etc, which is correct).
*Offer the user a choice to continue buying shares or quit

I managed to get all of the above working. Now, here's where it all fell apart. I failed to read properly and noticed that this is also a requirement:

I need to check for input errors (I guess make sure numbers are entered instead of letters), and if the input is incorrect, it should display a message asking the users if they'd like to start from the beginning or quit.

I'm afraid of even thinking that I may have to rewrite this code again. I'm hoping one of you C++ gurus can take a look at my code and advise if there's an easier way to add this requirement to what I have. I thought about using strings, but I don't know how to work with those too well...

Any help is very much appreciated, and if you need more info from me, feel free to ask!! :o)

Thanks so much!
Cooberman76

how about:

if ( totalShares !> 0)
displayError();

UPDATE:
-------
I just tried the "if" statement with a few modifications, and it does the job as long as numerics are concerned, but any letters that are entered crash the program.

I guess what I need is to be able to incorporate something that would check for invalid characters or numbers and prompt with a message. And then, it should opt to retry or quit the program altogether.

Any thoughts?


-------------------------------------------
Thanx for responding, Nizzy...

I actually contemplated adding an if statement to the mix, but I wasn't sure if that would work. Let me incorporate a version of your suggestion into my code. If that works (for validation purposes at least), I'll just need to worry about prompting the user whether to continue or quit.

Thanx again!

This little while loop will ask for an integer input until it gets one ...

// controlled integer input in C++:

#include <iostream>
#include <string>
#include <sstream>

int main(void)
{
  std::string response;

  int value;
  while(1)
  {
    std::cout << "Enter an integer number: ";
    std::cin >> response;
    // convert to integer using a stringstream
    std::istringstream convert(response);
    
    if ( convert >> value )
    {
      break;
    }              
  }
  std::cout << "value = " << value << std::endl;
  
  std::cin.sync();  // purge any \n
  std::cin.get();   // console wait  
  return 0;
}

C++ sure is cumbersome!

This question has already been answered. Start a new discussion instead.