C++ Console Application only partially working on other computer?

Pages: 12
Mar 18, 2013 at 4:19pm
closed account (3qX21hU5)
Made changes to it and have tested on multiple computers and it works fine. Here is what I changed.


1) Added #include <limits>


1
2
3
4
#include <iostream>
#include <vector>
#include <limits>
using namespace std;



2) Line 53 int c was never used so deleted it.


3) Line 54 changed int numInsCos = 0; to unsigned numInsCos = 0; to get rid of warning that you are comparing unsigned variables to signed variables. It won't affect the program since I don't think you are going to be using a number less then 0 for numInsCos, but if you are I would leave it as signed.

4) added

1
2
std::cout << "Press ENTER to exit...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );


To the very botton right before it closes at the return 0; statement. This statement will make sure that the window stays open even if their are multiple things in the buffer waiting.

Hope that helps a bit and hope it works on all your computers.
Last edited on Mar 18, 2013 at 4:21pm
Mar 18, 2013 at 4:27pm
If that still isn't pausing, try this:

1
2
3
std::cout << "Press ENTER to exit...";
std::cin.sync();
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );


Sometimes you may have extra newlines hanging out in the input stream depending on how you've used cin earlier. If that is the case, ignore is going to go ahead and grab that newline and won't give you a chance to press enter.

http://cplusplus.com/reference/istream/istream/sync/

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <limits>

int main(int argc, char* argv[])
{
    int number;
    std::cout << "Enter a number: ";
    std::cin >> number;
    std::cout << "Press ENTER to exit...";
    std::cin.sync();  //try commenting out this line and see what happens!
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
    
    return 0;
}
Last edited on Mar 18, 2013 at 11:59pm
Mar 18, 2013 at 4:43pm
Guys,

Thanks! It finally works. I had to add the sync line suggested by booradley60, but it works now. I have a few final touches to make (like I want to display 2 for the date as "Febraury" rather than "2"), but it's almost there.

Thanks again for the help.

Kevin
Topic archived. No new replies allowed.
Pages: 12