Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // TobbeLekerCPlusPlus.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream> // Varför? Vet ej.
- #include <time.h>
- using namespace std; // Orka skriva std hela tiden :P
- // Funktion för att returnera vad ett tärningskast gav.
- int ThrowDice() {
- return (rand() % 6) + 1;
- // Modulus 6 för att få ett tal mellan 0 - 5. + 1 för att öka detta till 1 - 6 som vi vill ha.
- }
- // Funktion för att ÄNDRA EN VARIABEL SOM PASSAS IN I FUNKTIONEN vad ett tärningskast gav.
- void ThrowDiceWithVar(int &var) {
- var = (rand() % 6) + 1;
- }
- // Funktion för att ÄNDRA EN VARIABEL SOM PASSAS IN I FUNKTIONEN vad ett tärningskast gav med en pekare !
- void ThrowDiceWithPointer(int* p) {
- *p = (rand() % 6) + 1;
- }
- int main()
- {
- // Vi måste skapa en slumpgenerator, och vad den ska vara relativ till.
- // I de flesta fall räcker det med tid (därav time.h). På något sätt så kopplas rand()-funktionen till srand (?)
- srand(time(0));
- cout << "\n\nMy first C++ program! Rolling dice with different methods. // Tobbe\n\n" << endl;
- cout << "\n\n------------------------------ Method: returnvalue from function ------------------------\n\n" << endl;
- // Loop för antal tärningskast med returnvärde som metod.
- for (int i = 0; i < 10; i++) {
- // 10st kast.
- // Skriv ut. Tärningsvärdet returneras och skrivs ut direkt.
- cout << "\rThrowing dice number from function with returnvalue, dice number #" << (i + 1) << ": (" << ThrowDice() << ")" << endl;
- }
- cout << "\n\n------------------------------ Next method: reference ------------------------\n\n" << endl;
- int x;
- // Loop för antal tärningskast med manupilering av variabel som metod.
- for (int i = 0; i < 10; i++) {
- // 10st kast.
- // Manupilera x genom funktionen.
- ThrowDiceWithVar(x);
- // Skriv ut.
- cout << "\rThrowing dice number from function with reference, dice number #" << (i + 1) << ": (" << x << ")" << endl;
- }
- cout << "\n\n------------------------------ Next method: pointer ------------------------\n\n" << endl;
- // Loop för antal tärningskast med manupilering av variabel som metod.
- for (int i = 0; i < 10; i++) {
- // 10st kast.
- // Manupilera x genom funktionen.
- ThrowDiceWithPointer(&x);
- // Skriv ut.
- cout << "\rThrowing dice number from function with pointer, dice number #" << (i + 1) << ": (" << x << ")" << endl;
- }
- // Bara för att stanna upp programmet.
- int a;
- cin >> a;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement