ifstream.getline to a string

ok im going to post the full code as some of the code wont make since without the globals and yes there is a reason for making them globals i think the problem i am having is the string will only pick up the first word and store it to the string i need it to read until the comma and save all of it to the variable. function Clist at the bottom of the code
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
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
ifstream fIn;
ofstream fOut;
int menu();
string Clist ();
int menuOpt;

int main()
{
menu();
}
int menu()
{
char inp;

do
{
cout << "Please chose from the following options" <<endl;
cout << "A: Load Trivia type." <<endl;
cout << "B: Start Game." <<endl;
cout << "C: Exit Game" <<endl;
cin >> inp;
	

switch(inp)
{
case 'a':
	Clist();
	break;
case 'b': 
	cout << "test2" << endl;
	break;
case 'c':
	return 0;
	break;
case 'A':
	Clist();
	cout << "test" << endl;
	break;
case 'B': 
	cout << "test2" << endl;
	break;
case 'C':
	return 0;
	break;
default:
	cout << "Error that is not an option\n Press enter to continue " << endl;
	cin.get();
	cin.get();
	system("cls");
	break;
}}while(inp != 'c');
}


string Clist()
{
	string triviaName;
	fIn.open("list.csv");
	fIn >> triviaName;
	cout << triviaName;
	return triviaName;
	
}


Last edited on
closed account (S6k9GNh0)
1. You don't call getline();.
2. myFstream.getline(pChar, 256, ','); //This should do it.
Avoid the use of char* if possible. Use the <string> header's getline() over the istream's:
1
2
3
4
5
6
7
8
string Clist()
{                                        
	string triviaName;
	fIn.open("list.csv");
	getline( fIn, triviaName, ',' );
	cout << triviaName;
	return triviaName;
}

http://www.cplusplus.com/reference/string/getline/

Hope this helps.
Thanx Duoas thats perfect
Topic archived. No new replies allowed.