I have this program. It is an example from a book. I am using Dev-C++ and my program just runs and close and I cannot see results. I have two questions

1 - Is the try/catch block ok, becuase it doesn t seem to work sometimes.
2 - How can I get to make my program pause without using system('pause"). I have use cin.get(); as you can see but it seems to work only on some type of programs.

Anyway I am a C++ beginner so anyhelp would be greatly appreciated

GCard

#include <fstream>
#include <stdexcept>
#include <iostream>
using namespace std;  
struct division
       {
       float dividend;
       float divisor;
       float answer;
       };
int main () 
    {
    division localdivide;
    try
       {
       cout << "Please enter a number. \n";
       cin >> localdivide.dividend;
       cout << "Please enter another number.\n";
       cin >> localdivide.divisor;
       localdivide.answer = localdivide.dividend/localdivide.divisor;
       cout << localdivide.dividend << "divided by " ;
       cout << localdivide.divisor << " is " << localdivide.answer;
       cout << "Press enter to continue";
       cin.get();
       }// end of the try blcok     
    catch(...)
            {
            cout << "an error occurred!";
            }
    return 0;
    }

Recommended Answers

All 5 Replies

add cin.ignore() just before the return statement to make it stop until you press a key.

add cin.ignore() just before the return statement to make it stop until you press a key.

It does not work.

Link.

Thank you, this did the trick. What is te difference on using
cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n') ;

to use
cin.ignore();

istream.ignore()

The first does what you want, accepting whatever the user types until he (hopefully) presses ENTER (or the input ends, whichever comes first).

The second just waits for a single character, which may or may not be the newline.\

Hope this helps.

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.