469,076 Members | 1,441 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,076 developers. It's quick & easy.

getline for string will not work

so here is my code. My getlines for the strings keyword and phrase at lines 44 and 79 respectively don't work. Please help!!!
Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib>
  2. #include <string>
  3. #include <iostream>
  4. #include <fstream>
  5. using namespace std;
  6. string removeAllWhite( string eric)
  7. {
  8.  
  9.     int strLength = eric.length() - 1;
  10.     for( int n=0; n < strLength; n++){
  11.            if( eric.substr(n, 1) == " ")
  12.            {
  13.                eric.erase(n, 1);
  14.  
  15.            }
  16.  
  17.  
  18.            }
  19.     return eric;
  20. }
  21.  
  22. string tolower(string a)  
  23. {
  24. int two;
  25.   int first = a.size();           
  26.      for ( int two = 0; two < first; ++two)
  27.    {    a[two] = tolower(a[two]);   }
  28.    return a;
  29. }
  30. int main(int argc, char *a0rgv[])
  31. {
  32.     begining:
  33.     string whichone;
  34.     cout << "Encode or Decode? ";
  35.     cin >> whichone;   
  36.     whichone = tolower(whichone);
  37. if (whichone != "encode" &&  whichone != "decode" )
  38. {
  39.               cout << "try again" << endl;
  40.  goto begining;
  41.     string keyword;
  42.     cout << "Keyword? "; 
  43.     getline (cin, keyword);
  44.     keyword = tolower(keyword);  
  45.     int keysize = keyword.size();  
  46.     string keyletters[keysize];
  47.     int keyplc[keysize];
  48.     int x = 0;
  49.     int y = 0;
  50.     int k = 0;
  51.     string alph;
  52.     string compare;   
  53.            alph = "abcdefghijklmnopqrstuvwxyz";
  54.      while (k <= keysize - 1)
  55.      { 
  56.           keyletters[k] = keyword.substr(k,1);
  57.           int tester = -1;
  58.           tester = compare.find(keyletters[k]);
  59.            if(tester == -1)
  60.            {
  61.           keyplc[k] = alph.find(keyletters[k]); 
  62.           alph.erase(keyplc[k],1);
  63.           compare = compare + keyletters[k];        
  64.            }
  65.           k++;
  66.            }             
  67.     string cypher;
  68.           cypher = compare + alph;
  69.     /* 
  70.        !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  71.        divider between first part and 2nd
  72.        !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  73.                                          */
  74.     if (whichone == "encode")// if Encode
  75.      {//starts if statment  
  76.     string input;
  77.     cout << "Phrase? " ;
  78.     getline (cin, input);
  79.     input = removeAllWhite(input);
  80.     int strsize = input.size(); // finds the size of the string
  81.     string letters[strsize]; // array that holds the letters of the string
  82.     int numbers[strsize];
  83.     string yorn;
  84.     herea:
  85.     cout << "output encoded text to .txt file? Y/N: ";
  86.     cin >> yorn;
  87.      yorn = tolower(yorn);
  88.    if (yorn != "y" && yorn != "n")
  89.     {
  90.          cout << "try again" << endl;
  91.          goto herea;
  92.          }
  93.    if (yorn == "y")
  94.    {//begins output to text if statement
  95.     ofstream outputfile;
  96.     outputfile.open("output.txt",ios::out);
  97.     outputfile.close();
  98. }//ends output to text if statement
  99.        while (x <= strsize - 1)
  100.      {//begins first while loop          
  101.            letters[x] = input.substr(x,1);
  102.                                 while ( cypher.substr(y,1) != letters[x])
  103.                                        {//begins nested while loop
  104.                                                         y++;
  105.                                                          }//ends nested while loop
  106.            numbers[x] = y +1;
  107.  
  108.            if (yorn == "y")
  109.            {//begins output to text if statement
  110.                      ofstream outputfile;
  111.                     outputfile.open ("output.txt",ios::app );
  112.            outputfile << numbers[x] << " ";
  113.            outputfile.close();
  114.            }//ends output to text if statement
  115.             cout << numbers[x]<< " " ;
  116.            y = 0;
  117.            x++;           
  118.            }//ends while loop            
  119.            if (yorn == "y")
  120.            {//begins output to text if statement
  121.            ofstream outputfile;
  122.            outputfile.open ("output.txt",ios::app);
  123.           outputfile << "END" << endl;
  124.            }//ends output to text if statement
  125.                    cout << "END" << endl;
  126.            } // ends if statement
  127.            /* 
  128.            !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  129.            divider between 2nd part and third
  130.            !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  131.                                              */
  132.   if (whichone == "decode")
  133.   {//begins decode1 if statement
  134.                int digits;
  135.                  cout << "number of digts to be decoded? ";
  136.               cin >> digits;
  137.                string decoded[digits];
  138.               int inputs[digits];        
  139.                string yorn;
  140.                hereb:
  141.                 cout << "Read digits to be decoded from .txt file? Y/N: ";
  142.                 cin >> yorn;
  143.                 yorn = tolower(yorn);
  144.  
  145.     if (yorn != "y" && yorn != "n")
  146.     {
  147.          cout << "try again" << endl;
  148.          goto hereb;
  149.          }                
  150.                if (yorn == "y")
  151.                {//begins yes if statement
  152.                          string a; 
  153.                          hered:
  154.                          cout <<"Is the .txt file in the same directory as the program? Y/N: ";
  155.                          cin >> a;  
  156.                          a = tolower(a);
  157.                 if (a != "y" && a != "n")
  158.     {
  159.          cout << "try again" << endl;
  160.          goto hered;
  161.          }  
  162.                          if (a == "y")
  163.                          {
  164.                                ifstream outputfile("output.txt");
  165.                                int f = 0;
  166.                     while (f < digits)
  167.                     {
  168.                           outputfile >> inputs[f];
  169.                           f++;
  170.                           }
  171.                               } 
  172.                            if (a =="n")
  173.                            {
  174.                                  char path [256];
  175.                                 cout << "Please enter the path for the desired file: ";
  176.  
  177.                                 //cin >> path;
  178.                                 cin.getline (path,256);
  179.                                 cout << path << endl;
  180.                                 system("pause");
  181.                                 ifstream outputfile(path);
  182.  
  183.                                 int f = 0;
  184.                     while (f < digits)
  185.                     {
  186.                           outputfile >> inputs[f];
  187.                           f++;
  188.                           }
  189.                                 }   
  190.                     }//ends yes if statement          
  191.                if (yorn =="n")
  192.                {//begins no if statement
  193.  
  194.               cout << "Enter digts seperated by spaces: ";
  195.               int f = 0;
  196.               while (f < digits)
  197.               {
  198.                     cin >> inputs[f];
  199.                     f++;
  200.                     }
  201.                     }//ends no ifstatement
  202.  
  203.                     int f = 0;
  204.                     while (f < digits)
  205.                     {//begins decode2 if statement
  206.                           decoded[f] = cypher.substr(inputs[f]-1,1);
  207.                           cout << decoded[f] ;
  208.                           f++;
  209.                           }//ends decode2 if statement
  210.                           cout <<endl;    
  211.                }//ends decode1 if statement
  212.                string yesno;
  213.                herec:
  214.                cout << "run again? Y/N: ";
  215.                cin >> yesno;                
  216.                //yesno = tolower(yesno);
  217.                 if (yesno != "y" && yesno != "n")
  218.     {
  219.          cout << "try again" << endl;
  220.          goto herec;
  221.          }
  222.           if (yesno == "y")
  223.           {
  224.                goto begining;
  225.                }           
  226.     if (yesno == "n")
  227.     {//begins don't run again if statment
  228.  
  229.     return EXIT_SUCCESS;
  230. }//ends don't run again if statement
  231. }
  232.  
Mar 19 '09 #1
5 3314
weaknessforcats
9,208 Expert Mod 8TB
They work for me using Visual Studio.NET 2008.

What do you mean by "they don't worrk"?
Mar 19 '09 #2
donbock
2,422 Expert 2GB
I don't know if it is any prettier, but you could replace your goto's with do...while:
Expand|Select|Wrap|Line Numbers
  1. int firstLoop = 1;
  2. do {
  3.    if (!firstLoop) {
  4.       <print error message>;
  5.    }
  6.    <do your thing>;
  7.    firstLoop = 0;
  8. } while (<condition>);
Mar 19 '09 #3
what happens is there isn't a chance for me to input the text. So I guess you could say it skips the line.
Mar 19 '09 #4
donbock
2,422 Expert 2GB
@erictheone
Do you see the "Keyword?" or "Phrase?" prompts?
I trust you would have told us if you keep getting "try again" messages.

Umm ... any chance of confusion between your tolower function for strings and the ctype tolower function that you're using on line 27? I don't know about C++, but it is not uncommon for tolower to be implemented as a macro in C.
Mar 19 '09 #5
weaknessforcats
9,208 Expert Mod 8TB
Are you sure your input stream is not in a fail state?

Should cin encounter bad information, like trying to put a character into an int variable, it sets the fail state for the stream. Every subsequent cin first checks the fail state, and if set, does nothing but just returns. The effect os after the first cin sets the fail state all of the other cin statements appear to not exist.

I don't see any checks after any of your statements that use cin.
Like, if (cin.fail() etc...
Mar 20 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Lal - Luxor | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.