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 8714015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:29:13+00:00 2026-06-13T05:29:13+00:00

Let’s say that the user input = 6000 and the number inside input.txt is

  • 0

Let’s say that the user input = 6000 and the number inside input.txt is = 5000. The sum up will be 11000. The number that will be displayed on the screen and the value stored in the file will be overwritten to 11000. Please help me, Thanks

import System.IO

menu :: IO ()
menu = do

handle <- openFile "input.txt" ReadMode  
          contents <- hGetContents handle 
      putStr contents
      hClose handle
      contents <- readFile "input.txt"
      print . sum . read $ contents
      putStr("\nThe amount of money that you want to deposit : ")
      y<-getLine
  • 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-06-13T05:29:14+00:00Added an answer on June 13, 2026 at 5:29 am

    Whats wrong with yor code :

    Lots of problems with your code.

    • Why are you reading two times.
    • Why there is a getLine at the end.
    • The types of most of the functions you have used do not match.
    • Does the input file contains only one line or many lines.

    It is better to see types of functions you are using and then compose them.

    A probable correction of your code from what I can infer you want would be

    import System.IO
    
    main :: IO ()
    main = do
        y<-getLine
        content <- readFile "input.txt"
        let out = sum $ map read $ y:(words content)
        putStrLn $ "\nThe amount of money that you want to deposit : " ++ show out
        writeFile "input.txt" $ show out
    

    Direct answer :

    It is better to use withFile as you want to read and then write. readFile reads the file lazily so there is no guarantee when the file handle will be closed. In the above case if you write the writeFile line before printing the output it might give a runtime error complaining about open handles. So withFile is a safer option and you will be opening file only once as compared to twice in the above case.

    I have considered that you just want to add the number in the first line of the input file with the number you input.

    import System.IO
    
    main = do
        input <- getLine
        out <- sumFile (read input) "input.txt"
        putStr $ "\nThe amount of money that you want to deposit : " ++ show out
    
    sumFile :: Int -> FilePath -> IO Int
    sumFile input fp = withFile fp ReadWriteMode add
        where
            add handle = do
                number <- hGetLine handle
                hSeek handle AbsoluteSeek 0
                let sum = input + read number
                hPutStrLn handle (show sum)
                return sum
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say that I've got a sheet - number one - with over 5000
Let's say I have a method in java, which looks up a user in
Let me explain best with an example. Say you have node class that can
Let's say that I have a SQLite database that I create in a separate
Let's say after I had login I will be prompt to enter the Name
Let's say I have multiple requirements for a password. The first is that the
Let's assume that a user votes for some movies in a scale of 1
Let's say that I have a date in R and it's formatted as follows.
Let's say I have some text as follows: do this, do that, then this,
Let's say you have a method that expects a numerical value as an argument.

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.