Jump to content

Help with a CLI calculator (Go)

Go to solution Solved by Sauron,

No offense but that for loop at the end is a mess :S

 

Why not something like this

import "strings"

...
  
for {
  
  ... //your code before the final loop

  var run = false
  for {
    fmt.Scan(&resume)
    var low_resume = strings.ToLower(resume)
    if low_resume == "y" || low_resume == "yes" {
      run = true
      break
    } else if low_resume == "n" || low_resume == "no" {
      break
    } else {
      fmt.Println("Invalid input, try again - would you like to repeat the process? [y/n]")
    }
  }
  if !run {
    break
  }
}

 

Hello, I am trying to get an assignment wrapped up for school and can't figure out how to make this command line calculator work. I am trying to have it accept input from the user to determine whether or not it should repeat or end, and it completely stalls and I have to ^C to get it to stop. Any ideas?

 

package main

import (
	"fmt"
	_ "fmt"
	"os"
	//"sort"
)

func main() {
	fmt.Println("\n*****************************************************")
	fmt.Println("**** Welcome to the world's best CLI calculator! ****")
	fmt.Println("*****************************************************")
	fmt.Println("\n\nLet's get started!")

	var a, b, c int = 1, 1, 3
	var operator, resume, valid string = "", "y", "n"

	for c == 3 {
		fmt.Println("Please enter your first int: ")
		_, err := fmt.Scan(&a)
		if err != nil {
			fmt.Println("Invalid;", err)
			os.Exit(0)
		}
		fmt.Println("Please enter your second int: ")
		_, err2 := fmt.Scan(&b)
		if err2 != nil {
			fmt.Println("Invalid;", err2)
			os.Exit(0)
		}

		fmt.Println("Please choose your operator (+ - * /): ")
		_, err3 := fmt.Scan(&operator)
		if err3 != nil {
			fmt.Println("Invalid;", err3)
			os.Exit(0)
		}

		//Choose an operator.

		switch operator {
		case "/":
			fmt.Println("Your equation is ", a, operator, b, "=", a/b, ". \nWould you like to go again? (y/n): ")
		case "*":
			fmt.Println("Your equation is ", a, operator, b, "=", a*b, ". \nWould you like to go again? (y/n): ")
		case "-":
			fmt.Println("Your equation is ", a, operator, b, "=", a-b, ". \nWould you like to go again? (y/n): ")
		case "+":
			fmt.Println("Your equation is ", a, operator, b, "=", a+b, ". \nWould you like to go again? (y/n): ")
		default:
			fmt.Println("Invalid input.")

		}

		_, err4 := fmt.Scan(&resume)
		if err4 != nil {
			fmt.Println("Invalid;", err4)
			os.Exit(0)
		}

		//Do we repeat the process?

		for valid == "n" {
			switch resume {
			case "y":
				valid := "y"
				_ = valid
			case "yes":
				valid := "y"
				_ = valid
			case "yeS":
				valid := "y"
				_ = valid
			case "yES":
				valid := "y"
				_ = valid
			case "yEs":
				valid := "y"
				_ = valid
			case "Y":
				valid := "y"
				_ = valid
			case "Yes":
				valid := "y"
				_ = valid
			case "YeS":
				valid := "y"
				_ = valid
			case "YEs":
				valid := "y"
				_ = valid
			case "YES":
				valid := "y"
				_ = valid
			case "n":
				valid := "y"
				_ = valid
			case "N":
				valid := "y"
				_ = valid
			case "no":
				valid := "y"
				_ = valid
			case "No":
				valid := "y"
				_ = valid
			case "nO":
				valid := "y"
				_ = valid
			case "NO":
				valid := "y"
				_ = valid
			default:
				fmt.Println("Invalid input. Calculate again? (y/n): ")
				_, err5 := fmt.Scan(&resume)
				if err5 != nil {
					fmt.Println("Invalid;", err5)

				}
			}
		}
	}
}

 

Edited by manicottimuffin
correct formatting
Link to comment
Share on other sites

Link to post
Share on other sites

No offense but that for loop at the end is a mess :S

 

Why not something like this

import "strings"

...
  
for {
  
  ... //your code before the final loop

  var run = false
  for {
    fmt.Scan(&resume)
    var low_resume = strings.ToLower(resume)
    if low_resume == "y" || low_resume == "yes" {
      run = true
      break
    } else if low_resume == "n" || low_resume == "no" {
      break
    } else {
      fmt.Println("Invalid input, try again - would you like to repeat the process? [y/n]")
    }
  }
  if !run {
    break
  }
}

 

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, Sauron said:

No offense but that for loop at the end is a mess :S

 

Why not something like this


import "strings"

...
  
for {
  
  ... //your code before the final loop

  var run = false
  for {
    fmt.Scan(&resume)
    var low_resume = strings.ToLower(resume)
    if low_resume == "y" || low_resume == "yes" {
      run = true
      break
    } else if low_resume == "n" || low_resume == "no" {
      break
    } else {
      fmt.Println("Invalid input, try again - would you like to repeat the process? [y/n]")
    }
  }
  if !continue {
    break
  }
}

 

Thanks! That did it

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, manicottimuffin said:

Thanks! That did it

actually rereading it I spotted a small mistake, that last if statement should be

if !run {
  break
}

 

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

may be a little late here but there are a couple of things which stand out to me, where I am not the best at go or any programming for that fact: i would be looking at some abstraction, so I would look to have main loop which others have suggested which is as simple as "for run{" (see line 65) which will loop till you break the condition. 

 

look at giving different exit codes i.e. for me I usually try and give a different code per error so when i have a larger program i can take the exit code and it is quicker to debug (trust me when you strart wrting longer code you will thank me)

 

not sure if you have covered it but look at abstracting what you can, i.e. in mine i have kept it low level to look for a int specifically in one of my functions, an opperand in another, and a return value in another.

 

also you can see that there are different ways to validate the continue, for me I wanted to simply have the user and anything begging with "y" to continue or it would exit.

 

hope this helps, and enjoy Go :)

main.go

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×