Need help converting Psuedocode to C++

Now that our campus has been closed I have no access to the resources I would of before, like a helpful professor. How would I convert this psuedocode to C++

//start
// Declarations
// InputFile geraldineFile
// InputFile geraldFile
// OutputFile mergedFile
// num geraldineNum
// num geraldineArea
// string geraldineLastName
// string geraldineAddress
// num geraldNum
// num geraldArea
// string geraldLastName
// string geraldAddress
// string areBothAtEnd= "N"
// num END_NUM = 999

// getReady()
// while areBothAtEnd <> "Y"
// mergeRecords()
// endwhile
// finishUp()
//stop

// getReady()
// open geraldineFile "GeraldinesLandscaping.txt"
// open geraldFile "GeraldsLandscaping.txt"
// readGeraldine()
// readGerald()
// checkEnd()
// return

// readGeraldine()
// input geraldineNum, geraldineLastName, geraldineAddress, geraldineArea from geraldineFile
// if eof then
// geraldineNum = END_NUM
// endif
// return

// readGerald()
// input geraldNum, geraldLastName, geraldAddress, geeraldArea from geraldFile
// if eof then
// geraldNum = END_NUM
// endif
// return

// checkEnd()
// if geraldineNum = END_NUM then
// if geraldNum = END_NUM then
// areBothAtEnd = "Y"
// endif
// endif
// return

// mergeRecords()
// if geraldineNum < geraldNum then
// output geraldineNum, geraldineLastName, geraldineAddress, geraldineArea to mergedFile
// readGeraldine()
// else
// output geraldNum, geraldLastName, geraldAddress, geeraldArea to mergedFile
// readGerald()
// endif
// checkEnd()
// return

// finishUp()
// close geraldineFile
// close geraldFile
// close mergedFile
// return
Let's try one or two of these chunks and see if you can figure the rest out:

// getReady()
// while areBothAtEnd <> "Y"
// mergeRecords()
// endwhile
// finishUp()
//stop


So getReady() - that would be the name of your function:

1
2
3
void getReady() //We'll make it a void function until we find a reason not to
{
}


while areBothAtEnd <> "Y"

This line wants a while loop. The condition being while areBothAtEnd (a string) is less than or greater than "Y". In other words, while the string is NOT equal to "Y":

1
2
3
4
5
6
7
void getReady() //We'll make it a void function until we find a reason not to
{
     while(areBothAtEnd != "Y")
     {

     }
}


// mergeRecords()
// endwhile


So it wants to call this function mergeRecords inside the loop. At that seems to be all it wants to loop to do until it ends:

1
2
3
4
5
6
7
void getReady() //We'll make it a void function until we find a reason not to
{
     while(areBothAtEnd != "Y")
     {
          mergeRecords();
     }
}



// finishUp()
//stop

The first line is another function call. This happens after your loop is over since it's written after "endwhile". Then "stop" would seemingly refer to ending the function. The other functions shown say "return" instead of stop - so this shows that this function has no value to return, it's logical to assume we should keep it a void function as well:

1
2
3
4
5
6
7
8
void getReady() //We'll make it a void function until we find a reason not to
{
     while(areBothAtEnd != "Y")
     {
          mergeRecords();
     }
     finishUp();
}



Let's try this one as well:

// checkEnd()
// if geraldineNum = END_NUM then
// if geraldNum = END_NUM then
// areBothAtEnd = "Y"
// endif
// endif
// return


checkEnd() is the function name:

1
2
3
void checkEnd() //void until we find reason to change it
{
}


// if geraldineNum = END_NUM then
// if geraldNum = END_NUM then

And here it shows two if statements. If geraldineNum = END_NUM THEN if geraldNum = END_NUM

This shows us that we want an if statement which HOLDS another if statement:

1
2
3
4
5
6
7
if(geraldineNum == END_NUM)
{
     if(geraldNum == END_NUM)
     {
          
     }
}


if geraldNum = END_NUM then
// areBothAtEnd = "Y"

This one is saying if the inner if statement is true, THEN areBothAtEnd = "Y" - as in telling you to set the string to "Y":

1
2
3
4
5
6
7
if(geraldineNum == END_NUM)
{
     if(geraldNum == END_NUM)
     {
          areBothAtEnd = "Y";
     }
}


// endif
// endif

This is saying that we're done with the if statements, nothing more than what we've done should be placed inside them.

// return

Hmm... The function is called checkEnd, seemingly to return TRUE or FALSE, but it seems we notify whatever needs it that we've reached the end through the changing of the string to "Y". However, to be safe, let's add the appropriate returns:

1
2
3
4
5
6
7
8
9
10
11
12
13
void checkEnd() //void until we find reason to change it
{
     if(geraldineNum == END_NUM)
     {
          if(geraldNum == END_NUM)
          {
               areBothAtEnd = "Y";
               return true; //We reach this point of it's the end, so return true
          }
     }

     return false; //We reach this point if we haven't reached the end, so return false
}



Hope this has showed you how to go about this on your own.
Last edited on
So when i tried to convert it myself this is what i got, during the compiling process it says there is a problem with my while statement and im not sure what it is


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
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>

using namespace std;

void getReady();
void mergeRecords();
void readGeraldine();
void readGerald();
void checkEnd();
void finishUp();
// Declarations
	ifstream geraldineFile;
	ifstream geraldFile;
	ofstream mergedFile;
	float geraldineNum;
	float geraldineArea;
	string geraldineLastName;
	string geraldineAddress;
	float geraldNum;
	float geraldArea;
	string geraldLastName;
	string geraldAddress;
	bool areBothAtEnd = "N";
	int END_NUM = 999;
	
int main()
{
	getReady();
		while(areBothAtEnd != "Y")
		{
			mergeRecords();
		}
	finishUp();
	return 0;
}
void getReady()
{
	geraldineFile.open("GeraldinesLandscaping.txt",ios::in);
	geraldFile.open("GeraldsLandscaping.txt",ios::in);
	mergedFile.open("MergedLandscaping.txt",ios::in);
	readGeraldine();
	readGerald();
	checkEnd();
	return;
}
void readGeraldine()
{
 geraldineFile >> geraldineNum;
 geraldineFile >> geraldineLastName;
 geraldineFile >> geraldineAddress;
 geraldineFile >> geraldineArea;
 	if (!geraldineFile.eof())
 	{
 		geraldineNum = END_NUM;
	 }
return;
}
void readGerald()
{
	geraldFile >> geraldNum;
	geraldFile >> geraldLastName;
	geraldFile >> geraldAddress;
	geraldFile >> geraldArea;
		if(!geraldFile.eof())
		{
			geraldNum = END_NUM;
		}
	return;	
}
void checkEnd()
{
	if (geraldineNum = END_NUM)
	{
		if (geraldNum = END_NUM)
		{
			areBothAtEnd = END_NUM;
		}
	}
return;	
}
void mergeRecords()
{
	if(geraldineNum < geraldNum)
	{
		mergedFile << geraldineNum << '\n';
		mergedFile << geraldineLastName << '\n';
		mergedFile << geraldineAddress << '\n';
		mergedFile << geraldineArea << '\n';
		void readGeraldine();
	}
	else
	{
		mergedFile << geraldNum << '\n';
		mergedFile << geraldLastName << '\n';
		mergedFile << geraldAddress << '\n';
		mergedFile << geraldArea <<  'n';
		void readGerald();
	}
void checkEnd();
return;	
}
void finishUp()
{
	geraldineFile.close();
	geraldFile.close();
	mergedFile.close();
	return;
}
Last edited on
Use code tags so people can see your code a little better. Also, proper indentation can go a long way.

http://www.cplusplus.com/articles/jEywvCM9/

(I didn't change anything about your 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>

using namespace std;

void getReady();
void mergeRecords();
void readGeraldine();
void readGerald();
void checkEnd();
void finishUp();

// Declarations
ifstream geraldineFile;
ifstream geraldFile;
ofstream mergedFile;
float geraldineNum;
float geraldineArea;
string geraldineLastName;
string geraldineAddress;
float geraldNum;
float geraldArea;
string geraldLastName;
string geraldAddress;
bool areBothAtEnd = "N";
int END_NUM = 999;

int main()
{
  getReady();
    while(areBothAtEnd != "Y")
    {
      mergeRecords();
    }
    finishUp();
return 0;
}

void getReady()
{
  geraldineFile.open("GeraldinesLandscaping.txt",ios::in);
  geraldFile.open("GeraldsLandscaping.txt",ios::in);
  mergedFile.open("MergedLandscaping.txt",ios::in);
  readGeraldine();
  readGerald();
  checkEnd();
  return;
}


void readGeraldine()
{
  geraldineFile >> geraldineNum;
  geraldineFile >> geraldineLastName;
  geraldineFile >> geraldineAddress;
  geraldineFile >> geraldineArea;
  
  if (!geraldineFile.eof())
  {
    geraldineNum = END_NUM;
  }
return;
}


void readGerald()
{
  geraldFile >> geraldNum;
  geraldFile >> geraldLastName;
  geraldFile >> geraldAddress;
  geraldFile >> geraldArea;
  
  if(!geraldFile.eof())
  {
    geraldNum = END_NUM;
  }
return;
}

void checkEnd()
{
  if (geraldineNum = END_NUM)
  {
    if (geraldNum = END_NUM)
    {
      areBothAtEnd = END_NUM;
    }
  }
return;
}


void mergeRecords()
{
  if(geraldineNum < geraldNum)
  {
    mergedFile << geraldineNum << '\n';
    mergedFile << geraldineLastName << '\n';
    mergedFile << geraldineAddress << '\n';
    mergedFile << geraldineArea << '\n';
void readGeraldine();
  }
else
{
  mergedFile << geraldNum << '\n';
  mergedFile << geraldLastName << '\n';
  mergedFile << geraldAddress << '\n';
  mergedFile << geraldArea << 'n';
  
void readGerald();
}
void checkEnd();
return;
}

void finishUp()
{
  geraldineFile.close();
  geraldFile.close();
  mergedFile.close();
return;
}
Last edited on
Hello GabriellaN3,

In addition to what KittyIchigo1 said I have found this link to be of more help
http://www.cplusplus.com/articles/z13hAqkS/

Looking at your pseudo code:

// start. I.E., " int main()".
// Declarations
// InputFile geraldineFile
  // Check that it opened. Unless it means to define the file stream only.
// InputFile geraldFile
  // Check that it opened. Unless it means to define the file stream only.
// OutputFile mergedFile
// num geraldineNum
// num geraldineArea
// string geraldineLastName
// string geraldineAddress
// num geraldNum
// num geraldArea
// string geraldLastName
// string geraldAddress
// string areBothAtEnd= "N"
// num END_NUM = 999

// getReady()
// while areBothAtEnd <> "Y"
// mergeRecords()
// endwhile
// finishUp()
// stop. Closing } of "main".


I do not get the impression that these variables should be global variables. It looks to me like they should be defined in "main" and passed to the functions that need them. Or defined in the functions that need them.

You should try to avoid using global variables. Those variables that start with "constexpr" or "const" are OK because they can not be changed.

Just looking at the code I see some problems.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void getReady()
{
  geraldineFile.open("GeraldinesLandscaping.txt", ios::in);
  geraldFile.open("GeraldsLandscaping.txt",i os::in);
  mergedFile.open("MergedLandscaping.txt", ios::in);

  readGeraldine();

  readGerald();

  checkEnd();

  return;  // <--- Not necessary with a "void" function. the function will return at the closing }.
}

Lines 3 and 4 are both defined as an "ifstream". The "i" says it is an input stream, so the ", ios::in" is either redundant or has no effect.

Line 5 is a different problem. "mergedFile"was defined as an "ofstream" meaning it is an output stream, so I am not sure if the ", ios::in" will be a problem or just ignored.

In all 3 lines the ", ios::in" is not needed.

Also a few blank lines and some spaces make it much easier to read.

In the function "readGeraldine":
1
2
3
4
5
6
7
8
9
10
11
12
13
void readGeraldine()
{
  geraldineFile >> geraldineNum;
  geraldineFile >> geraldineLastName;
  geraldineFile >> geraldineAddress;
  geraldineFile >> geraldineArea;
  
  if (!geraldineFile.eof())
  {
    geraldineNum = END_NUM;
  }
return;
}

Given this code and not knowing what the input file looks like I would guess that line 3 would be the read that sets the "eof" bit which means that you if statement is in the wrong place. When line 3 sets the "eof" bit it still tries to read 3 more fields. In the end the value of all 4 variables may depend on which set of C++ standards you are using.

The variable "END_NUM" is misleading. Being all capital letters you give the impression that it is defined as a constant, but it is not. It should be defined as a constant for the way it is used.

And again the return statement is not needed.

Your program uses 2 input files it helps to provide those file, or a small portion to work with, so everyone can see what you are using and use the same information.

Andy
Hello Hello GabriellaN3,

I have been working on your program, but I got stuck when I started working on the "readGeraldine" function.

What you have will not work and with out seeing of having the input files to look at or use I do not know what to do or how to read them.

I did this with "main" for now, but it still could be changed:
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
int main()
{
	constexpr int END_NUM = 999;

	std::ifstream geraldineFile;
	std::ifstream geraldFile;
	std::ofstream mergedFile;

	double geraldineNum;
	double geraldineArea;
	std::string geraldineLastName;
	std::string geraldineAddress;

	double geraldNum;
	double geraldArea;
	std::string geraldLastName;
	std::string geraldAddress;

	std::string areBothAtEnd{ "N" };
	//bool areBothAtEnd = "N";
	//char areBothAtEnd = 'N';

	std::vector< GeraldinesCusts> geraldinesCusts;
	std::vector< GeraldsCusts> geraldsCusts;

	if (OpenFiles(geraldineFile, geraldFile, mergedFile))
		return 1;

	return 0;  // <--- Not required, but makes a good break point.
}


And the "OpenFiles" function which could be changed to "GetReady" if needed. I did this just to get things started:

The "OpenFiles" function looks like this:
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
bool OpenFiles(std::ifstream& geraldineFile,
	std::ifstream& geraldFile,
	std::ofstream& mergedFile,
	std::vector< GeraldinesCusts>& geraldinesCusts)
{
	geraldineFile.open("GeraldinesLandscaping.txt");

	if (!geraldineFile)
	{
		std::cout << "\n    File \"GeraldinesLandscaping.txt\" Did not open\n";

		return true;
	}

	geraldFile.open("GeraldsLandscaping.txt");

	if (!geraldFile)
	{
		std::cout << "\n    File \"GeraldsLandscaping.txt\" Did not open\n";

		return true;
	}

	mergedFile.open("MergedLandscaping.txt", std::ios::app);

	if (!mergedFile)
	{
		std::cout << "\n    File \"MergedLandscaping.txt\" Did not open\n";

		return true;
	}

	return false;
}

The last parameter is if you need to call the read functions, but you would need one more for the other file.

If you can not share your input files you are pretty much on your own when it comes to the read function, because I do not know what you have to work with and anything I could come up with is likely to be different than what you have to use.

Andy
Topic archived. No new replies allowed.