C++ question

  • Two Factor Authentication is now available on BeyondUnreal Forums. To configure it, visit your Profile and look for the "Two Step Verification" option on the left side. We can send codes via email (may be slower) or you can set up any TOTP Authenticator app on your phone (Authy, Google Authenticator, etc) to deliver codes. It is highly recommended that you configure this to keep your account safe.

GoAt

Never wrong
Nov 3, 2001
1,444
10
38
41
USA
Visit site
now, the (refined and optimized) code works on my laptop but on the PC i get this ****ty
'cout' : undeclared identifier
'cin' : undeclared identifier

i dont have the source code from the laptop but im fairly certain its correct.

// crap.cpp : main project file.

#include <iostream>
#include <stdafx.h>


using namespace System;

int main()
{

int num, num2;

//ZOMG LOOP!
num = 1;
cout << "Enter a number bitch!";
cin >> num2;

while (num <= num2)
cout << "Fail " num;
num++;


return 0;
}

i know about the braces needed for the loop, and the for loops and how that can further optimize.

Do not correct the code! as i am just learning, i want to figure out the syntax/logic errors myself and will learn nothing if done for me.

i just want this cout error crap fixed.


(using visual C++ 2005)
 

GoAt

Never wrong
Nov 3, 2001
1,444
10
38
41
USA
Visit site
i have the laptop now (at work so i dont have the PC) and i see this change


using namespace std;

Now on the laptop im using Visual C++ 2008
so i dunno if that has anything to do with it.

now i put the code into the 2008 compiler and it works. (after a few corrections)

// crap.cpp : main project file.

#include <stdafx.h>
#include <iostream>


using namespace std;

int main()
{

int num, num2;

//ZOMG LOOP!
num = 1;
cout << "Enter a number, bitch!"; //happy Q that i added the comma?
cin >> num2;

while (num <= num2){
cout << "Fail " << num << endl;
num++;}


return 0;
}
 

GoAt

Never wrong
Nov 3, 2001
1,444
10
38
41
USA
Visit site
this leads me to my next question

while (num <= num2){
cout << "Fail " << num << endl;
num++;}


without the braces, it goes into an infinite loop. but with the braces, it does what it is supposed to.

why?
 

QUALTHWAR

Baitshop opening soon.
Apr 9, 2000
6,432
71
48
Nali City, Florida
web.tampabay.rr.com
I took C++ back in 2003 or so. I still have my book, but I’m too lasy to stand up to get it.

So this is a guess based on my other programming experience: A while loop can have multiple conditions within in. Therefore, brackets maybe required to let the loop know what conditions are associated with the while loop. It basically tells the loop what all it should consider as “contained within it.”
 

sharpfish

never really gone away
Apr 10, 2002
53
0
0
U.K
www.realityfakers.com
Without the braces you are basically doing this:

--------
while (num <= num2)
cout << "Fail " << num << endl;
--------

so if num == 1 and num2 was higher then this condition is ALWAYS true from then on without anyway of escaping out (you won't increase the num variable as it's simply not called due to the lack of the bracing. This will just call:

cout << "Fail " << num << endl;

endlessly.

The Braces in place ensure than after the ';' of endl; the next statement is considered before looping the while again, in this case 'num++;' which increases the number and thus breaks out of the loop (num no longer is smaller or equal to num 2). If you put 'X' in as num2 then it should print the fail message 'X' times then exit the loop if it's working ok.
 

Continuum

Lobotomistician
Jul 24, 2005
1,305
0
0
43
Boise
this leads me to my next question

while (num <= num2){
cout << "Fail " << num << endl;
num++;}


without the braces, it goes into an infinite loop. but with the braces, it does what it is supposed to.

why?


On the namespace thing you had using System in the origional so you would need to use namespace::some_class to access anything outside of the System namespace.

For the loop c++ and most other languages allow ommiting braces for a single line only so basically num++ was never executed since it was on the 2nd line.