Sponsored By

Sign in to follow this  
iccaros

Simple Question On Int (c++)

Recommended Posts

ok I decided to really to learn how to program and not just use shells..

so here is my problem.. I add three integers together and I keep getting a negative number as answer?

anyone tell me why? I dothe same in a bash shell and get a positive number as I should. ...

I always enter 1 when asked so the answer should be 3 correct?

// this is a program for putting intagers together
#include <iostream>

using namespace std; // use this insted of a std stament for each type

// main function as required
int main()
{
       int integer1; // first integer variable
       int integer2; // next integer
       int integer3; // the third
       int sum;      // place holder for answer

       cout << "Enter first integer\n"; // our first prompt
       cin >> integer1;                // Read standard input aka keyboard

       cout << "Enter Second integer\n"; // our second promp for an integer
       cin >> integer2;                 // read standard input and direct it to integer2

       cout << "Enter Third and Final integer\n"; // finaly.. the last one
       cin >> integer2;                           // coments can be fun.. same as above read stdin

       sum = integer1 + integer2 + integer3; //time for the math, also notice that we put the answer in the integer sum
       cout << "Sum is " << sum << endl; // print sum

       return 0;// report a successfull compleation
} //the end foks

Share this post


Link to post
Share on other sites

never mind.. after I hit post I saw I was putting no input into integer3 as I had integer2 twice... so I had a garbage variable..

thanks anyways..

Share this post


Link to post
Share on other sites

well, just a small comment on C++ in general:

If you want to have the "Hit enter to continue" thing, for iostream i believe the code is "cin.get()"

lets say you want the person to hit enter to finish the program. put that in the place of return 0 and the person will have to hit enter to finish the program. Also, i got into the habit of after inputs, putting "cin.ignore();" which means, ignore the "enter" that is going to be pressed....

just wanted to share my knowledge....

Share this post


Link to post
Share on other sites

first thank you for the suggestions.. I will keep it in mind. I am finishing my degree in CIS but have found that in programing alot that I thought I knew .. I was wrong.

now one question on your suggestion, do I not need a return 0 or return exit_sucess to let the program know the function compleated correctly?

I'm not 100% new, but I'm going to a new level as I have been writing and editing for a while (mostly modifing others programs for my needs.. or all csh and bash stuff) . but I now see the need to write my own programs and so I'm starting over to really learn and not just have a working knowlage so any advice is helpfull (well ecept don't lern such and such learn this insted..as I know what languages I need for what I want to do..)

thansk agian.. :D

Edited by iccaros

Share this post


Link to post
Share on other sites

no, there is no need to have return 0 because basically you are doing that manually....return 0 = the enter you hit.

If you really want to test your knowledge (in the future, i don't think i or even you can do this now) and make a text RPG....that would be very cool B)

Share this post


Link to post
Share on other sites
now one question on your suggestion, do I not need a return 0 or return exit_sucess to let the program know the function compleated correctly?

Falling off the end of a function is equivalent to a return statement without a value and is only legal in functions with a return type of void (i.e. functions that have no return value). But as a special case falling off the end of main() is eqivalent to returning a value of zero.

Edited by jcl

Share this post


Link to post
Share on other sites
now one question on your suggestion, do I not need a return 0 or return exit_sucess to let the program know the function compleated correctly?

Falling off the end of a function is equivalent to a return statement without a value and is only legal in functions with a return type of void (i.e. functions that have no return value). But as a special case falling off the end of main() is eqivalent to returning a value of zero.

ok .. so if its main I can do with out return 0 (I don't think my teacher would approve of me removing it for assinments though) or a void function (and I have to declare them as void correct?)

all others require a return status..

Share this post


Link to post
Share on other sites
no, there is no need to have return 0 because basically you are doing that manually....return 0 = the enter you hit.

If you really want to test your knowledge (in the future, i don't think i or even you can do this now) and make a text RPG....that would be very cool B)

I have some text RPG I wrote in basic back in the C64 days, I just ported some to chipmunk basic as a test of the interpeter. after these classes are over I could problay port it to C or C++ with out much trouble..

Share this post


Link to post
Share on other sites
ok .. so if its main I can do with out return 0 (I don't think my teacher would approve of me removing it for assinments though) or a void function (and I have to declare them as void correct?)

all others require a return status..

Correct across the board. Including the bit about your teacher :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this