I Wrote My First Cool C++ Program Today

bkaron

Yep, That's Me!
The reasion why I say "My first cool Program" is because I don't think "Hello World" is a cool program. :rolleyes: a Calculator program on the other hand is awesome! :D But I didn't have enough time in class to ask what was wrong with the program. :( I know it's somthing small, it even starts up fine but when I put in somthing like 5+5 it will give me "1.38168e-41Enter the problem:" :mad: Could you guys help me out a little, I am completly stumped :confused: ::sleepy:: :( :eek:
 
Oh yea, you need the program don't ya

#include <iostream>
using namespace std;

void addition ()
{
char oper;
float x, y, ans;
cout << "Enter the problem: ";
cin >> x;
cin >> y;
cin >> oper;
if(oper == '+')
ans = x+y;
cout << ans;
}

void sub ()
{
char oper;
float x, y, ans;
cout << "Enter the problem: ";
cin >> x;
cin >> y;
cin >> oper;
if(oper == '-')
ans = x-y;
cout << ans;
}

void mult ()
{
char oper;
float x, y, ans;
cout << "Enter the problem: ";
cin >> x;
cin >> y;
cin >> oper;
if(oper == '*')
ans = x*y;
cout << ans;
}

void divd ()
{
char oper;
float x, y, ans;
cout << "Enter the problem: ";
cin >> x;
cin >> y;
cin >> oper;
if(oper == '/')
ans = x/y;
cout << ans;
}

int main ()
{addition ();
sub ();
mult ();
divd ();
}
 
i'm newbie...

if i install Xcode... can i do C, C++ or JAVA on my ibook?

cause i heard that Xcode is used to debug OSX .3.. i could be wrong
 
ok, here is where you are going wrong.

First you are calling each function in sequential order (so the add funtion is done first, then the sub function, etc....) , so if the user's first input is 2 3 -, this is being handled by your add function, which is only checking for oper == '+'. Since oper isn't equal to the +, it is just outputting ans (which is a float), here is where your are getting your undefined behavior, since your are declaring: float ans, but you are not assigning it an initial value, so it is pulling some number out of the memory space where float ans resides. If you go back into your program, and set each float ans = 0 when you initialize them, and do a test where oper fails you will get a zero instead of the 1.38168e-41 (or some number similar). Not only that, but if you enter 5 + 5 at the command prompt, you are assigning float x == 5, float y == +, and char oper == 5. The way your have your inputs set up, you need to do the following: 5 5 + or 5 5 - or 5 5 * or 5 5 / (I overlooked this the first time I posted this information)

Not to sound picky, but it is a good habit to declare each variable you use on it's own line: (eg)

float x;
float y;
float ans;

not only that, but if you want to make sure you don't run into undefined behavior, it is always a good thing to initialize your variables with a default value:

float x = 0;
float y = 0;
float ans = 0;

the other thing you need to do is include an endl after your cout << ans.

cout << ans << endl;

which will output the next line in your code on the next line in the terminal.

I am including my example of your program, which takes the oper input from the user input, and passes it to the appropriate function depending on the value. The other minor thing I did was to check to make sure that the denominator isn't zero in the division function and setting it to 1 if it is.

#include <iostream>

using namespace std;

void add( float, float );
void sub( float, float );
void mult( float, float );
void divd( float, float );

int main()
{
cout << "Input two numbers and an operator ";
float a;
float b;
char oper;
cin >> a >> b >> oper;

switch( oper )
{
case '+':
add( a, b );
break;
case '-':
sub( a, b );
break;
case '*':
mult( a, b );
break;
case '/':
divd( a, b );
break;
default:
break;
}

return( 0 );
}

void add( float x, float y )
{
cout << "Answer: " << x + y << endl;
}

void sub( float x, float y )
{
cout << "Answer: " << x - y << endl;
}

void mult( float x, float y )
{
cout << "Answer: " << x * y << endl;
}

void divd( float x, float y )
{
if( y == 0 )
{
cout << "Can't divide by zero, setting denominator to 1" << endl;
y = 0;
}
cout << "Answer: " << x / y << endl;
}


The other thing that you'll notice is that I declare my functions with a function header call before main, and do all my functions them self afterwards. If you are just starting out, now is a good time to learn how to make your code look clean, it will help you out drastically in the long run.

Another good tip, if you use the terminal to write your programs, is to learn vim, it is an awesome editor, which will run circles around most others, after learning how to use it. The learning curve is a little steeper than using something like pico, but if/when you get into bigger more complex programs it will save you time in the long run. I'm including a link from my instructor's website on some of the commands for vim/vi (vim is an improved version of vi, and is now included with 10.3)

http://arioch.unomaha.edu/~jclark/vim.html

If you have any questions about what I've written or about vim, please feel free to contact me. (e-mail is in profile)

Good luck and happy coding
 
Man that's good code! :) I havn't seen clear code like that in a long time...thanks naodx! :)

My first program was a currency converter. :p
 
java will be on your machine regardless of whether or not your install the xcode tools, but to get the c/c++/objective-c complier (which is the gnu version 3.3) you need to install the xcode cd.
 
thanks trip, the copy and paste killed my indenting, but at least a person can get an idea as to where to start. (did I mention that good indenting is as important as anything else, when writing code?) :)

Thanks again.
 
what if I put operand a = 'm' and operand b = 'z', I believe it will compile fine, just get different numbers
 
mkwan,

I'm not sure what you are talking about there, please give more info. Are you talking about the division function????
 
trip,

my first program was some variant of hello, but it was using the old apple II basic back in junior high, if that tells you anything. :)

I did do the currency conversion program, but it wasn't until I hit college and it was in c++ (ok so I had a few years between high school and college, but at least I finally made it) :)
 
mkwan,

very true, but you usually don't do to much for error checking in a beginning level program. That comes later after you master the basics.
 
I would say, whenever you want to show off code like this, use the [ code ] [ /code ] tag in vB... put your code between these tags (without the spaces), and it will format correctly.
 
Like this: (btw, thanks for the code. You really know your stuff)

Code:
#include <iostream>
 
 using namespace std;
 
 void add( float, float );
 void sub( float, float );
 void mult( float, float );
 void divd( float, float );
 
 int main()
 {
     cout << "Input two numbers and an operator ";
     float a;
     float b;
     char oper;
     cin >> a >> b >> oper;
 
     switch( oper )
     {
         case '+':
             add( a, b );
             break;
         case '-':
             sub( a, b );
             break;
         case '*':
             mult( a, b );
             break;
         case '/':
             divd( a, b );
             break;
         default:
             break;
     }
 
     return( 0 );
 }
 
 void add( float x, float y )
 {
     cout << "Answer: " << x + y << endl;
 }
 
 void sub( float x, float y )
 {
     cout << "Answer: " << x - y << endl;
 }
 
 void mult( float x, float y )
 {
     cout << "Answer: " << x * y << endl;
 }
 
 void divd( float x, float y )
 {
     if( y == 0 )
     {
         cout << "Can't divide by zero, setting denominator to 1" << endl;
         y = 0;
     }
     cout << "Answer: " << x / y << endl;
 }
 
might want to change your cout to reflect the change to, something like:

cout << "Input an equation to solve, eg. ( 1 + 2 )" << endl;
 
Oh yea, you need the program don't ya

#include <iostream>
using namespace std;

void addition ()
{
char oper;
float x, y, ans;
cout << "Enter the problem: ";
cin >> x;
cin >> y;
cin >> oper;
if(oper == '+')
ans = x+y;
cout << ans;
}

void sub ()
{
char oper;
float x, y, ans;
cout << "Enter the problem: ";
cin >> x;
cin >> y;
cin >> oper;
if(oper == '-')
ans = x-y;
cout << ans;
}

void mult ()
{
char oper;
float x, y, ans;
cout << "Enter the problem: ";
cin >> x;
cin >> y;
cin >> oper;
if(oper == '*')
ans = x*y;
cout << ans;
}

void divd ()
{
char oper;
float x, y, ans;
cout << "Enter the problem: ";
cin >> x;
cin >> y;
cin >> oper;
if(oper == '/')
ans = x/y;
cout << ans;
}

int main ()
{addition ();
sub ();
mult ();
divd ();
}







_______________________________________________________________

I had problems when I ran your code. I fixed it by giving your variables a value of 0 when it is being declared. The computer automatically assignes each variable some number so the calculation was wrong. also add this to the end of your code

system ("pause");
result 0;
 
31565d1270489092-little-known-facts-regarding-world-war-2-holy-thread-resurrection.jpg
 
Back
Top