FRIHOST • FORUMS • SEARCH • FAQ • TOS • BLOGS • COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


App crash c++





Denvis
I'm trying to convert a binary number into a decimal number however whenever i type in anything, the app crashes. what's wrong?! Confused

Code:
#include <iostream>
#include <cmath>

using namespace std;

int main(){
   string BinaryNum;
    int OriginalNum = 1;
    int DecOutput = 0;
   
    cout << "Type in a binary number" << endl;
    cin >> BinaryNum;
    int NumSize = BinaryNum.size();
   
        for (int i=NumSize; NumSize>0; i++){ //starting from the back
            if (BinaryNum[i] == 1){
               DecOutput = DecOutput + OriginalNum; //adding only if it's true
            }
            OriginalNum = OriginalNum * 2; // multiples of 2
        }
    cout << DecOutput;
system("PAUSE");
return 0;
}
fladry
First of all, look at the line

for (int i=NumSize; NumSize>0; i++){ //starting from the back

you are initializing i and saying it to loop until NumSize is greater than 0. In your code, NumSize is not modified inside the loop. So, it is always greater than 0. So, it enters infinite loop. And secondly, you are increasing the value of i(i++). In reverse loop, you should decrease it.

And, the other problem is that you are mistakenly comparing a character value with an integer in the line

if (BinaryNum[i] == 1){

the line should be

if (BinaryNum[i] == '1'){

Plus, your above loop needs to be modified a little to gain desired string array accessing. Anyway, the code that works is

Code:

#include <iostream>
#include <cmath>

using namespace std;

int main(){
   string BinaryNum;
   int OriginalNum = 1;
   int DecOutput = 0;   
   cout << "Type in a binary number ";
   cin >> BinaryNum;
   int NumSize = BinaryNum.size();

   for (int i=(NumSize-1); i>=0; i--){ //starting from the back
      if (BinaryNum[i] == '1'){
         DecOutput = DecOutput + OriginalNum; //adding only if it's true
      }
      OriginalNum = OriginalNum * 2; // multiples of 2
   }
   cout << DecOutput;
   system("PAUSE");
   return 0;
}

rickylau
One more point, I would wonder if you can use string without including <string>, that might be another reason for the app crash

also, i would use bitwise operator, at least a space for one integer variable is saved and it may slightly faster
Code:
   for (int i=0; i<NumSize; i++){
      DecOutput <<= 1; // Shift the DecOuput left for 1 bit
      if (BinaryNum[i] == '1'){
         DecOutput++ // Add one if that bit is 1
      }
   }
Peterssidan
rickylau wrote:
One more point, I would wonder if you can use string without including <string>, that might be another reason for the app crash

If you need to #include <string> it will most likely generate a compile time error and not crash during runtime. The reason why he don't need to include string here is probably because the iostream header already includes string because it uses string a lot.
rickylau
Peterssidan wrote:
rickylau wrote:
One more point, I would wonder if you can use string without including <string>, that might be another reason for the app crash

If you need to #include <string> it will most likely generate a compile time error and not crash during runtime. The reason why he don't need to include string here is probably because the iostream header already includes string because it uses string a lot.
I don't agree... In fact before I added #include <string> in the code the VC++ told me that there was problem with cin and string, and the problem has just been solved after that line of code was present. [Maybe compiler other than VC++ would have string included in iostream? I'm not sure however at least VC++ isn't that case]

In my previous semester's C++ course, the lecturer told me that string is not standard type of C++, therefore, including string standard library is necessary, or else you might only able to use char[] instead of string.
Peterssidan
I have more experience with GCC but of course other compilers can be different. Best (most cross platform) is to always include string whenever you use it. I guess I forget to do so very often.
snowboardalliance
I think most of the errors are already addressed by others.

rickylau wrote:
Peterssidan wrote:
rickylau wrote:
One more point, I would wonder if you can use string without including <string>, that might be another reason for the app crash

If you need to #include <string> it will most likely generate a compile time error and not crash during runtime. The reason why he don't need to include string here is probably because the iostream header already includes string because it uses string a lot.
I don't agree... In fact before I added #include <string> in the code the VC++ told me that there was problem with cin and string, and the problem has just been solved after that line of code was present. [Maybe compiler other than VC++ would have string included in iostream? I'm not sure however at least VC++ isn't that case]

In my previous semester's C++ course, the lecturer told me that string is not standard type of C++, therefore, including string standard library is necessary, or else you might only able to use char[] instead of string.


I'm pretty sure he's right that it SHOULD be a compiler error (string is an undefined type so the compiler will think it is a variable name missing a type or something).

BUT, I think VC++ is pretty well known for not following standards and it may do something funky.

I'd say it's good practice to include string when using it, but I don't see why it would be a runtime error
Related topics
One Liners:: Funny and Somewhat Inspiring
Heu...
Wirusy, ktσre nie daja mi spokoju :(
Robust thread monitoring
Java, python, or c++?
Good Cross Platform Programming Language
10 Reasons why PC's crash
mobile programmers codes
Java vs. C++
[C] Splitting names program
WinXP to Linux Instead of Windows 7
Reinstall windows easily?
[Solved]C: Drive Free Space Problem
Funny C variable
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.