iccaros Posted April 3, 2005 Report Share Posted April 3, 2005 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 requiredint 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 Quote Link to post Share on other sites
iccaros Posted April 3, 2005 Author Report Share Posted April 3, 2005 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.. Quote Link to post Share on other sites
murtu52 Posted April 3, 2005 Report Share Posted April 3, 2005 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.... Quote Link to post Share on other sites
iccaros Posted April 3, 2005 Author Report Share Posted April 3, 2005 (edited) 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.. Edited April 3, 2005 by iccaros Quote Link to post Share on other sites
murtu52 Posted April 3, 2005 Report Share Posted April 3, 2005 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 Quote Link to post Share on other sites
jcl Posted April 4, 2005 Report Share Posted April 4, 2005 (edited) 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 April 4, 2005 by jcl Quote Link to post Share on other sites
iccaros Posted April 4, 2005 Author Report Share Posted April 4, 2005 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.. Quote Link to post Share on other sites
iccaros Posted April 4, 2005 Author Report Share Posted April 4, 2005 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 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.. Quote Link to post Share on other sites
jcl Posted April 4, 2005 Report Share Posted April 4, 2005 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 Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.