forums.evilmana.com

PSP Forums => Work In Progess => Topic started by: chi-kitory on March 08, 2010, 10:46:25 PM



Title: 10 minutes, No reference just code!
Post by: chi-kitory on March 08, 2010, 10:46:25 PM
Well im bored so going to just try and code something for 10 minutes and see if it works :P
Code:
#include <iostream>
int main()
{
    using namespace std;
    int minutes;            
    minutes = 10;          
    cout << “I have “;
    cout << minutes;        
    cout << “ minutes.”;
    cout << endl;
    minutes = minutes - 1; // modify the variable
    cout << “Now I have “ << minutes << “ minutes left to code” << endl;
    return 0;
}

Done :D
this is what it shows:
Quote
I have 10 minutes
Now i have 9 minutes left to code
Doesnt really do much but shows that i know how to do subtractions (not even a timer), but its my first c++ app away from book.
Tested:
Doesnt work i for got to put "using namespace std;", close though!:D but its 1:47Am so i'm half awake. haha


just cause im really bored im going to convert it too java :D

Code:
public class AddDate {
long minutes = 10;
int submin = -1;
System.out.println("i have" +minutes "minutes.");
System.out.println("Now i have" +minutes - submin+ "minutes");
}


Title: Re: 10 minutes, No reference just code!
Post by: PL3X on March 08, 2010, 11:26:54 PM
The entire standard template library is in the name space called "std". Therefore, if you wish to access any of the functions from iostream then you must sepcify that you are using the std namespace. This can be done one of three main ways.
First, declaring the following

using namespace std;

this says that in this scope, any function called from the standard template library will be linked to the std namespace. Additionally you can use the scope resolution operator, '::'.

Code:
#include <iostream>
int main()
{
    int minutes;           
    minutes = 10;           
    std::cout << “I have “;
    std::cout << minutes;       
    std::cout << “ minutes.”;
    std::cout << endl;
    minutes = minutes - 1; // modify the variable
    std::cout << “Now I have “ << minutes << “ minutes left to code” << endl;
    return 0;
}

Additionaly, the following will work:

#include "iostream"
using namespace std;
{
// snip
}

I think you need to look more into scope and namespace's.


Title: Re: 10 minutes, No reference just code!
Post by: chi-kitory on March 09, 2010, 04:17:36 AM
yep cause i have no clue about that yet. i bought a c++ book two days ago and been reading over the same 5 chapters to learn it so i wanted to see if i could remember what i was reading. Thanks for the information pl3x :D


Title: Re: 10 minutes, No reference just code!
Post by: yaustar on March 09, 2010, 10:35:34 AM
#include "iostream"
using namespace std;
{
// snip
}
This makes no sense. Can you post full code of this example please?


Title: Re: 10 minutes, No reference just code!
Post by: PL3X on March 09, 2010, 10:39:52 AM
Code:
#include <iostream>
using namespace std;
int main()
{
    using namespace std;
    int minutes;           
    minutes = 10;           
    cout << “I have “;
    cout << minutes;       
    cout << “ minutes.”;
    cout << endl;
    minutes = minutes - 1; // modify the variable
    cout << “Now I have “ << minutes << “ minutes left to code” << endl;
    return 0;
}


Title: Re: 10 minutes, No reference just code!
Post by: yaustar on March 10, 2010, 02:19:27 AM
You have it there twice, one in global scope and the other in scope of the function main(). Was that a typo?


Title: Re: 10 minutes, No reference just code!
Post by: chi-kitory on March 10, 2010, 05:25:31 AM
Plex how come you did use the :: this time?


Title: Re: 10 minutes, No reference just code!
Post by: yaustar on March 10, 2010, 06:49:17 AM
As in?
Code:
std::cout

As he said, it is the scope resolution operator. http://en.wikipedia.org/wiki/Scope_resolution_operator#C.2B.2B

Since he didn't declare what namespace was going to be used in that scope, he had to explicitly declare which namespace he was using every time he used an object/class/function/variable from that namespace.

Also worth pointing out that cout and cin are global variables in the std namespace.


Title: Re: 10 minutes, No reference just code!
Post by: PL3X on March 10, 2010, 11:13:36 PM
Sorry, it was only supposed to be in global scope:

Code:
#include <iostream>
using namespace std;
int main()
{
    int minutes;           
    minutes = 10;           
    cout << “I have “;
    cout << minutes;       
    cout << “ minutes.”;
    cout << endl;
    minutes = minutes - 1; // modify the variable
    cout << “Now I have “ << minutes << “ minutes left to code” << endl;
    return 0;
}