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
|
#include <iostream>
#include <string>
#include <cctype>
#include <fstream>
using namespace std;
//Function Prototypes//
void charString(string &, string &, string &);
void reverseString(string &, string &, string &);
void is_alpha_length(const string& line, int &length);
//------------------//
int main()
{
string x, y, z;
charString(x, y, z);
reverseString(x, y, z);
}
// line1, line2 and line3 are passed by reference, so they'll be changed in the function and you don't need to return anything
void charString(string &line1, string &line2, string &line3 )
{
ifstream inFile;
// Holds the length of each of the lines.
int length1 = 0;
int length2 = 0;
int length3 = 0;
inFile.open("testdata.txt"); //Opens test .txt file
//Gets the full string line.
getline(inFile, line1);
getline(inFile, line2);
getline(inFile, line3);
inFile.close(); // unless you still need it...
//Gets the strings length.
// use a function instead of repeating the same code thrice
is_alpha_length(line1, length1);
is_alpha_length(line2, length2);
is_alpha_length(line3, length3);
cout << length1 << endl;
cout << length2 << endl;
cout << length3 << endl;
}
void reverseString(string &line1, string &line2, string &line3) //Trying to pass the values from lines 1-3 here...
{
cout << "Here is the String the correct way." << endl;
cout << line1 << endl << line2 << endl << line3 << endl;
cout << "Here is the String backwards." << endl;
cout << "Nothing here yet :/" << endl;
}
// original loop:
//for(int i = 0; i < line1.length(); i++) //Determines isalpha string length for line 1
//{
// if(isalpha(line1[i]))
// {
// length1++;
// }
//}
inline void is_alpha_length(const string& line, int &length)
{
// a simpler way for this kind of loop
for (auto i : line) if (isalpha(i)) ++length;
}
|