getline is crashing?

So I'm fairly new to coding in c++ and for some reason my program keeps crashing and I have no clue why. Yes, I looks know that there are many prettier ways to do what I did, but I don't see why this cant be executed. I've tried doing it in a loop and it wont work either. As a recap I'm pulling data from a file that has 4 lines in the beginning that tells a card name such as "of spades" or "of hearts". Then the nest 13 lines after are "one", "two", "jack", "king" etc. All I really need to know is how to get the lines from the file into the the 2 string arrays I have.Thanks

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
#include <iostream>
#include <cmath>
#include <ctime>
#include <fstream>
#include <string>
using namespace std;
int main()
{
	ifstream data1;
	data1.open("C:\\Users\\Scott\\Downloads\\cards.txt");
	string number[12], phrase[3];
	int x, y, i;
	getline(data1, phrase[0]);
	getline(data1, phrase[1]);
	getline(data1, phrase[2]);
	getline(data1, phrase[3]);
	getline(data1, number[0]);
	getline(data1, number[1]);
	getline(data1, number[2]);
	getline(data1, number[3]);
	getline(data1, number[4]);
	getline(data1, number[5]);
	getline(data1, number[6]);
	getline(data1, number[7]);
	getline(data1, number[8]);
	getline(data1, number[9]);
	getline(data1, number[10]);
	getline(data1, number[11]);
	getline(data1, number[12]);

	
	srand((unsigned int)time(NULL));
	x = rand() % 4;
	y = rand() % 13;
	for (i = 1; i < 6; i++)
	{
		x = rand() % 5;
		cout << x << endl;
		y = rand() % 14;
		cout << y << endl;
		cout << phrase[x];
		

	}
	return 0;
}
phrase[3]; valid indexes are 0-2

number[12] valid indexes are 0-11
I tried that and every time i run it it still crashes instantly. :/ I have to use getline and store them from the file.
closed account (E0p9LyTq)
It isn't getline() that is crashing, it is in your for loop.
I tried that and every time i run it it still crashes instantly.

Did you also fix lines 37 and 39?
They are also going to generate out of bounds references.

Here's a version of your program that by using constants avoids the problem of having to change multiple places in your program should the number of phrases or numbers change.

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
#include <fstream>
#include <string>
using namespace std;
const int MAX_PHRASES = 4;
const int MAX_NUMBER = 12;

int main()
{   ifstream data1;
	data1.open("C:\\Users\\Scott\\Downloads\\cards.txt");
	string number[MAX_NUMBER], phrase[MAX_PHRASE];
	int x, y, i;
	for (i=0; i<MAX_PHRASE; i++)
	    getline(data1, phrase[i]);
	for (i=0; i<MAX_NUMBER; i++)
	    getline(data1, number[i]);
	
	srand((unsigned int)time(NULL));
	for (i = 1; i < 6; i++)
	{    	x = rand() % MAX_PHRASES;
		cout << x << endl;
		y = rand() % MAX_NUMBER;
		cout << y << endl;
		cout << phrase[x];	
	}
	return 0;
}

Last edited on
closed account (E0p9LyTq)
Aren't there 13 cards in a suit? Ace (one) through 10 and 3 face cards (Jack, Queen and King). MAX_NUMBER should be 13?
AbstractionAnon, i went back and checked lines 37 and 39 and i didn't know what i was thinking. It works sometimes because it wouldn't get out of bounds references. I changed it back to 4 and 13 and it is stable now. Thank you so much! One last question. How would i get this to make sure that no hand is ever repeated? I have to make two hands of 5 cards. no cards in those 2 hands can be the same. how would i approach that?
I would populate an array of 52 cards sequentially, then use random_shuffle().
http://www.cplusplus.com/reference/algorithm/random_shuffle/
The only problem is that i cant make an array of 52. My professor wants us to make the two arrays like i have done and work from one data file. I have to work from that.
Topic archived. No new replies allowed.