Help with get.line in my computing project

My project is to produce a working note program with a bit of additional functionality.

So it is mostly file mangement and string manipulation and if I have time hopefully some more complicated ideas.

However currently I have had issues with inputting text into my file
(string size issues and space character issues).

In my program I can write and read the file correctly however I can't use cin >> string because as soon as I use the "space" character when
inputting my string it ends and takes the first characters before the space.

I've heard of get.line methods where getline( cin, <string>) is used as a method if creating a string with spaces.

I understand cin >> string doesn't work because cin/cout is intended for formated strings.

I'm hoping some one can help me use it because my teacher is having issues using it in my program as well.

When I used it, my program just skipped past it with all my attempts or failed to work.

Some of the commented out parts of the program are just information I need to look at as a reminder or to communicate my problem.

I also need the string to be of unlimited size (well not truely unlimited but unlimited so it can handle a large amount of characters).
I think there is something you can do to reduce the restriction on the limit.

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <fstream>
//#include <cctype>
#include <math.h>
#include <cstdlib>
//#include <limits>
#include <string>
//#include <vector>

using namespace std;

void usercontrol();

void writetofile();
void read();
void spellcheck();




string inputtext; // i removed this when doing my 
getline attempt (i declared it in void writetofile(); )

int main()
{
	usercontrol();
	
	//writetofile();   // everything works from user control.
	//read();
	

	return 0;
}

void usercontrol()

{


string choice;


	cout << "enter your choice of action "<< endl;
	cout << "please enter either"<< "write or " << "read" << endl;
	cin >> choice;

	if (choice == "write")
		writetofile();
	else if (choice == "read" ) 
		read();                    
	
//more will go here later. I might use case statements 
//instead of list of "if" statements
	
}

void writetofile()
	
{			
		cout << "enter your text"<< endl;	        
			
		/*string inputtext:*/
			///*while*/ getline( cin, inputtext ); 
// <-- part of the issue I tried to follow an example. However I didn't get it to work. 
//The main issue is the program completely skipped past the getline section of my program and I couldn't get the getline to work. 
//I'd appreciate any help here.


		cin >> inputtext;	 // my current method but it only works with one word. Above is one of my attempts with the getline method.					
			
			

	        
			ofstream out("writefile.txt");  // at some point need to let the user rename/create a new file some how.
			
			out << (inputtext) << endl;    
		out.close(); 
		
		cin.get();	
	}
	

void read()     // the txt file is read correctly from file, 
//but cmd doesn't display it correctly (no space characters!). 
//So the same issue as my input not accepting space characters, 
//my output also ignores space characters.
		{
			char i,array[100];
			i=0;
			ifstream in("writefile.txt");
			while(!in.eof())
				{
					in >> array[i];
					i++;
				}
			for(int j=0;j<i;j++)
			cout << array[j] ;
			in.close();
			cin.get();
		}


	

So any help with showing me what I'm doing wrong with the getline method of inputing a string or if there is a better method any one can show me, I would greatly appreciate. Thankyou
Last edited on
You could try something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void writetofile()
{		
    ofstream out("writefile.txt");
    cout << "enter your text"<< endl;	        
    string inputtext; 

    while (getline(cin, inputtext))
    {
        if (inputtext == "quit")
            break;
        out << inputtext << endl;
    } 
 
}


The only tricky point is terminating the while loop, hence line 9. Ideally you would choose some keyword which would never appear in the normal text.

Or this, an alternative way of expressing the same thing:
1
2
3
4
5
6
7
8
9
void writetofile()
{		
    ofstream out("writefile.txt");
    cout << "enter your text"<< endl;	        
    string inputtext; 

    while (getline(cin, inputtext) && (inputtext != "quit"))
        out << inputtext << endl;
}
Last edited on
Thank you! This is very helpful as I can now input text with spaces :)

I will have to improve my read function to get the data from the file correctly. Is there a specific file management statement or command to output the data from the file correctly? (sorry if I'm asking too much, I can understand if you don't wish to answer that)
The code to read from the file could be almost identical to the above - I'm just improvising here, but this should work:

1
2
3
4
5
6
7
8
void read()     
{
    string line;
    ifstream in("writefile.txt");

    while (getline(in, line))
        cout << line << endl;
}
Last edited on
Wow you're fast. Thank you for all your help.

Well I think I can manage for now. I mostly have to go on to string manipulation and consider what else to add. Such as an alternative writefile close method.

I hope if I get really stuck again I can come back and get some more assistance if I really need it.
Topic archived. No new replies allowed.