-2

I have written a simple c++ program on visual studio to convert number from any base to decimal.

The program compiles but doesnt show output on the console.

Even after taking inputs through cin, consle is just open blinking but nothing happens thereafter

Source code:

#include <iostream>

using namespace std;

 void main()
{

    int Base = 3; int Number = 122;

    int j = 0;
    int dec = 0;  
    int num = Number;

    while (num >= 0)
    {
    dec += (num % 10)* (Base^j);

    num = num / 10;
    j++;

    }
    cout << "Decimal is" << dec;

}
Paul R
  • 208,748
  • 37
  • 389
  • 560
askatral
  • 133
  • 8
  • Unrelated: Did you really intend an XOR of `Base` and `j` ? And maybe flush your IO, not that it matters since your process is terminating anyway, but will if you put in the fugly `system("pause")` – WhozCraig Sep 18 '14 at 07:30

1 Answers1

1

You need to flush the output stream and also make the program pause so that your console window does not disappear:

char ch;
cout << "Decimal is" << dec << endl;
cout << "Hit any key to continue..." << endl;
cin >> ch;
Paul R
  • 208,748
  • 37
  • 389
  • 560
  • Do a _getch() instead on windows if you're keen on having this feature. cin would require you to enter a key and press the return key as well. – GlGuru Sep 18 '14 at 07:32
  • @GlGuru: sure, but it depends on whether you care about portability or not. In this case it doesn't really matter I expect, but writing portable code is a good habit to get into early in the learning process. – Paul R Sep 18 '14 at 07:34
  • You can just use the POSIX variant getch() if that's the case. You won't have to deal with warnings and as far as I am aware they are pretty much the same. – GlGuru Sep 18 '14 at 07:37
  • im not sure if im doing it right... i have included the lines above and the console wouldn't close, no matter how many charecters I hit – askatral Sep 18 '14 at 07:39
  • A further objection is that in general you shouldn't mix C and C++ I/O. Furthermore using C APIs in C++ code should in general be discouraged - otherwise people end up writing "C with classes" rather than proper C++. But again, it's probably not that relevant for a simple homework task such as this. – Paul R Sep 18 '14 at 07:39
  • @askatral: did you hit return? – Paul R Sep 18 '14 at 07:39
  • @PaulR: Yes..I did ctrl+F5 and it runs fine....logic doesn't seem to work right...so working on it – askatral Sep 18 '14 at 07:52
  • Maybe use `ch = getchar()` instead of `cin >> ch`, as suggested by @GlGuru? – Paul R Sep 18 '14 at 07:53
  • Im trying to input Base and Number using Cin....it takes the inputs but nothing happens after that...i mean the console just stays there and program doesnt execute any further – askatral Sep 18 '14 at 07:59
  • If you're stuck on something other than what's in the question above then it's probably time to start a new question describing your problem. – Paul R Sep 18 '14 at 08:04