forums.evilmana.com
Welcome, Guest. Please login or register.

Login with username, password and session length
September 10, 2010, 07:41:25 AM
.
News: 06/10/2009 - New Features For Forum.
Friendly URLs, twitter for profile, and video site URLs will auto-display the video.
Pages: [1]   Go Down
0 Members and 1 Guest are viewing this topic. Topic Tools  
Read March 08, 2010, 10:46:25 PM #0
chi-kitory

10 minutes, No reference just code!

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");
}
« Last Edit: March 08, 2010, 10:56:15 PM by chi-kitory »

Currently working on:
-Gundam Space adventure for PSP
Offline  
Read March 08, 2010, 11:26:54 PM #1
PL3X

Re: 10 minutes, No reference just code!

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.
Offline  
Read March 09, 2010, 04:17:36 AM #2
chi-kitory

Re: 10 minutes, No reference just code!

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


Currently working on:
-Gundam Space adventure for PSP
Offline  
Read March 09, 2010, 10:35:34 AM #3
yaustar

Re: 10 minutes, No reference just code!

#include "iostream"
using namespace std;
{
// snip
}
This makes no sense. Can you post full code of this example please?
Offline  
Read March 09, 2010, 10:39:52 AM #4
PL3X

Re: 10 minutes, No reference just code!

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;
}
Offline  
Read March 10, 2010, 02:19:27 AM #5
yaustar

Re: 10 minutes, No reference just code!

You have it there twice, one in global scope and the other in scope of the function main(). Was that a typo?
Offline  
Read March 10, 2010, 05:25:31 AM #6
chi-kitory

Re: 10 minutes, No reference just code!

Plex how come you did use the :: this time?


Currently working on:
-Gundam Space adventure for PSP
Offline  
Read March 10, 2010, 06:49:17 AM #7
yaustar

Re: 10 minutes, No reference just code!

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.
Offline  
Read March 10, 2010, 11:13:36 PM #8
PL3X

Re: 10 minutes, No reference just code!

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;
}
Offline  
Pages: [1]   Go Up
Jump to:  

Theme Update by Runic Warrior Originally created by m3talc0re