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

Recommended Answers

All 3 Replies

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.