Error

M

MZaza

That's a calculator and there is an option which should convert the
result you got to binary, but when I try to do so I get a Windows
error stating that the applications has encountered an error..

#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
int c, b, i;
double x, y;
double r;
char o;

cout <<"Enter the mathmatical operation, for example 1+2:" <<endl;
cin >>x >>o >>y;

switch (o)
{
case '+': r=x+y;
cout <<"The result is: " <<r <<endl;
break;

case '-': r=x-y;
cout <<"The result is: " <<r <<endl;
break;

case '*': r=x*y;
cout <<"The result is: " <<r <<endl;
break;

case '/': r=x/y;
cout <<"The result is: " <<r <<endl;
break;

case '^': r=pow(x, y);
cout <<"The result is: " <<r <<endl;
break;

default: cout <<"Error: wrong input" <<endl;
}

while (true)

{
cout <<endl <<"-Press 1 to use the result in another
mathmatical operation" <<endl <<"-Press 2 to clear the following
result and continue using the calculator" <<endl <<"-Press 3 to go to
the extra menu" <<endl <<"-Press 4 to exit" <<endl;
cin >>c;
if (c==1)
{
cout <<"Enter the mathamtical operation, for
example +1:" <<endl;
cin >>o >>y;
switch (o)
{
case '+': x=r+y;
cout <<"The result is: " <<x <<endl;
r=x;
break;

case '-': x=r-y;
cout <<"The result is: " <<x <<endl;
r=x;
break;

case '*': x=r*y;
cout <<"The result is: " <<x <<endl;
r=x;
break;

case '/': x=r/y;
cout <<"The result is: " <<x <<endl;
r=x;
break;

case '^': x=pow(r, y);
cout <<"The result is: " <<x <<endl;
r=x;
break;

default: cout <<"Error: wrong input" <<endl;
}
}
else if (c==2)
{
r==0;
cout <<"Enter the mathmatical operation, for example
1+2:" <<endl;
cin >>x >>o >>y;

switch (o)
{
case '+': r=x+y;
cout <<"The result is: " <<r <<endl;
break;

case '-': r=x-y;
cout <<"The result is: " <<r <<endl;
break;

case '*': r=x*y;
cout <<"The result is: " <<r <<endl;
break;

case '/': r=x/y;
cout <<"The result is: " <<r <<endl;
break;

case '^': r=pow(x, y);
cout <<"The result is: " <<r <<endl;
break;

default: cout <<"Error: wrong input" <<endl;
}
}
else if (c==3)
{
cout <<endl <<"-Press 1 to convert the result to
binary" <<endl <<"-Press 2 to convert the result to octal" <<endl <<"-
Press 3 to convert the result to hexa" <<endl <<"-Press 4 to go back"
<<endl;
cin >>c;
if (c==1)
{
b==r;
int m[100];
while (b!=0)
{
m=b%2;
i++;
b=b/2;
}
for (int v=i; v>=0; v--)
cout << m[v];
}

else if (c==2)
{
b==r;
int m[50];
while (b!=0)
{
m=b%8;
i++;
b=b/2;
}
for (int v=i; v>=0; v--)
cout << m[v];
}

else if (c==3)
{
b==r;
int m[50];
while (b!=0)
{
m=b%16;
i++;
b=b/2;
}
for (int v=i; v>=0; v--)
cout << m[v];
}

else if (c==4)
{
}
}
else if (c==4)
std::exit(EXIT_FAILURE);
}


system("PAUSE");
return EXIT_SUCCESS;
}
 
T

Thomas J. Gritzan

MZaza said:
That's a calculator and there is an option which should convert the
result you got to binary, but when I try to do so I get a Windows
error stating that the applications has encountered an error..

#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
int c, b, i;
double x, y;
double r;
char o;

cout <<"Enter the mathmatical operation, for example 1+2:" <<endl;
cin >>x >>o >>y; [...snip...]
else if (c==3)
{
cout <<endl <<"-Press 1 to convert the result to
binary" <<endl <<"-Press 2 to convert the result to octal" <<endl <<"-
Press 3 to convert the result to hexa" <<endl <<"-Press 4 to go back"
<<endl;
cin >>c;
if (c==1)
{
b==r;
^^
You should turn up your warning level, then your compiler will tell you
whats wrong with this line.
int m[100];
while (b!=0)
{
m=b%2;
i++;
b=b/2;
}


In this loop you use 'i' without initialization. Insert this the line
before the 'while':

int i = 0;

In C++ you declare variables just before you use them, and not all
variables on top of the function. That helps spotting such errors.

Also, it helps putting stuff in different functions. Maintaining a huge
main() will all code in it is a nightmare.
for (int v=i; v>=0; v--)
cout << m[v];
}
[...]
 
M

MZaza

MZaza said:
That's a calculator and there is an option which should convert the
result you got to binary, but when I try to do so I get a Windows
error stating that the applications has encountered an error..
#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int c, b, i;
double x, y;
double r;
char o;
cout <<"Enter the mathmatical operation, for example 1+2:" <<endl;
cin >>x >>o >>y; [...snip...]
else if (c==3)
{
cout <<endl <<"-Press 1 to convert the result to
binary" <<endl <<"-Press 2 to convert the result to octal" <<endl <<"-
Press 3 to convert the result to hexa" <<endl <<"-Press 4 to go back"
<<endl;
cin >>c;
if (c==1)
{
b==r;

^^
You should turn up your warning level, then your compiler will tell you
whats wrong with this line.
int m[100];
while (b!=0)
{
m=b%2;
i++;
b=b/2;
}


In this loop you use 'i' without initialization. Insert this the line
before the 'while':

int i = 0;

In C++ you declare variables just before you use them, and not all
variables on top of the function. That helps spotting such errors.

Also, it helps putting stuff in different functions. Maintaining a huge
main() will all code in it is a nightmare.
for (int v=i; v>=0; v--)
cout << m[v];
}

[...]

--


thanks
 
M

Micah Cowan

Thomas J. Gritzan said:
In this loop you use 'i' without initialization. Insert this the line
before the 'while':

int i = 0;

In C++ you declare variables just before you use them, and not all
variables on top of the function. That helps spotting such errors.

This, of course, is a matter of personal preference, though it's more
universal for variables like this one, that are used for iteration.
 
M

Micah Cowan

MZaza said:
cin >>c;
if (c==1)
{
b==r;
int m[100];
while (b!=0)
{
m=b%2;
i++;
b=b/2;
}
for (int v=i; v>=0; v--)
cout << m[v];
}

else if (c==2)
{
b==r;
int m[50];
while (b!=0)
{
m=b%8;
i++;
b=b/2;


This last line (and a similar one for the hexadecimal variant) has an
obvious error. I haven't looked through the rest of the code, but this
one happened to jump out at me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top