Saturday, May 22, 2010

Need help with C++. Program closing on a simplish program.?

Hey, I'm learning to prgram in C++ with the help of a book I bought. However, for some reason all the exercises in the book do not have cin.get() or system("PAUSE") and do not mention anything about using these to stop the program from closing (very frustrating!)





Here is my program that has been copied straight from the book and is closing after I enter a number. ( Or after I press enter after I have entered the number in the case that I have put a cin.get() in)





#include %26lt;iostream%26gt;


using namespace std;





int main( void )


{





int number;





cout %26lt;%26lt; "Enter a number" %26lt;%26lt;endl;


cin%26gt;%26gt; number;


if (number %26gt;= 1 %26amp;%26amp; number %26lt;= 100 %26amp;%26amp; (number % 3 == 0))


{


number = number * 2;


}


else if (number %26gt;= 1 %26amp;%26amp; number %26lt;= 100


%26amp;%26amp; !(number % 3 == 0))


{


number = number * 3;


}


else if (!(number %26gt;= 1 %26amp;%26amp; number %26lt;= 100))


{


number = number * number % 3;


}





cin.get();


cin.get();


return 0;


}





Help would be great thanks.





Doug

Need help with C++. Program closing on a simplish program.?
IMO, the reason why the exercises don't have a system("pause") is because not all C++ editors/IDE need them. Dev C++ is the only IDE I know that closes after program execution.





In your program, I don't think that you need the cin.get(); lines, just 'system("pause")' before the 'return 0;'. You may also want to display your programs result, just add a 'cout %26lt;%26lt; number;'.
Reply:Hey thanks... the cout%26lt;%26lt;number was all I needed. I must have forgot that.





I'm using the beta version of Microsoft Visual Studio 2008. Report It



No comments:

Post a Comment