Current Events > Can anyone help me out with some basic C++?

Topic List
Page List: 1
longsticks
04/13/17 3:22:56 PM
#1:


I have the following program:

#include<iostream>
#include <cstdlib> // for rand() and srand()
#include <ctime> // for time()
using namespace std;

int main()
{
int rolls;
int resultArray[6] = { 0, 0, 0, 0, 0, 0 };
cout << "Welcome to the Roll Counter" << endl;
cout << "How many time would you like to roll the die? ";
cin >> rolls;

while (rolls < 1)
{
cout << "Please enter a number of 1 or larger ";
cin >> rolls;
}

srand(time(0));
int i = 0;
int x;

while (i <= rolls)
{

x = (rand() % 6 + 1);
resultArray[x] = resultArray[x] + 1;
i++;
}

cout << resultArray[0];
cout << resultArray[1];
cout << resultArray[2];
cout << resultArray[3];
cout << resultArray[4];
cout << resultArray[5];


return 0;

}

I'm just trying to get it to function properly so I'm not concerned with making it nice and neat yet. Anyway, I get the following error when I run it:
"Run-Time Check Failure #2 - Stack around the variable 'resultArray' was corrupted"
For some reason, it wont properly add more than once to each location in the array. What am I doing wrong?
---
... Copied to Clipboard!
meestermj
04/13/17 3:26:13 PM
#2:


When you initialized your array you put
ResultArray[6] = blah blah

Don't put that 6 there. Just put
resultArray[] = blah blah
---
Psn: beastlytoast
Left-handed fire-slapsies leave me feeling confused about life. - Merydia
... Copied to Clipboard!
Frostshock
04/13/17 3:28:12 PM
#3:


meestermj posted...
When you initialized your array you put
ResultArray[6] = blah blah

Don't put that 6 there. Just put
resultArray[] = blah blah


The 6 is fine but unnecessary.

Hint: the problem is on the line with the rand().
---
Got questions about schoolwork? Want to share answers, or discuss your studies? Come to Homework Helpers!
http://www.gamefaqs.com/boards/1060-homework-helpers
... Copied to Clipboard!
meestermj
04/13/17 3:32:11 PM
#4:


Frostshock posted...
meestermj posted...
When you initialized your array you put
ResultArray[6] = blah blah

Don't put that 6 there. Just put
resultArray[] = blah blah


The 6 is fine but unnecessary.

Hint: the problem is on the line with the rand().

Oh I was just helping him clean up his code a little. He also didn't seed the rand function.
---
Psn: beastlytoast
Left-handed fire-slapsies leave me feeling confused about life. - Merydia
... Copied to Clipboard!
longsticks
04/13/17 3:37:17 PM
#5:


meestermj posted...
Frostshock posted...
meestermj posted...
When you initialized your array you put
ResultArray[6] = blah blah

Don't put that 6 there. Just put
resultArray[] = blah blah


The 6 is fine but unnecessary.

Hint: the problem is on the line with the rand().

Oh I was just helping him clean up his code a little. He also didn't seed the rand function.


I seeded the srand though, I don't really understand how these function work yet, but thats how my professor did it in an example with a simple program. He left the 'rand()' empty
---
... Copied to Clipboard!
longsticks
04/13/17 3:37:34 PM
#6:


Frostshock posted...
meestermj posted...
When you initialized your array you put
ResultArray[6] = blah blah

Don't put that 6 there. Just put
resultArray[] = blah blah


The 6 is fine but unnecessary.

Hint: the problem is on the line with the rand().

Can I have another hint lol
---
... Copied to Clipboard!
longsticks
04/13/17 3:38:53 PM
#7:


Actually never mind, I think I got it!
---
... Copied to Clipboard!
Frostshock
04/13/17 3:38:54 PM
#8:


longsticks posted...
Frostshock posted...
meestermj posted...
When you initialized your array you put
ResultArray[6] = blah blah

Don't put that 6 there. Just put
resultArray[] = blah blah


The 6 is fine but unnecessary.

Hint: the problem is on the line with the rand().

Can I have another hint lol


If you aren't willing to do even a little bit of problem solving after I basically gave you the answer, you're doomed to fail.
---
Got questions about schoolwork? Want to share answers, or discuss your studies? Come to Homework Helpers!
http://www.gamefaqs.com/boards/1060-homework-helpers
... Copied to Clipboard!
DevsChum
04/13/17 3:55:27 PM
#9:


So when you ask a programming question online, it's really bad etiquitte to end the topic with a "never mind" or something similar. It doesn't make much of a difference on CE where the topic will be gone in a few days, but there cpuld always be someone else with your exact problem searching google, and there's nothing more soul-crushing than finding that exact problem but the solution is never given, especially when it's clear that the problem actually was resolved.

Even if it seems like a really dumb rookie mistake, make sure you mention what you did that fixed it. Nothing to be embarrassed about; you never stop making those dumb missing semicolon or = instead of == mistakes, no matter how experienced you get.
---
... Copied to Clipboard!
NintendoFanGirl
04/13/17 3:56:27 PM
#10:


don't be denver coder tc
... Copied to Clipboard!
Kekistan
04/13/17 4:03:26 PM
#11:


You shouldn't need to add 1 for rand() % 6, you need to produce results between 0 and 5.
... Copied to Clipboard!
Topic List
Page List: 1