473,471 Members | 2,613 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

getline for string will not work

13 New Member
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 3599
weaknessforcats
9,208 Recognized Expert Moderator Expert
They work for me using Visual Studio.NET 2008.

What do you mean by "they don't worrk"?
Mar 19 '09 #2
donbock
2,426 Recognized Expert Top Contributor
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
erictheone
13 New Member
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,426 Recognized Expert Top Contributor
@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 Recognized Expert Moderator Expert
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

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

Similar topics

7
by: Mark | last post by:
Hi - could anyone please tell me why this line: <td width="116" height="22" nowrap id="toptSearch" class="mtTabOnTop" onClick="changeLoc('fixit_proceduresresultstick.asp','search')">Search <img...
4
by: Field | last post by:
Hi, the following snippet shows once executed this output: 2 2 I'd have rather expected this output: 2 10
2
by: Claire | last post by:
Is there an easy way to calculate the number of bytes that a UTF8 encoded string will occupy within a file in advance of writing? I am writing records to a complex file. These records contain...
1
by: Lal - Luxor | last post by:
please help me. how to work object.move will work on ve.net regards lal
2
by: Money | last post by:
When we do this Test *ptr=new Test; then runtime memory manager will allocate 10*sizeof(Test) + X bytes(X varying from implementation to implementation) But instead if I call operator new...
4
by: alice | last post by:
I'm trying to get sounds to play on image mouseovers using dreamweaver 8, and I found out that the code it generates to do this only works for IE. Is there a way to do this so that it will work...
2
by: Damfino | last post by:
Hi all, Newbie question here wrt defining a class that will work on bits read from a binary file. How would you go about doing it? As an example please look at the structure of my data given...
1
by: Bergamot | last post by:
marss wrote: You may be trying to fix the wrong problem. Post a URL, then maybe we'll see a better way to accomplish whatever your goal is. -- Berg
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.