Having trouble outputting correctly.

I have to write a program that takes an input file formatted like this.

FR3D,jones,123456-7891,12-123456B
joe,jones,1234a6-7891,12-123456B


The program is suppose to check for and fix capitalization. If the program comes across a number in the name or a letter in the numbers its suppose to output it blank ,Jones rather then Fr3d,Jones. The problem I'm having is making that actually work with what I set up originally. I getline one of the lines and store it into a character array then loop through it fixing the caps and inserting a whitespace if comes across a number in the name section and inserting an x if it comes across a letter in the number section. But from here I'm lost as to how to output, I would appreciate the help.

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
 #include<iostream>
#include<fstream>
#include<iomanip>
#include<cctype>
using namespace std;

void validity(char[]);
void ReadInputRecord(char[], const int, ifstream&);
int OpenInputFile(ifstream&);
int OpenOutputFile(ofstream&);

int main()
{
	ofstream output;
	ifstream input;
	const int SIZE = 256;
	char storeddata[SIZE];
	OpenInputFile(input);
	OpenOutputFile(output);
	ReadInputRecord(storeddata, SIZE, input);
}

void ReadInputRecord(char storeddata[], const int SIZE, ifstream &input)
{
	while (input.getline(storeddata, SIZE))
	{
		validity(storeddata);
		cout << storeddata[0];
	}
}

void validity(char storeddata[])
{

	bool Capital = false;
	char CharacterCheck;
	int CommaCounter = 0;
	int WrongInputCheck = 0;
	for (int ArrayIndex = 0; storeddata[ArrayIndex] != NULL; ArrayIndex++)
	{
		CharacterCheck = storeddata[ArrayIndex];
		for (; storeddata[WrongInputCheck] != NULL; WrongInputCheck++)
		{
			if (storeddata[WrongInputCheck] == ',')
				CommaCounter++;
			if (isdigit(storeddata[WrongInputCheck]) != 0 && CommaCounter < 2)
				storeddata[WrongInputCheck] = ' ';
			if (isalpha(storeddata[WrongInputCheck]) != 0 && CommaCounter >= 2)
				storeddata[WrongInputCheck] = 'x';			
		}
		if (storeddata[ArrayIndex] == ',')
			Capital = false;

		if (isalpha(storeddata[ArrayIndex]) != 0)
		{
			if (Capital == false && (isupper(storeddata[ArrayIndex]) == false || isupper(storeddata[ArrayIndex]) == true))
			{
				char UpperSwap = storeddata[ArrayIndex];
				storeddata[ArrayIndex] = toupper(UpperSwap);
				Capital = true;
			}
			else if (Capital == true && islower(storeddata[ArrayIndex]) == false)
			{
				char LowerSwap = storeddata[ArrayIndex];
				storeddata[ArrayIndex] = tolower(LowerSwap);
				Capital = true;
			}
		}
	}
}

int OpenInputFile(ifstream &input)
{
	input.open("CustomerInput.txt");

	if (input.fail())
	{
		cout << "Can't open the input file.\n";
		return(0);
	}
	return(1);
}


int OpenOutputFile(ofstream &output)
{
	output.open("CustomerOut.txt");

	if (output.fail())
	{
		cout << "Can't open the output file.\n";
		return(0);
	}
	return(1);
}
Try using string, they come with many built-in functions! Also, the ctype.h header has a function called isdigit which returns true or false if its a numeric digit!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Char charFromFile;
string sentence;

//while char can be read from file, test if number, if it is, insert white space, if not, add it to our string!

while ( file >> charFromFile )
{
   if ( isdigit(charFromFile)
    {
      sentence += " ";
    }
  else
  {
    sentence += charFromFile;
  }
}
Last edited on
As for outputting it to a file, I would suggest creating a string vector to store each new line read from file and formatted correctly, which will make it much simpler to output. To output, cycle through the vector, and after each element, end the line!
Topic archived. No new replies allowed.