Printing out an array for a dice roller
Mar 15, 2019 at 9:22pm UTC
Hello, I am having trouble coding this program that requires selecting a certain function from a menu and then entering the amount of die you want to roll.
In this code if it were done correctly the output should be similar to this:
d4
6
('Your Die Results Are: ',"[3,1,1,1,1,3])
Would You Like to Continue? Y
System Clears
I am only concerned with this portion of the code not the rest.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
using namespace std;
int arrayOne[2]={1,2};
int arrayOneF();
int diceChoice, choice;
string continueC;
int main(){
do
{
cout<<"#####################" <<endl;
cout<<"Dice Roller" <<endl;
cout<<"#####################" <<endl;
cout<<"Select a Die to Roll" <<endl;
cout<<"Once Selected, choose how many to roll." <<endl;
cout<<"1 d2" <<endl;
cout<<"2. d4" <<endl;
cout<<"3. d6" <<endl;
cout<<"4. d8" <<endl;
cout<<"5. d10" <<endl;
cout<<"6. d12" <<endl;
cout<<"7. d20" <<endl;
cout<<"8. d100" <<endl;
cin>>choice;
switch (choice){
case 1:arrayOneF();
break ;
}
}while (continueC == "Y" || continueC == "y" );
}
int arrayOneF(){
int x;
cout<<"How Many Dice should be rolled?" ;
cin>>diceChoice;
for (int x = 0; x<2; x++){
arrayOne[2];
}
for (int x = 0; x<2; x++){
x=(rand()%diceChoice)+1;
if (diceChoice < rand() % 100){
cout<<"('Your Die Results Are: '," <<arrayOne[x]<<")" <<endl;
cout<<"Would You Like to Continue?" <<endl;
cin>>continueC;
}
if (continueC == "Y" || continueC == "y" ){
system("cls" );
}
}
}
I apologize for the inconsistent layout of the function.
Last edited on Mar 15, 2019 at 9:22pm UTC
Mar 15, 2019 at 9:24pm UTC
I forgot to mention that I did not properly code the function itself, as when the array is outputted, only one number is displayed.
And I believe System("CLS") is not working properly as well.
Mar 30, 2019 at 10:24pm UTC
Have you even compiled this?
Function don't have a return...
You should include string...
"main" function should return "0" as number...
Don't use "system", at least people said to me that this makes machine vulnerable and its hated by anti-virus...
I had to remake the basics of your code, here is a functioning dice program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <vector>
using namespace std;
int diceChoice, choice;
vector<int > arrayOne;
string continueC;
void arrayOneF(int Dice)
{
cout << "How Many Dice should be rolled?" ;
cin >> diceChoice;
arrayOne.resize(diceChoice);
cout << "Value:\n" ;
for (int n = 0; n < diceChoice; ++n)
{
int Value = (rand() % Dice) + 1;
arrayOne[n] = Value; // You don't need if you don't want to register, i mean, if you only want the cout
cout << Value << endl;
}
for (int n = 0; n < diceChoice; ++n)
{
cout << "ArrayOne: " << arrayOne[n] << endl;
}
}
int main() {
do
{
cout << "#####################" << endl;
cout << "Dice Roller" << endl;
cout << "#####################" << endl;
cout << "Select a Die to Roll" << endl;
cout << "Once Selected, choose how many to roll." << endl;
cout << "1 d2" << endl;
cout << "2. d4" << endl;
cout << "3. d6" << endl;
cout << "4. d8" << endl;
cout << "5. d10" << endl;
cout << "6. d12" << endl;
cout << "7. d20" << endl;
cout << "8. d100" << endl;
cin >> choice;
switch (choice)
{
case 1:arrayOneF(2);
break ;
case 2:arrayOneF(4);
break ;
case 3:arrayOneF(6);
break ;
case 4:arrayOneF(8);
break ;
case 5:arrayOneF(10);
break ;
case 6:arrayOneF(12);
break ;
case 7:arrayOneF(20);
break ;
case 8:arrayOneF(100);
break ;
}
cout << "Would You Like to Continue?" << endl;
cin >> continueC;
}
while (continueC == "Y" || continueC == "y" );
return 0;
}
Last edited on Mar 30, 2019 at 10:45pm UTC
Mar 31, 2019 at 2:39am UTC
system is fine for students. People worry too much about it -- its a non issue until you are writing professional software.
Mar 31, 2019 at 1:06pm UTC
Ty, nice to know, i'm new on c++ too...
Topic archived. No new replies allowed.