Thread: looping

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    22

    looping

    Hit a bump again. Heres the part of my program thats not going according to plan. with my inputted file i want it to output it exactly the same, then output another copy but in all caps. As of now, it outputs a regular line, then a cap line then regular line and so on . Any help is appreciated


    Code:
    while( inFile)
        {
        getline(inFile, x);
        
        cout << x << endl;
        outFile << x << endl;
        
        for (int i = 0; i < x.length(); i++)
        {
        cout << (char)(toupper(x.at(i)));
        outFile << (char)(toupper(x.at(i)));
        } 
        }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So do you want

    This
    is
    a
    file

    Followed by
    THIS
    IS
    A
    FILE
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    22
    yes!! exactly

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Lines 5 and 6 output the line as read (without converting to upper case). These lines of code are executed immediately after reading every line. Then the code goes on to print the uppercase versions.

    I'll leave you to guess what needs to be removed from your code.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    22
    i want it to output the line as read. I want it to output the entire file as read...and then the entire file as capitals.

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    22
    currently what i have is:
    input file:
    this is
    my file

    output file:
    this is
    THIS IS
    my file
    MY FILE

    instead, it should be:
    output file:
    this is
    my file
    THIS IS
    MY FILE

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by brillpsycho View Post
    i want it to output the line as read. I want it to output the entire file as read...and then the entire file as capitals.
    Currently, you have a single loop that reads each line, writes it verbatim, then writes it in uppercase.

    You need to change the order of events. For example;
    1) A loop that reads the file, and outputs its contents verbatim. Close the file, reopen it for reading, then another loop to read it in and output the contents after converting to upper case.
    2) A loop that reads the file, outputs the contents verbatim, and stores those contents (say, in an array). Then a second loop to loop over elements of that array, converting values to uppercase, and outputting them.


    And, no, I am not going to provide code. If you aren't willing to put the effort in to converting a simple description (and I've given you two such descriptions) into code you have no business programming. Begging other people to do your work is not an effective development technique in the long run. Starting with your description, you should have been able to produce working code within minutes, even as a beginner. Starting with my description, the time you need should be even less, since I've given additional hints about how to implement it.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    22
    OMG Thank you! Im not asking for anyone to do my work for me, i just didn't even think about closing and reopening.

  9. #9
    Registered User
    Join Date
    Sep 2012
    Posts
    22
    But maybe I do have no business programming. I've only been doing it for 5 weeks. I've read and re-read the book I have also. And been putting in hours upon hours trying to learn. Thanks again.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    If you're willing to put in the effort, that's fine. My response may have been terse because some of your earlier posts seemed to keep coming back for more help with no visible sign of having tried to do something with the advice previously given. A few too many people do that here, and unfortunately assume it is their right to expect their work to be done for them. If that's not you, I apologise. If you show signs of effort (eg I did this, it did that, I don't understand why) you'll get more help than if you don't (asking for help, showing no signs of effort, then asking for help again on the same thing). Bear in mind that people in forums only see what you post, we can't read your minds.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. looping help
    By Creatlv3 in forum C++ Programming
    Replies: 3
    Last Post: 05-08-2010, 04:15 PM
  2. looking for help on looping
    By BNG in forum C Programming
    Replies: 4
    Last Post: 02-26-2005, 02:08 AM
  3. Looping
    By DeathDealer in forum C++ Programming
    Replies: 8
    Last Post: 02-23-2005, 01:13 PM
  4. looping help
    By Kazasan in forum C++ Programming
    Replies: 1
    Last Post: 10-24-2004, 05:10 PM
  5. Looping???
    By romeoz in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2003, 08:32 PM