so here is my code. My getlines for the strings keyword and phrase at lines 44 and 79 respectively don't work. Please help!!!
Code:
#include <cstdlib>
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
string removeAllWhite( string eric)
{
int strLength = eric.length() - 1;
for( int n=0; n < strLength; n++){
if( eric.substr(n, 1) == " ")
{
eric.erase(n, 1);
}
}
return eric;
}
string tolower(string a)
{
int two;
int first = a.size();
for ( int two = 0; two < first; ++two)
{ a[two] = tolower(a[two]); }
return a;
}
int main(int argc, char *a0rgv[])
{
begining:
string whichone;
cout << "Encode or Decode? ";
cin >> whichone;
whichone = tolower(whichone);
if (whichone != "encode" && whichone != "decode" )
{
cout << "try again" << endl;
goto begining;
}
string keyword;
cout << "Keyword? ";
getline (cin, keyword);
keyword = tolower(keyword);
int keysize = keyword.size();
string keyletters[keysize];
int keyplc[keysize];
int x = 0;
int y = 0;
int k = 0;
string alph;
string compare;
alph = "abcdefghijklmnopqrstuvwxyz";
while (k <= keysize - 1)
{
keyletters[k] = keyword.substr(k,1);
int tester = -1;
tester = compare.find(keyletters[k]);
if(tester == -1)
{
keyplc[k] = alph.find(keyletters[k]);
alph.erase(keyplc[k],1);
compare = compare + keyletters[k];
}
k++;
}
string cypher;
cypher = compare + alph;
/*
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
divider between first part and 2nd
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
*/
if (whichone == "encode")// if Encode
{//starts if statment
string input;
cout << "Phrase? " ;
getline (cin, input);
input = removeAllWhite(input);
int strsize = input.size(); // finds the size of the string
string letters[strsize]; // array that holds the letters of the string
int numbers[strsize];
string yorn;
herea:
cout << "output encoded text to .txt file? Y/N: ";
cin >> yorn;
yorn = tolower(yorn);
if (yorn != "y" && yorn != "n")
{
cout << "try again" << endl;
goto herea;
}
if (yorn == "y")
{//begins output to text if statement
ofstream outputfile;
outputfile.open("output.txt",ios::out);
outputfile.close();
}//ends output to text if statement
while (x <= strsize - 1)
{//begins first while loop
letters[x] = input.substr(x,1);
while ( cypher.substr(y,1) != letters[x])
{//begins nested while loop
y++;
}//ends nested while loop
numbers[x] = y +1;
if (yorn == "y")
{//begins output to text if statement
ofstream outputfile;
outputfile.open ("output.txt",ios::app );
outputfile << numbers[x] << " ";
outputfile.close();
}//ends output to text if statement
cout << numbers[x]<< " " ;
y = 0;
x++;
}//ends while loop
if (yorn == "y")
{//begins output to text if statement
ofstream outputfile;
outputfile.open ("output.txt",ios::app);
outputfile << "END" << endl;
}//ends output to text if statement
cout << "END" << endl;
} // ends if statement
/*
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
divider between 2nd part and third
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
*/
if (whichone == "decode")
{//begins decode1 if statement
int digits;
cout << "number of digts to be decoded? ";
cin >> digits;
string decoded[digits];
int inputs[digits];
string yorn;
hereb:
cout << "Read digits to be decoded from .txt file? Y/N: ";
cin >> yorn;
yorn = tolower(yorn);
if (yorn != "y" && yorn != "n")
{
cout << "try again" << endl;
goto hereb;
}
if (yorn == "y")
{//begins yes if statement
string a;
hered:
cout <<"Is the .txt file in the same directory as the program? Y/N: ";
cin >> a;
a = tolower(a);
if (a != "y" && a != "n")
{
cout << "try again" << endl;
goto hered;
}
if (a == "y")
{
ifstream outputfile("output.txt");
int f = 0;
while (f < digits)
{
outputfile >> inputs[f];
f++;
}
}
if (a =="n")
{
char path [256];
cout << "Please enter the path for the desired file: ";
//cin >> path;
cin.getline (path,256);
cout << path << endl;
system("pause");
ifstream outputfile(path);
int f = 0;
while (f < digits)
{
outputfile >> inputs[f];
f++;
}
}
}//ends yes if statement
if (yorn =="n")
{//begins no if statement
cout << "Enter digts seperated by spaces: ";
int f = 0;
while (f < digits)
{
cin >> inputs[f];
f++;
}
}//ends no ifstatement
int f = 0;
while (f < digits)
{//begins decode2 if statement
decoded[f] = cypher.substr(inputs[f]-1,1);
cout << decoded[f] ;
f++;
}//ends decode2 if statement
cout <<endl;
}//ends decode1 if statement
string yesno;
herec:
cout << "run again? Y/N: ";
cin >> yesno;
//yesno = tolower(yesno);
if (yesno != "y" && yesno != "n")
{
cout << "try again" << endl;
goto herec;
}
if (yesno == "y")
{
goto begining;
}
if (yesno == "n")
{//begins don't run again if statment
return EXIT_SUCCESS;
}//ends don't run again if statement
}
Comment