Getting Whitespaces from Text Documents


The short question is: How do I get input from a text file that says something like "the brown cow" to recognize the whitespaces using only the >> operator and not get() or getline() or anything like that.


The object of my program is to take simple text documents and "decrypt them" using a simple number system. Basically, in this case, the number system is 12, 5, 16.

So if you have something like "asb sds" you would take a, and go back 12 positions in the alphabet, and then so on and so forth. However, the problem I'm having is that "white spaces" are supposed to count.

So I need the total number system to be 12,5,16,12,5,16,12 for a case like asb sds. However we are restricted to ONLY using the >> operator to take input from the text file, and using this system I have no idea how to take whitespaces in to account. I tried using noskipws but it didn't seem to help. I know of one way to fix it, but it would make the code fairly brittle if I did it and I think that it is not something my professor wants. (just Couting a space at the end of the do while loop and adding 1 to the place in my decryption array)


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
	
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(){
	string alphabetCaps = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";   
	string alphabetLower = "abcdefghijklmnopqrstuvwxyz";
	int decryptionArray[3] = {12, 5, 16 };			//decryption code
int currentNum = 0;					//area of decryptionArray[3] to use
	int x=0;							/*my professor requires all
										  variables to be at the top
										  this is for a for loop*/
	int lineLoop=0;						// another for loop 
	string fileName;					// for >> operator from iostream
	string lineInFile;					/* file that holds each get (or whatever
										   you call in streaming from files*/
	string eachChar;					// the char from lineInFile to COUT

	ifstream thisfile;					
	thisfile.open("whiskers.txt");
	thisfile >> noskipws >> lineInFile >> ws;
	
	do {	                                                    //While loop to read from file
	for (lineLoop=0; lineLoop<lineInFile.length(); lineLoop++)  //for loop to get each stream 
		{
			eachChar = lineInFile[lineLoop];
			for (int letterChecks = 0; letterChecks < 26; letterChecks++){

				if (lineInFile[lineLoop] == alphabetCaps[letterChecks]) {
					if (letterChecks - decryptionArray[currentNum] < 0)
						eachChar = alphabetCaps[letterChecks + 26 - decryptionArray[currentNum]];
					else
						eachChar = alphabetCaps[letterChecks - decryptionArray[currentNum]];
				}
				if (lineInFile[lineLoop] == alphabetLower[letterChecks]) {
					if (letterChecks - decryptionArray[currentNum] < 0)
						eachChar = alphabetLower[letterChecks + 26 - decryptionArray[currentNum]];
					else
						eachChar = alphabetLower[letterChecks - decryptionArray[currentNum]];
				}
			}
				currentNum++;        //update location in decryptionarray[3]
				if (currentNum > 2)  /*In the event that currentNum is higher than decryption array
									 move decryption back to 0*/
				currentNum = 0;		

				cout << eachChar;    //output the decrpyted char from istream
	}		
		thisfile >> lineInFile;      //update lineInFile
	} while(thisfile);

	return (0);
}
Last edited on
Thank you for responding Zephilinox.

Sorry to be a bother, but I tried using noskipws and it hasn't worked, so I must be using it incorrectly? Could anyone instruct me on the proper use of it in this context?

On line 24:

thisfile >> noskipws >> lineInFile >> ws;

I tried using just thisfile >> noskipws >> lineInFile

without reinserting ws but that just caused my loop to end prematurely.
Last edited on
Topic archived. No new replies allowed.