Question [C++ Noob Question] Can someone check my code?

hedzup456

Semi-poetic moron
Joined
Aug 5, 2012
Messages
87
Reaction score
0
Points
0
Location
My LEO DGIV.
Website
mypoetryramblings.blogspot.co.uk
Hey all,
I've just started learning C++, and have made a program which has no issue when using
Code:
system("PAUSE");
. However, I have seen that system("pause") is frowned apon-security backdoors, ect.
So I changed from system("pause") to
Code:
std::cin.get();
, and it now does not work.
Could someone please look through my code in a spare minute and check I haven't made any blatant errors causing this? My debugger doesnt pick anything up, but then....

(If it makes a difference, I am running Windows Seven Home Premium x64, my editor/compiler/debugger is Dev-C++ 4.9.9.2)
Here is my code:
Code:
#include <iostream>
#include <cmath>
#include <limits>
#include <windows.h> //Allows 'sleep()'
//////////////////////////////////////////////////////////////////////////////////
//            TODO:                                                             //
//      re-set the counter in the Please Wait counter,                          //
//      after lowering its number to speed up testing.                          //
//////////////////////////////////////////////////////////////////////////////////
int main()
{
using namespace std;
int variable;
cout << "What number do you want to test?" << endl;
cin >> variable;
cout << "Testing " << variable << " to see if it is bigger than 3..." << endl;
Sleep(2000); // Delay timer
system("CLS");  // Clear Screen
int i;
for (i=1;i<2;i++) //Loop for the dots!
{
 cout << "Please Wait.";
 //First (one dot)
 Sleep(500);   
 //Pause for half a second
 system("CLS");
 //Clear screen
 cout << "Please Wait..";
 //Second (Two dots)
 Sleep(500);
 system("CLS");
 cout << "Please Wait...";
 //Third (three dots)
 Sleep(500);       
 system("CLS");
 // end of loop
}
system("CLS");
if (variable > 33) // If "variable" is less than 33
{
     cout << variable << " is bigger than three!" << endl; // DO THIS
     }
else if (variable % 2==0) //If "variable" is more than 33/if varibles modulo 2(remainder after dividiong by two) is zero, then do
{
     cout << "Woah! " << variable << " is not bigger than 33, but is EVEN! Woah. That is DEEP, man!" << endl; // THIS!
     }
else 
{
     cout << variable << " SUCKS! Its not even, its not more than thirty three... \nPick a better number next time!" << endl;
}

cout << "Press Enter to continue."; //'Press any key to continue'
std::cin.get();
return 0; 
    
}
Thanks in advance!
~Hedz
P.S. If I posted in the wrong area, please move and inform. Thanks!
 

Urwumpe

Not funny anymore
Addon Developer
Donator
Joined
Feb 6, 2008
Messages
37,588
Reaction score
2,312
Points
203
Location
Wolfsburg
Preferred Pronouns
Sire
Why do you use system() so often for handling your console outputs?

cin.get() does not wait for input, it returns the next character (as int) or a EOF symbol, if no input is available right now, it is the same as the old getchar() in C. What you might wanted to use is cin.get(char &c), which waits for the first character and returns it in c. Much better in your situation would be cin.ignore(1).
 

hedzup456

Semi-poetic moron
Joined
Aug 5, 2012
Messages
87
Reaction score
0
Points
0
Location
My LEO DGIV.
Website
mypoetryramblings.blogspot.co.uk
Why do you use system() so often for handling your console outputs?

cin.get() does not wait for input, it returns the next character (as int) or a EOF symbol, if no input is available right now, it is the same as the old getchar() in C. What you might wanted to use is cin.get(char &c), which waits for the first character and returns it in c. Much better in your situation would be cin.ignore(1).

Because I have found no other way to clear the console.
Also, will give this a go-thanks!
~Hedz
------------------------------------------EDIT------------------------------
Still doesn't work.. I complete one cycle of the for loop (starts ln20) then crashes. The number I am testing with is 44, if it makes a difference..
 
Last edited:

Urwumpe

Not funny anymore
Addon Developer
Donator
Joined
Feb 6, 2008
Messages
37,588
Reaction score
2,312
Points
203
Location
Wolfsburg
Preferred Pronouns
Sire
Because I have found no other way to clear the console.
Also, will give this a go-thanks!
~Hedz

Alternatives: Either PDCurses (which is what I prefer) or directly the Windows Console API.

---------- Post added at 11:33 PM ---------- Previous post was at 11:27 PM ----------

Your loop will only do one loop. you increment i to 2, and 2 is not less than 2, so the for loop exits. Three loops would be for(i = 0; i<3; i++)
 

hedzup456

Semi-poetic moron
Joined
Aug 5, 2012
Messages
87
Reaction score
0
Points
0
Location
My LEO DGIV.
Website
mypoetryramblings.blogspot.co.uk
Your loop will only do one loop. you increment i to 2, and 2 is not less than 2, so the for loop exits. Three loops would be for(i = 0; i<3; i++)

Well spotted-I thought it would give me two loops :L
Googleing PDCurses now :)
~Hedz

---------- Post added 11-19-12 at 10:19 AM ---------- Previous post was 11-18-12 at 10:38 PM ----------

Okay, my problem is solved.
Using
Code:
cin.get();
cin.get();
I was able to wait for a single keypress at the end of the window. (cin.ignore(1); did not work for me. At all.)
~Hedz
 
Top