Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6945639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:27:44+00:00 2026-05-27T13:27:44+00:00

I have code that works but only once. I need an input char a

  • 0

I have code that works but only once. I need an input char a to be swapped with an input char b. The first time through the loop, it swaps the two selected chars fine, but on the second and following iterations it does nothing but keep the outFile the same. How can I swap more than two chars until I want to stop?

ifstream inFile("decrypted.txt");   
ofstream outFile("swapped.txt");

const char exist = 'n';
char n = '\0';
char a = 0;
char b = 0;

cout<<"\nDo u want to swap letters? press <n> to keep letters or any button to continue:\n"<<endl;
cin>>n;

while (n != exist)                          
{
    cout<<"\nWhat is the letter you want to swap?\n"<<endl;
    cin>>a;             
    cout<<"\nWhat is the letter you want to swap it with?\n"<<endl;
    cin>>b;
    if (inFile.is_open())
    {
        while (inFile.good())
        {   
            inFile.get(c);

            if( c == b )
            {
                outFile<< a;
            }
            else if (c == a)
            {
                outFile<< b;
            }
            else
            {
                outFile<< c;
            }                               
        }
    }
    else
    {
        cout<<"Please run the decrypt."<<endl;
    }

    cout<<"\nAnother letter? <n> to stop swapping\n"<<endl;
    cin>>n;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:27:44+00:00Added an answer on May 27, 2026 at 1:27 pm

    Consider a different approach.

    Collect all the character swaps in a lookup table. By default translate['a'] == 'a', the input character is the same as the output character. To swap a with z just set translate['a'] = 'z' and translate['z'] = 'a'.

    Then perform a single pass over the file, copying and translating at the same time.

    #include <array>
    #include <fstream>
    #include <iostream>
    #include <numeric>
    
    int main()
    {
        std::array<char,256> translate;
        std::iota(translate.begin(), translate.end(), 0); // identity function
    
        for (;;)
        {
            char a, b;
            std::cout << "\nEnter ~ to end input and translate file\n";
            std::cout << "What is the letter you want to swap? ";
            std::cin >> a;
            if (a == '~') break;
            std::cout << "What is the letter you want to swap it with? ";
            std::cin >> b;
            if (b == '~') break;
            std::swap(translate[a], translate[b]); // update translation table
        }
    
        std::ifstream infile("decrypted.txt");
        std::ofstream outfile("swapped.txt");
    
        if (infile && outfile)
        {
            std::istreambuf_iterator<char> input(infile), eof;
            std::ostreambuf_iterator<char> output(outfile);
            // this does the actual file copying and translation
            std::transform(input, eof, output, [&](char c){ return translate[c]; });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am querying information from Active Directory . I have code that works, but
I have this code that works in a unit test but doesn't work when
I have some code that works fine in FF but not in IE. I
i have some javascript roll over code that works fine in firefox but when
I have some very basic semaphore code that works great on Linux, but cannot
I have a code that works 99% of the time since is deploy in
I have code that works great for adding a button to the toolbar: NSArray*
I have some Python code that works correctly when I use python.exe to run
I have some JavaScript code that works in IE containing the following: myElement.innerText =
I have the following code that works on PHP5 to send a HTTP POST

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.