473,837 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
+ 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 3631
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
1369
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 src="images/mt_srch.gif" width="16" height="16" border="0" align="absmiddle"></td> ...will work in IE6, but not in NN6.2. It does not recognize the nowrap, the width attribute, or the onClick.
4
4509
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
1429
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 variable length strings and I need to be able to work out in advance whether to overwrite the old record (if the string occupies the same or less bytes) or append a new one (if the string occupies more)
1
1249
by: Lal - Luxor | last post by:
please help me. how to work object.move will work on ve.net regards lal
2
1338
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 explicitly like Test *ptr = static_cast<Test *>(operator new( 10*sizeof(Test) ));
4
1917
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 with most of the popular browsers? I don't care if it can be done with dreamweaver or if I have to hand code or copy code from somewhere, I just want it to work. Here is what DW creates function MM_controlSound(x, _sndObj, sndFile) { //v3.0 var i,...
2
1522
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 below. The data comes in 40 byte packets via stdin or a binary file. my_Data_pkt(){ syncByte (8bits) XML_type (2bits) XML_subtype (2bits) record_value (3bits)
1
2000
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
9827
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10863
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10560
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10609
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10263
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9390
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7798
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4468
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3120
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.