file input & modifying strings

Hello, I hope you are having a fantastic evening!

I am trying to write code that takes a textfile, gets the information in strings, shifts it (by the users choice of number) and then flips it. I believe that my code should works (and it compiles) but all that it does is ask for the user number, have you put it in, and then quit. I set up debugging statements so that it should print after the string is manipulated, but it doesn't.

I would GREATLY appreciate any help that you could give, either in code or suggestions.

Again, thank you! :)

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

using namespace std;

void getFile (ifstream&, ofstream&, string);
void getNum (int&);
void shiftString (int&, string, ifstream&, ofstream&);
void stringFlip (int&, string);
void printEncode (string, ofstream&);


int main()
{
	
	// Declare vaiables
	ifstream dataIn;
	ofstream dataOut;
	string stringInput;
	int userChoice;
	string outputStringShift;

	// Call Functions
	getFile (dataIn, dataOut, stringInput);
	getNum (userChoice);
	shiftString (userChoice, stringInput, dataIn, dataOut);
	stringFlip (userChoice, stringInput);

	return 0;
}

void getFile (ifstream& inData, ofstream& outData, string inputString)
{
	
	// Call file
	inData.open("plaintext.txt");

	// Check that open succeeded
	if(inData.fail())
	{
		cout << "Failed to open file!" << endl;
		exit(1);
	}

	// Extract data
	inData >> inputString;

	// Continue to extract data until file is done
	while(!inData.eof())
		{
			inData >> inputString;
		}

}

// Parameters: an int reference parameter to get users choice
// Returns: none
// Description: gets users choice
void getNum (int& userNum)
{
	cout << "Please enter the number of letters you want the encoder to be based off of" << endl;
	cin >> userNum;
}

// Parameters: int reference parameter of users choice
// Returns: 
// Description: program to shift the letters of the input
void shiftString (int& userNum, string inputString, ifstream& inData, ofstream& outData)
{

	int i = 0;
// know I need some loop here - not sure what
	while (i < inputString.length());
	{
		inputString[i] = int(inputString[i]) + userNum;

		i ++;
		cout << inputString;
	}
	

} 


// Parameters: int reference parameter of users choice
// Returns: 
// Description: program to reverse the letters of the input (after they have been shifted)
void stringFlip (int& userNum, string inputString)
{
	int i = 0;

	while (i < inputString.length());
	{
		inputString = string (inputString.rbegin(), inputString.rend());
		cout << inputString;
	}
}


// Parameters:
// Returns: 
// Description: write encoded text to an output file
void printEncript (string inputString, ofstream& outData)
{
	outData << inputString;
}
Well, I'm not sure about the structure if the program.

Function getFile() has both an inout and output stream as parameters, but it doesn't do any thing with the output stream.

It opens the input file and checks that it works - good.

It reads tries to read the first string - ok.
It then reads and discards all of the strings until eof(). Really not sure this is a good idea. Do you only want to keep the only the last item in the file?

That was as far as I got, not because the rest of the code was either good or bad, but simply because this part of the design doesn't make sense to me.
Thank you, that was helpful. I am trying to modify the whole file, but I'm not sure how I'm supposed to get it to store the whole thing (and I'm supposed to use strings) any suggestions?
Well, as a high-level view of the requirements, I'd say you need to
repeat this sequence:
• read a single string
• process that string
• output the result

Now the input and output are commonplace actions. But the processing in the middle I don't fully understand.

I think it goes something like this (pseudocode):
1
2
3
4
5
6
7
8
open inData
open outData
while (inData >> string)
{
    shift string
    reverse string
    string >> outData
}

but I'm not sure that the current functions are called in this manner.

Topic archived. No new replies allowed.