getline for string will not work

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • erictheone
    New Member
    • Mar 2009
    • 13

    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!!!
    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
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    They work for me using Visual Studio.NET 2008.

    What do you mean by "they don't worrk"?

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      I don't know if it is any prettier, but you could replace your goto's with do...while:
      Code:
      int firstLoop = 1;
      do {
         if (!firstLoop) {
            <print error message>;
         }
         <do your thing>;
         firstLoop = 0;
      } while (<condition>);

      Comment

      • erictheone
        New Member
        • Mar 2009
        • 13

        #4
        what happens is there isn't a chance for me to input the text. So I guess you could say it skips the line.

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          Originally posted by erictheone
          what happens is there isn't a chance for me to input the text. So I guess you could say it skips the line.
          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.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            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...

            Comment

            Working...