Having trouble formatting whitespace when reading from a file

So in this program im supposed to be reading characters from a file. So i opened the file and read them into an array,

this is the assignment
Write a C++ program that reads in a text file ( called data8.txt), and prints the file to the screen the text with all extra blanks removed (that is, if there are 2 or more blanks in a row, replace them by a single blank) and changes ALL letters to UPPERCASE letters.
WATCH THAT YOU DON'T DELETE CARRIAGE RETURNS and tabs. Whitespace includes tabs and carriage returns as well as blanks…so you only want to get rid of extra blank characters.


I tried using another while loop to identify the blank spaces, something along the lines of
while(array[count]==" ")&&(array[count+1]==" "
but you cannot test to see if it is equal to a character.
any ideas? i would appreciate any help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include<iostream>
#include<fstream>
#include<cstdlib>
#include<cctype>
using namespace std;
int main()
{
	ifstream instream;
	char character;
	int count=0;
	char filename[100000];
	char newarray[100000];
	instream.open("data8.txt");
	if(instream.fail())
		cout<< "the file could not be opened";
	while(!(instream.eof()))
		{
			instream.get(character);
			cin>>filename[100000];
			toupper(filename[count]);
			count++;
	}
	
Last edited on
I added a few lines for you. see if this works.

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
  #include<iostream>
#include<fstream>
#include<cstdlib>
#include<cctype>
using namespace std;
int main()
{
	ifstream instream;
	char character;
	int count=0;
	char filename[100000];
	//char newarray[100000];
	instream.open("test.txt");
	if(instream.fail())
		cout<< "the file could not be opened";

	bool		blastspace = false;
	while(!(instream.eof()))
	{
		instream.get(character);
		if(character == ' ' && blastspace)
		{
			continue;
		}
		else
		{
			blastspace = false;
		}
		if(character == ' ')
		{
			blastspace = true;
		}
		filename[count] = character;
		toupper(filename[count]);
		count++;
	}
	filename[count] = '\0';;

	std::cout << filename << std::endl;


	return 0;
}
Last edited on
i had tried something like that initially and it was giving me an error that i couldnt set the character variable to a blank, maybe i made some other error, but i will try it out and let you know if it works,

Thanks a lot
ok just tried it and it worked, im not sure i understand what you did with the bool blastspace, its initialized to false, but then defaults to false in the if else statement
That's because it's in a while loop. Essentially, that if statement is done over and over and over, and whether blastspace is true or false is evaluated repeatedly.
blastspace is used to know if we have continued spaces. It is set to true if any space is found and so for any next consecutive spaces, nothing will happen, loop will just continue.
as soon the spaces ends, blastspace will be set to false and normal scenario will happen. try debugging to know whats going on.
Topic archived. No new replies allowed.